MicroPython 7-segment digit on a Pico through a 74HC595
MicroPython on a Raspberry Pi Pico: a 74HC595 on GP18, GP20 and GP21 shows the hex digits 0 to F on a common-anode 7-segment display. 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.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $5.15. On Mokxi they're free.
What you'll learn
- Driving a seven-segment display with a shift register
- Common anode displays: low means on
- Shifting bits out one at a time
- Latching all outputs at once
- Writing a small reusable MicroPython class
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Raspberry Pi Pico
- Parts: 15
How it works
A 74HC595 shift register takes three Pico pins, data on GP18, latch on GP20 and clock on GP21, and gives eight outputs. Each output drives one segment of a seven-segment display through a 220 Ω resistor.
This display is common anode: its common pins are tied to 5 V. A segment lights when its output is low, so every pattern has a 0 for each lit segment.
The program steps through the sixteen hex digits, 0 to F, one a second, and prints each one to the console. A small helper file handles the shifting and latching.
Parts list
15 parts, plus the jumper wires. They retail for about $5.15; 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 × Seven segment display
- 4 × Power, 5 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 |
|---|---|
| 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 |
| 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 | 5 V |
| 74HC595 shift register pin OE | Ground |
| Resistor, 220 Ω (1) pin 1 | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 5 V |
| Seven segment display pin DP | Resistor, 220 Ω (1) pin 2 |
| Seven segment display pin G | Resistor, 220 Ω (2) pin 2 |
| Seven segment display pin F | Resistor, 220 Ω (3) pin 2 |
| Seven segment display pin E | Resistor, 220 Ω (4) pin 2 |
| Seven segment display pin D | Resistor, 220 Ω (5) pin 2 |
| Seven segment display pin C | Resistor, 220 Ω (6) pin 2 |
| Seven segment display pin B | Resistor, 220 Ω (7) pin 2 |
| Seven segment display pin A | Resistor, 220 Ω (8) pin 2 |
| Seven segment display pin COM1 | 5 V |
| Seven segment display pin COM2 | 5 V |
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.
# One 7-segment digit through a 74HC595 (DS GP18, ST_CP GP20, SH_CP
# GP21): the hex digits 0 to F, one a second. The display is common
# anode, so a segment lights when its output is low: each pattern below
# has a 0 for every lit segment, in the order DP G F E D C B A.
import time
from hc595 import HC595
DIGITS = [0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E]
chip = HC595(ds=18, stcp=20, shcp=21)
while True:
for n, pattern in enumerate(DIGITS):
chip.write(pattern, msb_first=False) # A goes out first, to Q7
print("digit %X" % n)
time.sleep(1)
# 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.
- The digits look inverted, with unlit segments glowing This project
Check the display type. This one is common anode, with COM1 and COM2 on 5 V, and the patterns are written for it. - Segments appear in the wrong places This project
Q0 goes to segment DP, Q1 to G, down to Q7 on segment A. Check each resistor lines up with its segment. - 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. - Segments stay dark, or light when they should be off. Seven segment display
Check whether the display is common cathode or common anode. Common cathode: common pin to GND, drive segments HIGH. Common anode: common pin to VCC, drive segments LOW. - One segment is very bright or the LEDs burn out. Seven segment display
Each segment is an LED, so it needs its own resistor, about 220 ohm to 330 ohm on 5 V. One resistor on the common pin makes brightness change with the number of lit segments.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.