Built-in project · Raspberry Pi Pico W

Pico W four-digit display counts seconds

MicroPython on a Pico W: a 74HC595 on GP18 to GP20 and digit selects on GP10 to GP13 multiplex a four-digit display that counts the seconds. The whole build, a Raspberry Pi Pico W and 6 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 $7.55. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Multiplexing a four-digit display
  • A shift register setting the segments
  • Seven segment patterns as bytes
  • Timing with ticks_ms and ticks_diff
  • Difficulty: Intermediate
  • Time: About 30 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 9

How it works

A four-digit display shares one set of segment lines between all four digits, so only one digit can be lit at a time. A 74HC595 on GP18, GP19 and GP20 sets which segments are on. Pulling a digit's select pin low, GP10 to GP13, lights that digit.

show lights each of the four digits for 2 ms, one after another, over and over. That is too fast to see, so all four look lit. The main loop works out the seconds since start, and calls show every pass with the seconds padded to four places.

The number wraps back to 0 after 9999. It prints the new second to the console as it changes.

Parts list

9 parts, plus the jumper wires. They retail for about $7.55; on Mokxi they're free.

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 Ω (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

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
# Time counter: a four-digit display shows the seconds since start. The
# 74HC595 (DS GP18, STcp GP19, SHcp GP20) sets the segments, and pulling
# a digit's select pin (GP10 to GP13) low lights that digit.
from machine import Pin
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)

start = time.ticks_ms()
shown = -1

while True:
    seconds = time.ticks_diff(time.ticks_ms(), start) // 1000
    if seconds != shown:
        shown = seconds
        print("seconds", seconds)
    show("%4d" % (seconds % 10000))

Code walkthrough

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

segments main.py

segments shifts eight bits into the 74HC595, top bit first, pulsing the clock after each one. Then it pulses the latch so the new pattern appears on the outputs. Bit 7 is the decimal point.

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

show turns four characters into segment bytes, adding the point to the digit before a dot. Then for each digit it sends the segments, pulls that digit's select pin low for 2 ms and sets it high again.

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)

Troubleshooting

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

  • The digits flicker or one is dim. This project
    show has to be called constantly. A long sleep anywhere in the loop makes the display blink.
  • The numbers appear in the wrong digits. This project
    DIG1 to DIG4 go through resistors to GP10 to GP13 in order.
  • Segments are in the wrong places. This project
    Q0 to Q7 go to A, B, C, D, E, F, G and DP in that order.
  • 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.

Change it and keep it

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