Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino I/O function


May 15, 2021 Arduino


Table of contents


The pins on the Arduino board can be configured as inputs or outputs. W e will explain the function of the pins in these modes. It is important to note that most Arduino analog pins can be configured and used in exactly the same way as digital pins.

The pin is configured as INPUT

The Arduino pins are configured as inputs by default, so you do not need to explicitly declare them as inputs using pinMode() when using them as inputs. P ins configured in this manner are called in a high impedance state. The input pin has very little requirement for the sampling circuit, equivalent to a series resistance of 100 megaO in front of the pin.

This means that the current required to switch the input pin from one state to another is very small. /b10> This allows pins to be used for tasks such as implementing capacitive touch sensors or reading LEDs as photodes.

Pins configured as pinMode (pin, INPUT) (no wires are connected to them, or there are wires connected to them that are not connected to other circuits), report seemingly random changes in pin status, and pick up electronic noise from the environment or the state of the nearby pins with capacitive coupling.

Pull-up resistor

If there is no input, the pull-up resistor is typically used to guide the input pin to a known state. /b10> This can be achieved by adding a pull-up resistor (to 5V) or a pull-down resistor (ground resistance) to the input. /b11> A 10K resistor is a good value for a pull-up or pull-down resistor.

Using the built-in pull-up resistor, the pins are configured as inputs

The Atmega chip has 20,000 pull-up resistors built in and is accessible through software. /b10> These built-in pull-up resistors INPUT_PULLUP pinMode() is set to a high-level device. /b11> This effectively reverses the behavior of THEPUT mode, where HIGH indicates that the sensor is off and LOW indicates that the sensor is on. /b12> The value of this pull-up depends on the microcontroller used. /b13> On most AVR-based boards, this value is guaranteed to be between 20k and 50k. /b14> On Arduino Due, it's between 50k and 150k. /b15> For exact values, refer to the data sheet for the microcontroller on the board.

When the sensor is connected to a pin INPUT_PULLUP the connector, the other end should be grounded. /b10>In the case of a simple switch, this causes the pin to turn high when the switch is on and low when the switch is pressed. /b11> The pull-up resistor provides enough current to light up the LED connected to the pin configured as an input. /b12> If the LED in the project seems to be working but very dim, this may be what happened.

The same register (internal chip memory unit) that controls whether the pin is high or low controls the pull-up resistance. /b10>Therefore, when the pin is in INPUT mode, the pin configured to have pull-up resistance on is turned on, and if the pin is switched to OUTPUT mode via pinMode(), the pin is configured to be high. /b11>This also applies in the other direction, where if you switch to the input via pinMode(), the output pin in a high state sets the pull-up resistor.

Example

pinMode(3,INPUT) ; // set pin to input without using built in pull up resistor
pinMode(5,INPUT_PULLUP) ; // set pin to input using built in pull up resistor

The pin is configured as OUTPUT

Pins configured as OUTPUT by pinMode() are considered to be in a low impedance state. /b10> This means that they can provide a large amount of current to other circuits. /b11>The Atmega pin can provide (provide positive current) or absorb (provide negative current) up to 40mA (mAh) of current to other devices/circuits. /b12>This is enough to light up the LED or run many sensors (don't forget the series resistance), but not enough to run relays, son tubes or motors.

Attempting to run a high-current device from the output pin can damage or damage the output transistor in the pin or damage the entire Atmega chip. /b10> Typically, this results in a "dead" pin in the microcontroller, but the remaining chips still work. /b11>Therefore, it is best to connect the OUTPUT pin to other devices with a 470-ohm or 1k resistor, unless a particular application requires the maximum current to be drawn from the pin.

PinMode() function

The pinMode() function is used to configure a specific pin as input or output. /b10> The internal INPUT_PULLUP can be activated using the same mode. /b11> In addition, input mode explicitly prohibits internal pull-up.

PinMode() function syntax

Void setup () {
   pinMode (pin , mode);
}
  • pin - You want to set the number of the pin for the mode

  • mode - INPUT, OUTPUT or INPUT_PULLUP.

Example

int button = 5 ; // button connected to pin 5
int LED = 6; // LED connected to pin 6

void setup () {
   pinMode(button , INPUT_PULLUP); 
   // set the digital pin as input with pull-up resistor
   pinMode(button , OUTPUT); // set the digital pin as output
}

void setup () {
   If (digitalRead(button ) == LOW) // if button pressed {
      digitalWrite(LED,HIGH); // turn on led
      delay(500); // delay for 500 ms
      digitalWrite(LED,LOW); // turn off led
      delay(500); // delay for 500 ms
   }
}

DigitalWrite() function

The digitalWrite() function is used to write HIGH or LOW values to the digital pin. /b10> If the pin is configured as OUTPUT via pinMode(), its voltage is set to the appropriate value: HIGH is 5V (or 3.3V on a 3.3V board) and LOW is 0V (ground). /b12> If the pin is configured as INPUT, digitalWrite() enables (HIGH) or disables (LOW) internal pull-up of the input pin. /b13> It is recommended that pinMode() be INPUT_PULLUP to enable the internal pull-up resistor.

If pinMode() is not set to OUTPUT and the LED is connected to the pin, the LED may dim when digitalWrite (HIGH) is called. W hen pinMode() is not explicitly set, digitalWrite() activates the internal pull-up resistor, which is like a large flow-limiting resistor.

DigitalWrite() function syntax

Void loop() {
   digitalWrite (pin ,value);
}
  • pin - You want to set the number of the pin for the mode

  • value - HIGH or LOW.

Example

int LED = 6; // LED connected to pin 6

void setup () {
   pinMode(LED, OUTPUT); // set the digital pin as output
}

void setup() { 
   digitalWrite(LED,HIGH); // turn on led
   delay(500); // delay for 500 ms
   digitalWrite(LED,LOW); // turn off led
   delay(500); // delay for 500 ms
}

AnalogRead() function

Arduino is able to detect if a voltage is applied to its pin and report it through the digitalRead() function. /b10> There is a difference between the on/off sensor (detecting the presence of an object) and the analog sensor, which continuously changes the value of the analog sensor. /b11> In order to read this type of sensor, we need a different type of pin.

In the lower right corner of the Arduino board, you'll see six pins marked "Analog In." /b10> These special pins not only tell them if voltage is applied to them, but also the values of them. /b11> By using the analogRead() function, we can read the voltage applied to one of the pins.

This function returns a number between 0 and 1023, representing a voltage between 0 and 5 volts. /b10> For example, if the voltage applied to the pin number 0 is 2.5V, the analogRead(0) returns 512.

AnalogRead() function syntax

analogRead(pin);
  • pin - Number of analog input pins to read (0 to 5 on most boards, 0 to 7 on Mini and Nano, 0 to 15 on Mega)

Example

int analogPin = 3;//potentiometer wiper (middle terminal) 
   // connected to analog pin 3 
int val = 0; // variable to store the value read

void setup() {
   Serial.begin(9600); // setup serial
} 

void loop() {
   val = analogRead(analogPin); // read the input pin
   Serial.println(val); // debug value
}