como-usar-gpio-raspberry-pi

What are GPIO and how to use them on Raspberry Pi

  • 5 min

GPIOs 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 Characteristics of GPIOs

  • Voltage: 3.3V (never exceed this voltage).
  • Maximum current per pin: 16mA.
  • Total maximum current for all pins: 50mA.
  • Available pins: 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 with care, or you might turn your Raspberry 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, 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()
Copied!

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

Digital Input with GPIO

In addition to controlling output devices, GPIOs can also be used to read data from input devices (like 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()
Copied!

In this example,

  • The program reads the voltage level on GPIO pin 17, which could be connected to another device providing a 3.3V (HIGH) or 0V (LOW) signal.
  • 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 the pin has a defined state when not being activated by an external device.

The Raspberry Pi has internal pull-up and pull-down resistors (between 50k-60k) that can be enabled 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
Copied!

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

Practical Examples