Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Traffic light with a walk button: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
RedLED
YellowLED
GreenLED
Walk buttonPush 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.

#ColourFrom (part and leg)To (header pin)
1ColouredRed - anode +Pin 15 (GPIO22)
2BlackRed - cathode -Pin 6 (GND)
3ColouredYellow - anode +Pin 16 (GPIO23)
4BlackYellow - cathode -Pin 9 (GND)
5ColouredGreen - anode +Pin 18 (GPIO24)
6BlackGreen - cathode -Pin 14 (GND)
7ColouredWalk button - SignalPin 13 (GPIO27)
8BlackWalk button - GNDPin 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

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.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
55 mm LED, green1Robu.in Amazon.in ThinkRobotics
65 mm LED, yellow1Robu.in Amazon.in ThinkRobotics
7330 ohm resistor, 1/4 W
One per LED
3Robu.in Amazon.in ThinkRobotics
8Tactile push button (12 mm, with cap)1Robu.in Amazon.in ThinkRobotics
9Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
10Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 2. Button controls an LEDAll projects4. Door alarm with a buzzer →

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.