Python Raspberry Pi GPIO — Core Concepts
What GPIO Means in Practice
The Raspberry Pi’s 40-pin header is a direct connection between the Linux computer and external electronics. Unlike USB ports that follow strict protocols, GPIO pins give you raw electrical control. You decide what each pin does — output voltage, read voltage, or serve a specialized function like serial communication.
Not all 40 pins are GPIO. The header includes power pins (3.3V and 5V), ground pins, and reserved pins alongside the programmable GPIO pins. A Raspberry Pi 4 has 26 usable GPIO pins.
Pin Numbering: Two Systems
This trips up every beginner. There are two ways to refer to GPIO pins:
- BCM numbering — Uses the chip’s internal GPIO numbers. Pin labeled GPIO17 on diagrams is BCM 17. This is the recommended system.
- Board numbering — Uses the physical position on the header. The pin in physical position 11 on the header happens to be GPIO17. This numbering stays the same across Pi models.
When you read tutorials online, check which numbering system they use. Mixing them up means you control the wrong pin, which can damage components.
The gpiozero Library
The modern way to control GPIO in Python is gpiozero, developed by the Raspberry Pi Foundation. It provides high-level objects for common components:
from gpiozero import LED, Button
from signal import pause
led = LED(17) # GPIO 17
button = Button(2) # GPIO 2
button.when_pressed = led.on
button.when_released = led.off
pause() # keeps the program running
No pin numbering confusion — gpiozero always uses BCM numbers. No setup or cleanup boilerplate. The library handles pull resistors, debouncing, and resource cleanup automatically.
Common gpiozero Components
The library includes ready-made classes for many hardware scenarios:
- LED, PWMLED — On/off and brightness-controlled LEDs
- Button — Push buttons with debounce and hold detection
- MotionSensor — PIR motion detectors
- DistanceSensor — Ultrasonic distance measurement
- Servo — Hobby servo motor control
- Buzzer, TonalBuzzer — Active and passive buzzers
- TemperatureSensor — DS18B20 digital thermometers
- LightSensor — Light-dependent resistors
Input and Output Modes
Each GPIO pin operates in one of two basic modes:
Output mode — The pin drives voltage. HIGH means 3.3V, LOW means 0V. Use this to turn LEDs on and off, trigger relays, or send signals to other circuits.
Input mode — The pin reads voltage. If 3.3V is applied, it reads HIGH. If 0V (ground) is applied, it reads LOW. Use this for buttons, switches, and digital sensors.
Pull-Up and Pull-Down Resistors
When an input pin is not connected to anything, it “floats” — reading random values influenced by electrical noise. Pull resistors solve this:
- Pull-up — Connects the pin to 3.3V through a resistor. Pin reads HIGH by default, goes LOW when grounded (common for buttons).
- Pull-down — Connects the pin to ground through a resistor. Pin reads LOW by default, goes HIGH when voltage is applied.
The Raspberry Pi has built-in pull resistors you can enable in software. With gpiozero, buttons use pull-up by default.
PWM: Controlling Brightness and Speed
Pulse Width Modulation rapidly switches a pin between HIGH and LOW. By controlling how long the pin stays HIGH (the duty cycle), you can dim LEDs or control motor speed:
from gpiozero import PWMLED
import time
led = PWMLED(17)
# Fade in
for brightness in range(0, 100):
led.value = brightness / 100
time.sleep(0.02)
Software PWM works for LEDs and small servos. For applications needing precise timing (like driving multiple servos smoothly), hardware PWM on dedicated pins (GPIO12, 13, 18, 19) is more reliable.
Common Misconception
A common mistake is connecting 5V devices directly to GPIO pins. The Raspberry Pi’s GPIO operates at 3.3V. Applying 5V to a GPIO input pin can permanently damage the Pi. When interfacing with 5V devices, use a level shifter or voltage divider.
Safety Guidelines
- Never connect a GPIO output pin directly to ground — this creates a short circuit
- Always use current-limiting resistors with LEDs (220-330 ohm typical)
- Maximum current per GPIO pin is about 16 mA (safe limit), with an absolute maximum of 50 mA for the entire header
- Double-check wiring before powering on — there is no overcurrent protection
One thing to remember: GPIO gives you direct electrical control from Python code, but that power comes with responsibility — always respect voltage levels and current limits to protect your Pi.
See Also
- Python Behavior Trees Robotics How robots make decisions using a tree-shaped rulebook that keeps them organized, like a flowchart that tells a robot what to do in every situation.
- Python Bluetooth Ble How Python connects to fitness trackers, smart locks, and wireless sensors using the invisible radio signals all around you.
- Python Circuitpython Hardware Why CircuitPython makes wiring up LEDs, sensors, and motors as easy as plugging in a USB drive.
- Python Computer Vision Autonomous How self-driving cars use cameras and Python to see the road, spot pedestrians, read signs, and understand traffic — like giving a car human eyes and a brain.
- Python Home Assistant Automation How Python turns your home into a smart home that reacts to you automatically, like a helpful invisible butler.