Build it step by step
0 of 7 donePlace the LEDs
Put five LEDs on the breadboard, short legs towards the ground rail.
Add resistors and wires
A 220 Ω from each long leg to its GPIO wire as in the table. Connect the ground rail to a Pi ground pin.
Add the button and buzzer
Button between GPIO 17 and ground, buzzer between GPIO 18 and ground.
Test each LED
In Python, run LED(25).on() and so on to check them one at a time.
Run the program
Cars show green and pedestrians red until the button is pressed.
Press the button
Cars go yellow then red, the buzzer beeps, pedestrian green shows, then cars go.
Extend
Add a second button on the other side or a wait time before the change.
How do you build a traffic light with gpiozero?
A pedestrian crossing with LEDs, a button and a buzzer, all in Python. You need Raspberry Pi 4 Model B (4 GB), LED 5 mm red × 2, LED 5 mm yellow, LED 5 mm green × 2, Resistor 220 Ω × 5, Tactile push button 6 mm, Active buzzer 5 V, Breadboard 830 Tie Points and Jumper wires female to female (40 pcs). It takes 7 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 9
Parts list
- Single Board ComputersBuy
- SemiconductorsBuyLED 5 mm red × 2
- SemiconductorsBuyLED 5 mm yellow × 1
- SemiconductorsBuyLED 5 mm green × 2
- PassivesBuyResistor 220 Ω × 5
- SwitchesBuy
- Motors & ActuatorsBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Car red LED | GPIO 25 through 220 Ω to GND | Pin 22 |
| Car yellow LED | GPIO 24 through 220 Ω to GND | Pin 18 |
| Car green LED | GPIO 23 through 220 Ω to GND | Pin 16 |
| Pedestrian red LED | GPIO 22 through 220 Ω to GND | Pin 15 |
| Pedestrian green LED | GPIO 27 through 220 Ω to GND | Pin 13 |
| Button | GPIO 17 and GND | Pin 11 and a ground pin (internal pull-up) |
| Buzzer + | GPIO 18 | Pin 12, active buzzer |
| Buzzer - | GND | Any ground pin |
Code
Raspberry Pi OS (Bookworm or newer) with Python 3. gpiozero is installed by default. Save as traffic.py and run with python3 traffic.py. Pins use BCM (GPIO) numbers. Each LED needs a 220 Ω resistor, and the pin numbers in the wiring table are the physical header positions.
from gpiozero import LED, Button, Buzzer
from time import sleep
car_red, car_yellow, car_green = LED(25), LED(24), LED(23)
ped_red, ped_green = LED(22), LED(27)
button = Button(17)
buzzer = Buzzer(18)
def cars_go():
car_red.off(); car_yellow.off(); car_green.on()
ped_green.off(); ped_red.on()
def crossing():
car_green.off(); car_yellow.on()
sleep(2)
car_yellow.off(); car_red.on()
sleep(1)
ped_red.off(); ped_green.on()
for _ in range(8): # beep for the green man
buzzer.on(); sleep(0.2); buzzer.off(); sleep(0.6)
for _ in range(5): # flash before the cars go
ped_green.toggle(); sleep(0.3)
ped_green.off(); ped_red.on()
sleep(1)
cars_go()
cars_go()
while True:
button.wait_for_press()
crossing()
button.wait_for_release()Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Idle | Car green and pedestrian red |
| Press the button | Yellow for 2 s then red |
| Crossing time | Pedestrian green with beeps, then flashing |
| After the crossing | Back to car green |
If it does not work
LED does not light
Wrong way round or wrong pin. Check GPIO numbers against the header.
Button does nothing
gpiozero Button uses a pull-up, so wire it between the GPIO and ground.
Buzzer silent
A passive buzzer needs a tone. Use an active buzzer or PWM.
Program says GPIO busy
Another script is using the pins. Stop it first.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the traffic light with gpiozero?
Raspberry Pi 4 Model B (4 GB), LED 5 mm red × 2, LED 5 mm yellow, LED 5 mm green × 2, Resistor 220 Ω × 5, Tactile push button 6 mm, Active buzzer 5 V, Breadboard 830 Tie Points and Jumper wires female to female (40 pcs).
How long does the traffic light with gpiozero take to build?
About 60 minutes for a beginner maker, in 7 steps.
How do I know the traffic light with gpiozero works?
Idle: Car green and pedestrian red. Press the button: Yellow for 2 s then red. Crossing time: Pedestrian green with beeps, then flashing.
LED does not light: what should I check?
Wrong way round or wrong pin. Check GPIO numbers against the header.
Button does nothing: what should I check?
gpiozero Button uses a pull-up, so wire it between the GPIO and ground.
Buzzer silent: what should I check?
A passive buzzer needs a tone. Use an active buzzer or PWM.
Program says GPIO busy: what should I check?
Another script is using the pins. Stop it first.
