Built-in project · Raspberry Pi Pico W

Pico W traffic light with a countdown display

MicroPython on a Pico W: green, yellow and red LEDs on GP7, GP8 and GP9 change on a 30, 5 and 30 second cycle, counted down on four digits. The whole build, a Raspberry Pi Pico W and 12 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 30 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $8. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Using a Timer callback to count seconds
  • State machines with a list of times
  • Multiplexing a four-digit display
  • Shift register output with a 74HC595
  • Separating timing from drawing
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 15

How it works

Three LEDs on GP7, GP8 and GP9 act as a traffic light: green for 30 seconds, yellow for 5 and red for 30. A four-digit display shows how many seconds are left in the current color.

A Timer calls a function once a second to count down and move to the next color. The main loop does the visible work: it turns on the right LED and keeps drawing the number, because the display needs constant refreshing.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω (5) pin 2Raspberry Pi Pico W pin GP7
Resistor, 220 Ω (6) pin 2Raspberry Pi Pico W pin GP8
Resistor, 220 Ω (7) pin 2Raspberry Pi Pico W pin GP9
Resistor, 220 Ω (1) pin 2Raspberry Pi Pico W pin GP10
Resistor, 220 Ω (2) pin 2Raspberry Pi Pico W pin GP11
Resistor, 220 Ω (3) pin 2Raspberry Pi Pico W pin GP12
Resistor, 220 Ω (4) pin 2Raspberry Pi Pico W pin GP13
74HC595 shift register pin SHCPRaspberry Pi Pico W pin GP20
74HC595 shift register pin STCPRaspberry Pi Pico W pin GP19
74HC595 shift register pin DSRaspberry Pi Pico W pin GP18
Four-digit seven segment, multiplexed pin B74HC595 shift register pin Q1
Four-digit seven segment, multiplexed pin C74HC595 shift register pin Q2
Four-digit seven segment, multiplexed pin D74HC595 shift register pin Q3
Four-digit seven segment, multiplexed pin E74HC595 shift register pin Q4
Four-digit seven segment, multiplexed pin F74HC595 shift register pin Q5
Four-digit seven segment, multiplexed pin G74HC595 shift register pin Q6
Four-digit seven segment, multiplexed pin DP74HC595 shift register pin Q7
74HC595 shift register pin GNDGround
74HC595 shift register pin MR3.3 V
74HC595 shift register pin OEGround
Four-digit seven segment, multiplexed pin A74HC595 shift register pin Q0
74HC595 shift register pin VCC3.3 V
Resistor, 220 Ω (4) pin 1Four-digit seven segment, multiplexed pin DIG4
Resistor, 220 Ω (3) pin 1Four-digit seven segment, multiplexed pin DIG3
Resistor, 220 Ω (2) pin 1Four-digit seven segment, multiplexed pin DIG2
Resistor, 220 Ω (1) pin 1Four-digit seven segment, multiplexed pin DIG1
LED, green pin AResistor, 220 Ω (5) pin 1
LED, green pin CGround
LED, yellow pin AResistor, 220 Ω (6) pin 1
LED, yellow pin CGround
LED, red pin AResistor, 220 Ω (7) pin 1
LED, red pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Traffic light: green on GP7, yellow on GP8 and red on GP9, each through
# 220 ohm. A Timer ticks once a second: green 30 s, yellow 5 s, red 30 s,
# and the four-digit display counts down what is left.
from machine import Pin, Timer
import time

DIGITS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F]
ds = Pin(18, Pin.OUT)
latch = Pin(19, Pin.OUT)
clock = Pin(20, Pin.OUT)
selects = [Pin(n, Pin.OUT, value=1) for n in (10, 11, 12, 13)]   # digit 1 to 4

def segments(byte):
    for i in range(7, -1, -1):
        ds.value((byte >> i) & 1)
        clock.value(1)
        clock.value(0)
    latch.value(1)
    latch.value(0)

def show(text):
    # text is four characters, digits or spaces, with an optional "." after
    # one of them. Each digit is lit for a moment in turn, fast enough
    # that the eye sees all four.
    cells = []
    for ch in text:
        if ch == "." and cells:
            cells[-1] |= 0x80
        else:
            cells.append(DIGITS[int(ch)] if ch.isdigit() else 0)
    for n, byte in enumerate(cells[-4:]):
        segments(byte)
        selects[n].value(0)
        time.sleep_ms(2)
        selects[n].value(1)

lights = [Pin(n, Pin.OUT, value=0) for n in (7, 8, 9)]   # green, yellow, red
TIMES = [30, 5, 30]
NAMES = ["green", "yellow", "red"]
state = 0
left = TIMES[0]

def tick(timer):
    global state, left
    left -= 1
    if left == 0:
        state = (state + 1) % 3
        left = TIMES[state]
        print(NAMES[state])

Timer(period=1000, mode=Timer.PERIODIC, callback=tick)
print(NAMES[state])
while True:
    for n, light in enumerate(lights):
        light.value(1 if n == state else 0)
    show("%4d" % left)

Code walkthrough

The key functions of the sketch, and what each one does.

segments main.py

Shifts one byte out to the 74HC595, most significant bit first, then pulses the latch so the segment lines change together.

def segments(byte):
    for i in range(7, -1, -1):
        ds.value((byte >> i) & 1)
        clock.value(1)
        clock.value(0)
    latch.value(1)
    latch.value(0)

show main.py

Builds the segment bytes for four characters, then lights each digit for 2 ms in turn. Calling it over and over makes all four digits look steady.

def show(text):
    # text is four characters, digits or spaces, with an optional "." after
    # one of them. Each digit is lit for a moment in turn, fast enough
    # that the eye sees all four.
    cells = []
    for ch in text:
        if ch == "." and cells:
            cells[-1] |= 0x80
        else:
            cells.append(DIGITS[int(ch)] if ch.isdigit() else 0)
    for n, byte in enumerate(cells[-4:]):
        segments(byte)
        selects[n].value(0)
        time.sleep_ms(2)
        selects[n].value(1)

tick main.py

Runs every second from the Timer. It subtracts one from left. At zero it moves to the next color in the cycle of three, loads that color's time and prints its name.

def tick(timer):
    global state, left
    left -= 1
    if left == 0:
        state = (state + 1) % 3
        left = TIMES[state]
        print(NAMES[state])

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The lights are in the wrong order This project
    Green is GP7, yellow is GP8 and red is GP9. Check each LED is on the matching pin, with its anode on the resistor side.
  • The number flickers or goes dark This project
    The loop must keep calling show. Any long delay in the main loop makes the display go blank.
  • Outputs show garbage, or the register never clears. 74HC595 shift register
    Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte.
  • The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
    Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely.
  • Digits look dim, flicker, or show ghosts of other digits. Four-digit seven segment
    Refresh each digit quickly in a loop, at least 100 Hz for the whole set. Turn all segments off before switching digits. Do not use delay() for long pauses while displaying.
  • Segments burn out or the pins are overloaded. Four-digit seven segment
    Use a resistor of about 220 ohms on each segment line. Check whether the display is common cathode or common anode, since that sets which level turns a segment on.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.