Projects / Traffic light with gpiozero

Traffic light with gpiozero

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi 418.8 mA

    Place the LEDs

    Put five LEDs on the breadboard, short legs towards the ground rail.

  2. Pi 4

    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.

  3. Pi 4

    Add the button and buzzer

    Button between GPIO 17 and ground, buzzer between GPIO 18 and ground.

  4. Pi 418.9 mA

    Test each LED

    In Python, run LED(25).on() and so on to check them one at a time.

  5. Pi 4

    Run the program

    Cars show green and pedestrians red until the button is pressed.

  6. Pi 418.9 mA

    Press the button

    Cars go yellow then red, the buzzer beeps, pedestrian green shows, then cars go.

  7. Pi 4

    Extend

    Add a second button on the other side or a wait time before the change.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Car red LEDGPIO 25 through 220 Ω to GNDPin 22
Car yellow LEDGPIO 24 through 220 Ω to GNDPin 18
Car green LEDGPIO 23 through 220 Ω to GNDPin 16
Pedestrian red LEDGPIO 22 through 220 Ω to GNDPin 15
Pedestrian green LEDGPIO 27 through 220 Ω to GNDPin 13
ButtonGPIO 17 and GNDPin 11 and a ground pin (internal pull-up)
Buzzer +GPIO 18Pin 12, active buzzer
Buzzer -GNDAny 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.

CheckExpected
IdleCar green and pedestrian red
Press the buttonYellow for 2 s then red
Crossing timePedestrian green with beeps, then flashing
After the crossingBack 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 →

Ask Lark AI

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.