Electronics School · Build It Yourself · Project 6 · Stage 4 Challenge

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).

Before you begin: ask your parent, guardian or a responsible grown-up for permission. Some activities need a grown-up to stay and help. Not sure? Ask first!

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

Power5 V from USB; the Pico’s pins use 3.3 V
LED currentabout 6 mA each (red and yellow); green may be less
SupervisionAn adult nearby for the setup
About power: the Pico runs from a USB cable plugged into a computer, a USB battery pack, or a store-bought USB phone charger (a finished, certified product that does the mains conversion safely inside its case). USB supplies a safe 5 V, and the Pico’s pins use just 3.3 V, so this fits our low-voltage rule. Never connect a Pico pin to more than 3.3 V, never connect the 3V3 or VBUS pins straight to GND, and always put a resistor in series with an LED on a pin.

Build it

Finding the pins: the GP names are printed on the underside of the Pico, so you can’t see them once it’s in the breadboard. Keep a printed copy of Raspberry Pi’s Pico pinout diagram beside you. With the board face up and its USB connector pointing away from you, pin 1 is the top-left pin; pins 1 to 20 run down the left side, and pins 21 to 40 run back up the right side. Press the Pico in so pin 1 sits in row 1: then each left-side pin’s number matches its row number.
  1. 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).
  2. Ground: jumper from row 18, column a (GND, pin 18) to the left blue (−) rail.
  3. 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
  1. 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 seconds

Click 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.

Challenges: make the yellow light flash three times before red. Add a pedestrian button on GP16 that shortens the green light. (Hint: the program can’t notice the button during 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.

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.

Was this page helpful?
Get all four of our sites in one free email every Friday: Prairie Signals
⇧Top
Hello! “Love is blind; marriage is the eye-opener.”