What Is GPIO and Why Does It Matter?
The General Purpose Input/Output (GPIO) pins on a Raspberry Pi are your gateway to the physical world. They let your code interact with LEDs, buttons, sensors, motors, and almost anything electronic. If you've ever wanted your software to flip a switch or read a temperature sensor, GPIO is where it starts.
This tutorial walks through the fundamentals using Python and the popular RPi.GPIO library — no prior electronics experience required, just basic Python knowledge.
What You'll Need
- Raspberry Pi (any model with GPIO header — 3B+, 4, Zero W all work)
- Breadboard and a few jumper wires
- 1× LED and 1× 330Ω resistor
- 1× tactile push button
- Python 3 installed (comes pre-installed on Raspberry Pi OS)
Setting Up the Library
Raspberry Pi OS ships with RPi.GPIO already installed. If you're on a minimal image, install it with:
sudo apt update
sudo apt install python3-rpi.gpio
Alternatively, many projects are migrating to gpiozero, which provides a higher-level, more readable interface. Both are worth knowing.
Blinking an LED: Your First GPIO Script
Wire your LED to GPIO pin 17 (physical pin 11) through a 330Ω resistor to ground. Then create a file called blink.py:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # Use Broadcom pin numbering
GPIO.setup(17, GPIO.OUT) # Set pin 17 as output
try:
while True:
GPIO.output(17, GPIO.HIGH) # LED on
time.sleep(0.5)
GPIO.output(17, GPIO.LOW) # LED off
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup() # Always clean up!
Run it with python3 blink.py and hit Ctrl+C to stop. The GPIO.cleanup() call in the finally block resets all pins — always include this to avoid leaving pins in an unknown state.
Reading a Button Input
Wire a push button between GPIO pin 27 and ground. Enable the internal pull-up resistor in code so the pin reads HIGH when idle:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
while True:
if GPIO.input(27) == GPIO.LOW: # Button pressed pulls pin LOW
print("Button pressed!")
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
BCM vs. BOARD Numbering
This trips up nearly every beginner. There are two ways to refer to pins:
- BCM (Broadcom) — refers to the chip's GPIO number (e.g., GPIO 17). This is what most tutorials and pinout diagrams use.
- BOARD — refers to the physical pin position on the header (e.g., pin 11). Less portable across Pi models.
Pick one and stick with it within a project. GPIO.setmode(GPIO.BCM) or GPIO.setmode(GPIO.BOARD) — set it once at the top of your script.
Next Steps
Once you're comfortable with basic input and output, explore these natural progressions:
- PWM (Pulse Width Modulation) — dim LEDs or control servo motors
- I2C and SPI — communicate with sensors and displays
- gpiozero — cleaner, event-driven GPIO programming
- Interrupts — respond to pin changes without busy-waiting in a loop
GPIO programming is one of those things that clicks fast once you see the LED blink. From there, the jump to real projects — weather stations, home automation nodes, custom keyboards — is smaller than you think.