Traffic Light Controller
Program a Raspberry Pi Pico to run a model traffic light: green, yellow, red, repeat.
What you’ll make
A working model traffic light: green, then yellow, then red, then back to green, on a timer you program yourself. It’s the Stage 4 challenge, and a real state machine (Lesson 21).
What you need
- A Raspberry Pi Pico with its header pins already fitted (sold as the Pico H, or Pico WH), and a USB cable that carries data (some cables only charge)
- A computer with Thonny, and MicroPython installed on the Pico (Lesson 24)
- A breadboard and jumper wires
- 3 LEDs: red, yellow and green, 5 mm
- 3 resistors, 220 Ω (bands: red, red, brown; five-band: red, red, black, black, brown)
Safety numbers for this build
| Power | 5 V from USB; the Pico’s pins use 3.3 V |
|---|---|
| LED current | about 6 mA each (red and yellow); green may be less |
| Supervision | An adult nearby for the setup |
Build it
- Unplug the USB cable. Press the Pico into a full-size breadboard so it straddles the centre gap, with pin 1 in row 1. Each Pico pin row has two free holes on the outside (columns a and b on the left).
- Ground: jumper from row 18, column a (GND, pin 18) to the left blue (−) rail.
- Build each light in the empty rows below the Pico. Each light uses three rows: a jumper brings the signal from the Pico pin, the resistor carries it two rows down to the LED’s long leg, and the LED’s short leg goes in the next row, which is jumpered to the − rail.
| Red (GP15, pin 20) | jumper from row 20 col a to row 22 col a; resistor from row 22 col c to row 24 col c; LED long leg row 24 col e, short leg row 25 col e; jumper from row 25 col a to the − rail |
|---|---|
| Yellow (GP14, pin 19) | jumper from row 19 col a to row 27 col a; resistor row 27 col c to row 29 col c; LED long leg row 29 col e, short leg row 30 col e; jumper row 30 col a to − rail |
| Green (GP13, pin 17) | jumper from row 17 col a to row 32 col a; resistor row 32 col c to row 34 col c; LED long leg row 34 col e, short leg row 35 col e; jumper row 35 col a to − rail |
- Check every connection against the pinout diagram, then plug in the USB cable.
Program it
from machine import Pin
import time
red = Pin(15, Pin.OUT) # GP15
yellow = Pin(14, Pin.OUT) # GP14
green = Pin(13, Pin.OUT) # GP13
def all_off():
red.off()
yellow.off()
green.off()
while True:
all_off()
green.on()
time.sleep(5) # green for 5 seconds
all_off()
yellow.on()
time.sleep(2) # yellow for 2 seconds
all_off()
red.on()
time.sleep(5) # red for 5 secondsClick Run in Thonny. To make it run by itself whenever the Pico has power (from a USB battery pack, say), choose File, Save as, then Raspberry Pi Pico, and name it main.py.
time.sleep(5), so replace it with 500 short sleeps of 0.01 seconds and check the button in each one.) In Canada a flashing green means different things in different provinces: an advance left turn in some, a pedestrian-controlled light in British Columbia. Can you add one?It doesn’t work?
- Nothing lights, no error: check the LEDs’ orientation and that the − rail really connects to a GND pin.
- One colour never lights: check that its resistor is in the right GP pin row, using the pinout diagram.
- Green is very dim: some green LEDs need about 3 V, leaving little for the resistor at 3.3 V. Try a 100 Ω resistor for the green LED only (about 3 to 6 mA, still safe for the pin).
- Thonny can’t find the Pico: try another USB cable (charge-only cables are common), and check the interpreter setting.
For teachers and parents
Open the teacher guide
Time: 45 to 60 minutes, after Lesson 24’s setup.
Learners will: wire outputs to a microcontroller; write a program with a function, loop and delays; modify timing and sequence; describe the traffic light as a sequence of states.
Risk and supervision: low. USB power only. Check wiring before students plug in, and make sure no pin connects directly to the 5 V (VBUS) pin.
Discussion questions: Why does the program turn everything off before turning one light on? What would happen at a real intersection if two directions were green at once? How do real traffic lights know a car is waiting?
Common misconceptions: “the LED lights because the code is running” (it lights only if the circuit is complete as well); “GP15 means physical pin 15” (it’s pin 20); “a longer sleep makes the program faster.”
Worksheet: 1. Which line makes the yellow light last 2 seconds? 2. Change the program so red lasts 8 seconds. 3. List the states in order. 4. Why is all_off() called first?
Answers: 1. time.sleep(2) after yellow.on(). 2. Change the last time.sleep(5) to time.sleep(8). 3. Green, yellow, red, then back to green. 4. So only one light is ever on.
Extension: pairs build two lights for crossing streets and program them so they’re never green at the same time.
Learn more
- Raspberry Pi: Pico documentation: official getting-started guides
- Lesson 24: Introduction to programming
- Next build: Electronic Dice
Electronics School is part of the Discovery Zone. Every activity here runs on small batteries or a USB cable: never wall outlets. The one plug-in tool is the optional soldering iron, set up by an adult.