GPIO Zero is the simplest way to control a Raspberry Pi's GPIO pins from Python. It's installed by default on Raspberry Pi OS, works on every model including the Pi 5, and turns jobs like "light the LED when the button is pressed" into a couple of lines. Every example here was tested against GPIO Zero's built-in mock pins, with simulated button presses.
What you need
- A Raspberry Pi with Raspberry Pi OS
- An LED and a 330 Ω resistor
- A push button
- A breadboard and female-to-male jumper wires
Wiring
| Part | Connect |
|---|---|
| LED | GPIO 17 (physical pin 11) → 330 Ω resistor → LED long leg (anode). LED short leg (cathode) → GND (physical pin 9) |
| Button | One side → GPIO 27 (physical pin 13). Other side → GND (physical pin 14) |
The Pi's GPIO pins work at 3.3 V and aren't 5 V tolerant, so never connect them to 5 V. The resistor limits the LED current to a few milliamps; see the LED resistor calculator. The button needs no resistor, because GPIO Zero turns on the pin's internal pull-up: the pin reads high normally and low when the button connects it to ground. The GPIO pinout guide shows where every pin is.
Pin numbers
GPIO Zero uses the chip's GPIO (BCM) numbers, not the physical pin positions. LED(17) means GPIO 17, which is physical pin 11. Getting this wrong is the most common reason nothing happens.
Light the LED while the button is held
from gpiozero import LED, Button
from signal import pause
led = LED(17) # GPIO 17, physical pin 11
button = Button(27) # GPIO 27, physical pin 13
button.when_pressed = led.on
button.when_released = led.off
pause() # keep the program running
Save it as button_led.py and run python3 button_led.py. Press Ctrl+C to stop.
Notice there are no brackets after led.on. You're handing GPIO Zero the function to call later, when the button is pressed. Writing led.on() would switch the LED on once, straight away, and give when_pressed nothing useful to call.
pause() keeps the program alive. Without it, the script reaches the end and exits, and the LED never responds.
Toggle on each press
from gpiozero import LED, Button
from signal import pause
led = LED(17)
button = Button(27)
button.when_pressed = led.toggle
pause()
Blink
from gpiozero import LED
from signal import pause
led = LED(17)
led.blink(on_time=0.5, off_time=0.5) # runs in the background
pause()
blink() runs in a background thread, so your program can carry on doing other things while the LED flashes.
Wait for presses in a loop
from gpiozero import LED, Button
led = LED(17)
button = Button(27)
while True:
button.wait_for_press()
print("Pressed")
led.on()
button.wait_for_release()
print("Released")
led.off()
This style is useful when the program should do things in order, like a reaction timer. Our mock test pressed the button twice and the program printed:
Pressed
Released
Pressed
Released
If it doesn't work
- Nothing happens: check you used the GPIO number, not the physical pin number, and that the LED's long leg goes towards the GPIO pin.
- "GPIO busy" or "pin already in use": another program, or an earlier run of yours still in the background, is using that pin. Stop it and try again.
- Installing in a virtual environment: the GPIO Zero documentation suggests making the environment with
python3 -m venv --system-site-packages, so it can use the copy of GPIO Zero and its pin library that Raspberry Pi OS already has. See Python virtual environments.
This guide is for full Raspberry Pi computers. The Raspberry Pi Pico is a microcontroller and uses MicroPython's machine module instead; see the Pico night light project.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.
- GPIO Zero documentation: installing (default on Raspberry Pi OS; virtual environments)
- GPIO Zero documentation: basic recipes (LED, button, pause)
- GPIO Zero documentation: API, output devices (LED, blink)
- GPIO Zero documentation: API, input devices (Button, pull-up)
- Raspberry Pi documentation: GPIO and the 40-pin header