Pantech.AI

How to configure digital GPIO in Arduino UNO

Introduction to GPIO

General-Purpose Input/Output (GPIO) pins are digital pins on an integrated circuit (IC) that can be configured as either input or output to interface with various devices.

  • Input: If you need to read data, such as from a switch or sensor, the pin is configured as an input.
  • Output: If you need to control devices like LEDs, motors, or displays, the pin is configured as an output.

Arduino Uno GPIO Pin Diagram

The Arduino Uno board features multiple digital I/O pins that can be used to connect to input or output devices. The following image shows the digital I/O pin layout of the Arduino Uno board.

Arduino Uno GPIO Pin Diagram

Digital Input and Output in Arduino (ATmega)

Arduino’s analog pins can also function as digital input/output pins. Let’s explore how digital input and output work on Arduino (ATmega).

Digital Output

Arduino’s digital pins can be configured as outputs to drive devices like LEDs and LCDs. To configure a pin as an output, we use the pinMode() function, which sets the pin’s direction as either input or output.

pinMode(pin, mode)

This function configures the specified GPIO pin as either input or output.

  • pin: The pin number you want to configure.
  • mode: Can be INPUT, OUTPUT, or INPUT_PULLUP.

Example:

pinMode(3, OUTPUT);  // Set pin 3 as an output

Arduino (ATmega) pins can source or sink a current of up to 40 mA, which is enough to drive devices like LEDs or an LCD display. However, this is insufficient for power-hungry devices like motors or relays.

Note: When connecting devices to Arduino output pins, always use a current-limiting resistor. Drawing more than 40 mA from an Arduino pin could damage the pin or the IC.

The output from these pins can be either HIGH (5V or 3.3V) or LOW (0V). To set the output value on these pins, we use the digitalWrite() function.

digitalWrite(pin, value)

This function sets the output value of the specified pin to either HIGH (5V) or LOW (0V).

Example:

digitalWrite(3, HIGH);  // Set pin 3 to HIGH (5V)

Digital Input

To read data from a sensor or another device, we configure a digital pin as an input. By default, Arduino pins are set as digital inputs, so explicit configuration isn’t necessary unless the pin has been previously set to an output.

If you want to explicitly set a pin as an input, use the pinMode() function.

digitalRead(pin)

This function reads the value of the specified GPIO pin, returning either HIGH or LOW depending on the input signal.

Digital Input with Pull-up Resistor

When a pin is configured as an input with nothing connected to it, it can enter a high-impedance or floating state, which may cause random changes in the pin’s state. To avoid this, you can add a pull-up or pull-down resistor to stabilize the input.

  • Pull-up resistor: Connects the pin to +5V, ensuring the input state is defined when no external device is connected.
  • Pull-down resistor: Connects the pin to GND, ensuring the input state is defined.

The INPUT_PULLUP mode in the pinMode() function enables the internal pull-up resistor, which connects the pin to 5V. This is a common method to prevent floating input states.

Example:

pinMode(3, INPUT_PULLUP);  // Set pin 3 as an input with an internal pull-up resistor

This setup avoids the floating state and ensures reliable input readings.

Pull-up resistor

Arduino (ATmega) has built-in configurable pull-up resistors, which can be activated using the pinMode() function with the mode set to INPUT_PULLUP. These resistors help stabilize the input pin when no external device is connected, preventing it from floating and producing random values.

For example:

pinMode(3, INPUT_PULLUP);  // Set pin 3 as input with an internal pull-up resistor

When connecting a device or sensor to a pin configured as an input with a pull-up, the other end of the device should be connected to ground (GND).

Alternatively, you can enable the internal pull-up resistor in a different way:

  1. Set the pin direction as input using pinMode().
  2. Then, write a HIGH value to the pin using digitalWrite() to activate the pull-up resistor.

Example:

pinMode(3, INPUT);        // Set pin 3 as input
digitalWrite(3, HIGH);    // Write HIGH to enable the pull-up resistor

This approach also enables the pull-up resistor on the specified input pin.

Blink LED Using Arduino

To write a program for LED blinking using Arduino Uno, we will use the on-board LED connected to digital pin 13. The LED will blink by turning ON and OFF at regular intervals.

Here is the code to achieve this

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

void loop()
{
  digitalWrite(13, HIGH);       // sets the digital pin 13 on
  delay(1000);                  // waits for a second
  digitalWrite(13, LOW);        // sets the digital pin 13 off
  delay(1000);                  // waits for a second
}

Output of LED Blinking using Arduino Uno

Sequentially Turning ON-OFF LEDs Using Arduino

In this program, we will sequentially turn on and off four LEDs connected to digital pins 0 to 3 of the Arduino Uno. First, the LEDs will turn on one by one. After all four LEDs are on, they will turn off in reverse order, one by one.

Note: Before uploading the program to the Arduino, ensure that the LEDs are not connected to pins 0 and 1 during the upload process, as these pins are used for serial communication (RXD and TXD). After the upload is complete, you can reconnect the LEDs to pins 0 and 1.

void setup() {

//set gpio pin {0,1,2,3} as output
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
}

void loop() {
  //turn LED ON one by one
  for(int i=0;i<4;i++){
    digitalWrite(i, HIGH);
    delay(1000);
  }
  
  //turn LED OFF one by one
  for(int i=3;i>=0;i--){
    digitalWrite(i, LOW);
    delay(1000);
  }
} 

Control LED with Switch using Arduino

Controlling LED ON-OFF Using a Switch Interfaced to Arduino Uno

LED and Switch connection diagram with Arduino Uno 

Control LED with a Switch using Arduino Code

int pushButton = 2;
int LED = 13;

void setup() {
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT_PULLUP);  //configure pin as input with pullup enabled   
  pinMode(LED, OUTPUT);               //configure pin as output
}

void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
 
  digitalWrite(LED, (!(buttonState))); //turn len on when switch pressed
  
  delay(1);        // delay in between reads for stability
}
 

For more implementations

https://pantech.ai/wp-admin/post.php?post=23058&action=edit

https://pantech.ai/wp-admin/post.php?post=23064&action=edit

Leave a Comment

Your email address will not be published. Required fields are marked *