Try Embedded

MicroPython pedestrian crossing

A traffic light on a Raspberry Pi Pico in MicroPython: red, amber and green LEDs, and a button that asks to cross.

Output · Beginner

In a real product: Pelican crossing controller (Rail and transport): A crossing holds traffic on green until a pedestrian presses the button, then runs amber and red.

Three LEDs on GP16, GP17 and GP18 are a traffic light, and a push button on GP22 is the crossing's request button. The light stays green until someone presses the button; then it goes amber, red for the walkers, and back to green. It is written in MicroPython: main.py runs on the Pico as it is, with nothing to build.

What you'll learn

  • Driving outputs with machine.Pin(n, Pin.OUT) and reading a button with Pin.PULL_UP
  • Catching a press with a pin interrupt (irq), so no press is missed while the program sleeps
  • Timing the phases with time.sleep and time.ticks_ms

How it works

Each LED's anode is driven by a GPIO through a 220 Ω resistor, its cathode to GND. Pin(16, Pin.OUT) makes GP16 an output, and .value(1) puts 3.3 V on it.

The button joins GP22 to GND. Pin.PULL_UP turns on the RP2040's internal pull-up, so the pin reads 1 until the button is pressed and pulls it to 0. button.irq(trigger=Pin.IRQ_FALLING, handler=pressed) calls pressed on that falling edge, even in the middle of time.sleep: the handler only sets a flag, and the main loop acts on it.

time.ticks_ms() counts milliseconds; time.ticks_diff subtracts two of them correctly when the counter wraps. The program uses it to give the traffic at least five seconds of green between two crossings.

The Serial Monitor shows each phase. When you stop the program with Ctrl-C there, it shows MicroPython's >>> prompt: type red.value(1) and press Enter to light an LED by hand.

Try this

  • Blink the amber LED during the last two seconds of the walkers' phase, as a pelican crossing does.
  • Add a buzzer on another pin that beeps while the walkers may cross.
  • Replace the flag with a machine.Timer that switches the phases, so the main loop is free for other work.

main.py

# A pedestrian crossing: green for the traffic until the button asks to cross.
from machine import Pin
import time

red = Pin(16, Pin.OUT)
amber = Pin(17, Pin.OUT)
green = Pin(18, Pin.OUT)
button = Pin(22, Pin.IN, Pin.PULL_UP)

MIN_GREEN_MS = 5000
requested = False

def pressed(pin):
    global requested
    requested = True

button.irq(trigger=Pin.IRQ_FALLING, handler=pressed)

def show(r, a, g, name):
    red.value(r)
    amber.value(a)
    green.value(g)
    print(name)

show(0, 0, 1, "green: traffic goes")
green_since = time.ticks_ms()
while True:
    if requested and time.ticks_diff(time.ticks_ms(), green_since) >= MIN_GREEN_MS:
        show(0, 1, 0, "amber: traffic slows")
        time.sleep(2)
        show(1, 0, 0, "red: walk")
        time.sleep(5)
        requested = False
        show(0, 0, 1, "green: traffic goes")
        green_since = time.ticks_ms()
    time.sleep_ms(20)

Lessons behind it