Raspberry Pi LED and button with gpiozero
Build it step by step
0 of 5 doneSet up the Pi
Flash Raspberry Pi OS with Raspberry Pi Imager and boot it. Shut it down before wiring.
Fit the cobbler
Ribbon from the Pi header to the T-Cobbler, red stripe at pin 1. Push the cobbler across the breadboard gap.
Wire the LED
GPIO17 through 330 Ω to the LED's long leg, short leg to GND.
Wire the button
GPIO2 to one side, GND to the other.
Run
Boot the Pi, save the script and run it. Press the button a few times.
How do you build a raspberry Pi LED and button with gpiozero?
Wire an LED and a button to a Raspberry Pi's GPIO header and control them from Python with the gpiozero library. You need Raspberry Pi 5, Raspberry Pi 27 W USB-C power supply, SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi GPIO T-cobbler with ribbon cable, LED 5 mm green, Resistor 330 Ω, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 5 steps and about 40 minutes.
- Level
- Beginner
- Time
- About 40 min
- Parts
- 9
Parts list
- Single Board ComputersBuyRaspberry Pi 5 × 1
- SBC AccessoriesBuy
- SBC AccessoriesBuy
- SBC AccessoriesBuy
- SemiconductorsBuyLED 5 mm green × 1
- PassivesBuyResistor 330 Ω × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| T-Cobbler ribbon | Pi 40-pin header (red stripe to pin 1) | GPIO on the breadboard |
| GPIO17 | 330 Ω, LED anode, LED cathode to GND | LED (3.3 V logic) |
| GPIO2 | Button, other side to GND | Button (gpiozero turns on the pull-up) |
Code
Raspberry Pi OS (Bookworm or newer) with Python 3. gpiozero is preinstalled. Save as button_led.py and run python3 button_led.py.
from gpiozero import LED, Button
from signal import pause
led = LED(17)
button = Button(2)
def pressed():
led.toggle()
print("LED is", "on" if led.is_lit else "off")
button.when_pressed = pressed
print("Press the button. Ctrl+C to quit.")
pause()Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Script started | "Press the button" printed |
| Button press | LED toggles and the state is printed |
| Voltage on GPIO17 when on | About 3.3 V |
If it does not work
Nothing happens
Check you used BCM numbers (GPIO17 is physical pin 11, GPIO2 is pin 3).
"GPIO busy" error
Another script still owns the pin. Stop it, or reboot.
Never connect 5 V to a GPIO pin
The Pi's GPIO is 3.3 V only. 5 V can destroy it.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the raspberry Pi LED and button with gpiozero?
Raspberry Pi 5, Raspberry Pi 27 W USB-C power supply, SanDisk microSD card 32 GB (A1, Class 10), Raspberry Pi GPIO T-cobbler with ribbon cable, LED 5 mm green, Resistor 330 Ω, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the raspberry Pi LED and button with gpiozero take to build?
About 40 minutes for a beginner maker, in 5 steps.
How do I know the raspberry Pi LED and button with gpiozero works?
Script started: "Press the button" printed. Button press: LED toggles and the state is printed. Voltage on GPIO17 when on: About 3.3 V.
Nothing happens: what should I check?
Check you used BCM numbers (GPIO17 is physical pin 11, GPIO2 is pin 3).
GPIO busy" error: what should I check?
Another script still owns the pin. Stop it, or reboot.
Never connect 5 V to a GPIO pin: what should I check?
The Pi's GPIO is 3.3 V only. 5 V can destroy it.
