Engineering · Raspberry Pi · Project 3 of 47 · Outputs · Beginner
Traffic light with a walk button
Three LEDs run a traffic cycle. The light waits on green until a pedestrian presses the walk button.

Parts in this circuit
| Part | Type |
|---|---|
| Red | LED |
| Yellow | LED |
| Green | LED |
| Walk button | Push button |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Red - anode + | Pin 15 (GPIO22) |
| 2 | Black | Red - cathode - | Pin 6 (GND) |
| 3 | Coloured | Yellow - anode + | Pin 16 (GPIO23) |
| 4 | Black | Yellow - cathode - | Pin 9 (GND) |
| 5 | Coloured | Green - anode + | Pin 18 (GPIO24) |
| 6 | Black | Green - cathode - | Pin 14 (GND) |
| 7 | Coloured | Walk button - Signal | Pin 13 (GPIO27) |
| 8 | Black | Walk button - GND | Pin 20 (GND) |
Python code (gpiozero)
from gpiozero import LED, Button
from time import sleep
red = LED(22)
yellow = LED(23)
green = LED(24)
walk = Button(27)
def show(colour, seconds):
for led in (red, yellow, green):
led.off() # all off first
colour.on()
sleep(seconds)
while True:
show(green, 1)
print("Green. Press the walk button to cross.")
walk.wait_for_press(timeout=10) # stay green until pressed (max 10 s)
show(yellow, 2)
show(red, 5)
print("Walk now!")
The same program with RPi.GPIO
import RPi.GPIO as GPIO
import time
RED, YELLOW, GREEN, WALK = 22, 23, 24, 27
GPIO.setmode(GPIO.BCM)
GPIO.setup([RED, YELLOW, GREEN], GPIO.OUT)
GPIO.setup(WALK, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def show(pin, seconds):
GPIO.output([RED, YELLOW, GREEN], GPIO.LOW)
GPIO.output(pin, GPIO.HIGH)
time.sleep(seconds)
try:
while True:
show(GREEN, 1)
print("Green. Press the walk button to cross.")
GPIO.wait_for_edge(WALK, GPIO.FALLING, timeout=10000)
show(YELLOW, 2)
show(RED, 5)
print("Walk now!")
finally:
GPIO.cleanup()
How the code works
- show(colour, seconds) turns every LED off, then switches on one colour and waits.
- walk.wait_for_press(timeout=10) pauses the program until the button is pressed or 10 seconds pass.
- The order green, yellow, red is a small state machine, the same idea as a PLC sequence.
You will learn
- Writing your own function
- Timing and sequences
- Waiting for an input with a timeout
Try this
- Add a flashing yellow before red.
- Make the red time longer if the button was pressed twice.
- Add a buzzer on GPIO18 that beeps during the walk time.
On a real Pi
- Use one resistor per LED. Never share a resistor between LEDs.
- A real crossing also needs a fail-safe: if the Pi crashes, the lights must default to a safe state.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 5 mm LED, green | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | 5 mm LED, yellow | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 330 ohm resistor, 1/4 W One per LED | 3 | Robu.in Amazon.in ThinkRobotics |
| 8 | Tactile push button (12 mm, with cap) | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.