MicroPython four-digit display on a Pico, multiplexed
MicroPython on a Raspberry Pi Pico: a 74HC595 sets the segments and GP17 to GP14 pick the digit, fast enough that a 4-digit display reads 0123. The whole build, a Raspberry Pi Pico and 10 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.
Advanced About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $5.75. On Mokxi they're free.
What you'll learn
- Shift registers add outputs to a small board
- Multiplexing a display one digit at a time
- Seven segment codes stored as bytes
- Common anode displays
- Persistence of vision
- Difficulty: Advanced
- Time: About 30 minutes
- Board: Raspberry Pi Pico
- Parts: 13
How it works
A four-digit display shares its segment lines between all digits, so only one digit can be lit at a time. A 74HC595 on GP18, GP20 and GP21 holds the segment pattern, and GP17, GP16, GP15 and GP14 each switch one digit on.
The loop lights each digit for 1 ms in turn. That is fast enough that your eye sees all four lit, showing 0123. It blanks the segments before each switch so nothing ghosts.
Parts list
13 parts, plus the jumper wires. They retail for about $5.75; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 8 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Four-digit seven segment, multiplexed
- 2 × Power, 3.3 V
Kits that can build it
This starter kit has 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 pin | Connects to |
|---|---|
| Four-digit seven segment, multiplexed pin DIG4 | Raspberry Pi Pico pin GP14 |
| Four-digit seven segment, multiplexed pin DIG3 | Raspberry Pi Pico pin GP15 |
| 74HC595 shift register pin SHCP | Raspberry Pi Pico pin GP21 |
| 74HC595 shift register pin STCP | Raspberry Pi Pico pin GP20 |
| 74HC595 shift register pin DS | Raspberry Pi Pico pin GP18 |
| Four-digit seven segment, multiplexed pin DIG1 | Raspberry Pi Pico pin GP17 |
| Four-digit seven segment, multiplexed pin DIG2 | Raspberry Pi Pico pin GP16 |
| Resistor, 220 Ω (2) pin 1 | 74HC595 shift register pin Q1 |
| Resistor, 220 Ω (3) pin 1 | 74HC595 shift register pin Q2 |
| Resistor, 220 Ω (4) pin 1 | 74HC595 shift register pin Q3 |
| Resistor, 220 Ω (5) pin 1 | 74HC595 shift register pin Q4 |
| Resistor, 220 Ω (6) pin 1 | 74HC595 shift register pin Q5 |
| Resistor, 220 Ω (7) pin 1 | 74HC595 shift register pin Q6 |
| Resistor, 220 Ω (8) pin 1 | 74HC595 shift register pin Q7 |
| 74HC595 shift register pin GND | Ground |
| 74HC595 shift register pin MR | 3.3 V |
| 74HC595 shift register pin OE | Ground |
| Resistor, 220 Ω (1) pin 1 | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 3.3 V |
| Four-digit seven segment, multiplexed pin DP | Resistor, 220 Ω (1) pin 2 |
| Four-digit seven segment, multiplexed pin G | Resistor, 220 Ω (2) pin 2 |
| Four-digit seven segment, multiplexed pin F | Resistor, 220 Ω (3) pin 2 |
| Four-digit seven segment, multiplexed pin E | Resistor, 220 Ω (4) pin 2 |
| Four-digit seven segment, multiplexed pin D | Resistor, 220 Ω (5) pin 2 |
| Four-digit seven segment, multiplexed pin C | Resistor, 220 Ω (6) pin 2 |
| Four-digit seven segment, multiplexed pin B | Resistor, 220 Ω (7) pin 2 |
| Four-digit seven segment, multiplexed pin A | Resistor, 220 Ω (8) pin 2 |
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.
# Four digits from one 74HC595: the chip (DS GP18, ST_CP GP20, SH_CP
# GP21) sets the segments, and GP17, GP16, GP15 and GP14 each switch on
# one digit's common anode. Lighting one digit at a time, a millisecond
# each, is fast enough for the eye to see all four: 0123.
from machine import Pin
import time
from hc595 import HC595
DIGITS = [0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90]
chip = HC595(ds=18, stcp=20, shcp=21)
commons = [Pin(n, Pin.OUT, value=0) for n in (17, 16, 15, 14)]
print("showing 0123")
while True:
for place, common in enumerate(commons):
chip.write(0xFF, msb_first=False) # blank, so nothing ghosts
common.value(1)
chip.write(DIGITS[place], msb_first=False)
time.sleep_ms(1)
common.value(0)
# hc595.py: a 74HC595 shift register on three pins.
# Each bit goes out on DS and is clocked in on a rising SH_CP; a rising
# ST_CP then copies all eight to the outputs Q0 to Q7 at once.
from machine import Pin
class HC595:
def __init__(self, ds, stcp, shcp, oe=None):
self.ds = Pin(ds, Pin.OUT, value=0)
self.stcp = Pin(stcp, Pin.OUT, value=0)
self.shcp = Pin(shcp, Pin.OUT, value=0)
if oe is not None:
self.oe = Pin(oe, Pin.OUT, value=0) # OE low: the outputs are on
def shift(self, byte, msb_first=True):
for i in range(8):
bit = 7 - i if msb_first else i
self.ds.value((byte >> bit) & 1)
self.shcp.value(1)
self.shcp.value(0)
def latch(self):
self.stcp.value(1)
self.stcp.value(0)
def write(self, byte, msb_first=True):
self.shift(byte, msb_first)
self.latch()
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing lights This project
Check MR is on 3.3 V and OE is on ground, so the outputs are on. - Digits look smeared This project
Each digit needs the blank step before the next one. Keep the 1 ms timing short, or the display flickers. - Wrong digit order This project
GP17 is the first digit (DIG1) and GP14 is the last (DIG4). Swap wires if your number reads backwards. - 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. - 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.