como-usar-gpio-raspberry-pi

What are GPIO and how to use them on Raspberry Pi

  • 5 min

The GPIO are general-purpose input/output pins that allow the Raspberry Pi to interact with external devices.

These pins can be configured as inputs (to read data) or as outputs (to send signals).

GPIOs allow the Raspberry Pi to interact with the physical world, whether to read data from sensors or to control external devices like LEDs, motors, or relays.

Main features of GPIO

  • Voltage: 3.3V (never exceed this voltage).
  • Maximum current per pin: 16mA.
  • Maximum total current for all pins: 50mA.
  • Available pins: It depends on the Raspberry Pi model, but most have 40 GPIO pins.

GPIOs on Raspberry Pi are more “delicate” than those on an Arduino or an ESP32. Handle them carefully, or you might turn your Raspberry Pi into a paperweight.

GPIOs operate at 3.3V. Do not exceed this voltage, or you will damage the Raspberry Pi.

Digital output with GPIO

To use GPIOs on the Raspberry Pi, it is necessary to configure the pins as inputs or outputs. This can be done using programming languages like Python (which is the most commonly used for learning).

To use them, we have the RPi.GPIO library, which is one of the most popular for interacting with GPIOs in Python. We can install it by running the following command in your Raspberry Pi terminal:

sudo apt-get update
sudo apt-get install python3-rpi.gpio

Now, we can configure a GPIO pin as an output and turn on an LED:

import RPi.GPIO as GPIO
import time

# Set the pin mode
GPIO.setmode(GPIO.BCM)  # Use BCM numbering

# Define the GPIO pin as output
LED_PIN = 18
GPIO.setup(LED_PIN, GPIO.OUT)

try:
    while True:
        # Turn on the LED
        GPIO.output(LED_PIN, GPIO.HIGH)
        time.sleep(1)  # Wait 1 second

        # Turn off the LED
        GPIO.output(LED_PIN, GPIO.LOW)
        time.sleep(1)  # Wait 1 second
except KeyboardInterrupt:
    # Clean up GPIO pins on exit
    GPIO.cleanup()

In this example, the LED connected to GPIO pin 18 turns on and off every second.

Digital input with GPIO

Besides controlling output devices, GPIOs can also be used to read data from input devices (such as sensors or signals provided by other devices).

For example, this is how we could configure a GPIO pin as an input and read a voltage level provided by another device:

import RPi.GPIO as GPIO
import time

# Set the pin mode
GPIO.setmode(GPIO.BCM)

# Define the GPIO pin as input
INPUT_PIN = 17
GPIO.setup(INPUT_PIN, GPIO.IN)

try:
    while True:
        # Read the voltage level on the pin
        input_state = GPIO.input(INPUT_PIN)
        if input_state == GPIO.HIGH:
            print("HIGH voltage level detected")
        else:
            print("LOW voltage level detected")
        time.sleep(0.1)  # Wait 100ms to avoid rapid readings
except KeyboardInterrupt:
    # Clean up GPIO pins on exit
    GPIO.cleanup()

In this example,

  • The program reads the voltage level on GPIO pin 17, which could be connected to another device providing a 3.3V signal (HIGH) or 0V (LOW).
  • Depending on the voltage level, a message is printed to the console.

Using pull-up and pull-down resistors

When using GPIO inputs, it is common to use pull-up or pull-down resistors to ensure that the pin has a defined state when it is not being triggered by an external device.

The Raspberry Pi has internal pull-up and pull-down resistors (ranging from 50k-60k) that can be activated via software.

If you want to use a pull-down resistor, you can do it as follows:

GPIO.setup(INPUT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)  # Pull-up resistor
# or
GPIO.setup(INPUT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  # Pull-down resistor

In the next entry, we will see how to read a button using a GPIO.

Practical examples