One of the best things about single-board computers like the Raspberry Pi is that they have General Purpose Input/Output (GPIO). GPIO pins let the device send and receive electrical signals, which lets it interact with the real world. This feature turns a basic computer into a platform for robotics, automation, home control systems, and embedded development.
Python is the most popular language for controlling GPIO on Raspberry Pi because it is easy to read, understand, and has a lot of library support. It lets people write short programs that control LEDs, read sensors, run motors, and make complicated interactive systems.
Anyone who works with embedded systems or physical computing needs to know how to control GPIO with Python. This guide goes over everything you need to know about GPIO, including concepts, libraries, programming methods, and real-world examples. It starts with the basics and moves on to more advanced uses.
Understanding GPIO Basics
You can set GPIO pins on the Raspberry Pi to be either inputs or outputs.
A signal is sent from an output pin, which usually controls a device like an LED or motor. A sensor or button usually sends a signal to an input pin.
Every GPIO pin works at a certain voltage level, which is usually 3.3V. It’s important to know about electrical limits because using the wrong voltage can damage the board.
There are two numbering systems used with GPIO:
- BCM (Broadcom): Uses the chip’s internal numbering
- BOARD: Uses physical pin numbering
Most modern tutorials use BCM because it aligns with software-level identification.
Setting Up Python for GPIO
Python is pre-installed on most Raspberry Pi systems. To use GPIO, you need a library such as RPi.GPIO or gpiozero.
To install RPi.GPIO:
sudo apt update
sudo apt install python3-rpi.gpio
For gpiozero:
sudo apt install python3-gpiozero
RPi.GPIO provides low-level control, while gpiozero simplifies common tasks.
First GPIO Output Example: Blinking an LED
This is the most common beginner project.
Hardware Setup
- Connect LED to GPIO pin 17
- Use a resistor to limit current
Code Example
import RPi.GPIO as GPIO
import time
LED_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
while True:
GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(1)
GPIO.output(LED_PIN, GPIO.LOW)
time.sleep(1)
This program turns the LED on and off every second.
Cleaning Up GPIO
Always clean up GPIO after use:
GPIO.cleanup()
This prevents conflicts and ensures pins are reset properly.
Input Example: Reading a Button
Hardware Setup
- Connect button to GPIO pin 18
- Use pull-up resistor
Code Example
import RPi.GPIO as GPIO
BUTTON_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
if GPIO.input(BUTTON_PIN) == GPIO.LOW:
print("Button pressed")
Using gpiozero for Simplicity
gpiozero simplifies GPIO programming.
LED Example
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Example: LED Controlled by Button
from gpiozero import LED, Button
led = LED(17)
button = Button(18)
button.when_pressed = led.on
button.when_released = led.off
while True:
pass
Pulse Width Modulation (PWM)
PWM allows control of brightness or motor speed.
Example
import RPi.GPIO as GPIO
import time
PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.OUT)
pwm = GPIO.PWM(PIN, 100)
pwm.start(0)
for duty in range(0, 101, 5):
pwm.ChangeDutyCycle(duty)
time.sleep(0.1)
pwm.stop()
GPIO.cleanup()
Example: Controlling a Servo Motor
import RPi.GPIO as GPIO
import time
servo_pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo_pin, GPIO.OUT)
pwm = GPIO.PWM(servo_pin, 50)
pwm.start(0)
pwm.ChangeDutyCycle(7)
time.sleep(1)
pwm.stop()
GPIO.cleanup()
Example: Motion Sensor
import RPi.GPIO as GPIO
sensor = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN)
while True:
if GPIO.input(sensor):
print("Motion detected")
Example: Temperature Sensor Simulation
import random
temp = random.randint(20, 35)
if temp > 30:
print("High temperature")
else:
print("Normal temperature")
Event Detection
Polling is inefficient. Event detection improves performance.
def callback(channel):
print("Button pressed")
GPIO.add_event_detect(18, GPIO.FALLING, callback=callback, bouncetime=200)
Working with Multiple GPIO Pins
pins = [17, 18, 27]
for pin in pins:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.HIGH)
Example: Traffic Light System
import RPi.GPIO as GPIO
import time
red = 17
yellow = 18
green = 27
GPIO.setmode(GPIO.BCM)
for pin in [red, yellow, green]:
GPIO.setup(pin, GPIO.OUT)
while True:
GPIO.output(red, True)
time.sleep(3)
GPIO.output(red, False)
GPIO.output(green, True)
time.sleep(3)
GPIO.output(green, False)
GPIO.output(yellow, True)
time.sleep(1)
GPIO.output(yellow, False)
Error Handling
try:
# GPIO code
pass
except KeyboardInterrupt:
GPIO.cleanup()
Real-World Applications
GPIO with Python is used in:
- Home automation
- Robotics
- IoT systems
- Security systems
- Industrial monitoring
Mistakes
- Incorrect pin numbering
- Missing resistors
- Not calling cleanup
- Using wrong voltage
Best Practices
- Use gpiozero for beginners
- Document pin usage
- Test circuits before coding
- Use proper resistors
Conclusion
One of the best and easiest ways to control hardware with a Raspberry Pi is to use Python to control GPIO. It gives beginners a way to quickly make real-world projects and a way to learn more about advanced embedded systems development.
You can make a lot of different kinds of programs, from simple LED circuits to complicated automation systems, if you learn about GPIO concepts, libraries, and programming. Python is easy to learn, and GPIO is flexible, which makes them both great places to start for anyone who wants to learn about physical computing.
