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
Control of an LED with a button
This example shows how to control an LED using a button. The LED turns on when the button is pressed and turns off when released.
import RPi.GPIO as GPIO
import time
# Set the pin mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pins
LED_PIN = 18
BUTTON_PIN = 17
# Set up the pins
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
# Read the state of the button
button_state = GPIO.input(BUTTON_PIN)
if button_state == GPIO.LOW:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED
else:
GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED
time.sleep(0.1) # Wait 100ms to avoid bouncing
except KeyboardInterrupt:
# Clean up GPIO pins on exit
GPIO.cleanup()
Motion detector with PIR sensor
This example shows how to use a PIR (Passive Infrared) sensor to detect motion and turn on an LED when motion is detected.
import RPi.GPIO as GPIO
import time
# Set the pin mode
GPIO.setmode(GPIO.BCM)
# Define the GPIO pins
PIR_PIN = 17
LED_PIN = 18
# Set up the pins
GPIO.setup(PIR_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
try:
while True:
# Read the state of the PIR sensor
pir_state = GPIO.input(PIR_PIN)
if pir_state == GPIO.HIGH:
GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED
print("Motion detected")
else:
GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED
print("No motion")
time.sleep(0.1) # Wait 100ms to avoid false positives
except KeyboardInterrupt:
# Clean up GPIO pins on exit
GPIO.cleanup()
Control of a stepper motor
import RPi.GPIO as GPIO
import time
# Pin configuration
STEP_PIN = 17
DIR_PIN = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(STEP_PIN, GPIO.OUT)
GPIO.setup(DIR_PIN, GPIO.OUT)
# Motor direction
GPIO.output(DIR_PIN, GPIO.HIGH) # Clockwise direction
try:
while True:
GPIO.output(STEP_PIN, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(STEP_PIN, GPIO.LOW)
time.sleep(0.001)
except KeyboardInterrupt:
GPIO.cleanup()