Pico W 74HC595 counts 0 to 9 on a digit
MicroPython on a Pico W: a 74HC595 on GP0, GP1 and GP2 drives a common-cathode seven-segment digit that counts 0 to 9, half a second each. The whole build, a Raspberry Pi Pico W and 3 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.80. On Mokxi they're free.
What you'll learn
- Shift registers that add output pins
- Data, clock and latch signals
- Encoding a digit as a byte of segments
- Driving a common-cathode seven-segment display
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Raspberry Pi Pico W
- Parts: 6
How it works
A 74HC595 shift register turns three Pico W pins into eight outputs. GP0 sends data, GP2 clocks each bit in, and GP1 latches the bits onto the outputs. Outputs Q0 to Q7 go to segments a to g and the dot.
Each digit is one byte in the DIGITS list. For example, 0x3F lights the segments that make a zero. The show function sends a byte, and the display lights those segments.
The display is common cathode. Its common pins go to ground through one 220 ohm resistor. The main loop shows each digit from 0 to 9, holds it for 500 ms, and prints its number.
Parts list
6 parts, plus the jumper wires. They retail for about $6.80; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × Seven segment display
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Power, 3.3 V
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 pin | Connects to |
|---|---|
| 74HC595 shift register pin DS | Raspberry Pi Pico W pin GP0 |
| 74HC595 shift register pin STCP | Raspberry Pi Pico W pin GP1 |
| 74HC595 shift register pin SHCP | Raspberry Pi Pico W pin GP2 |
| Seven segment display pin B | 74HC595 shift register pin Q1 |
| Seven segment display pin C | 74HC595 shift register pin Q2 |
| Seven segment display pin D | 74HC595 shift register pin Q3 |
| Seven segment display pin E | 74HC595 shift register pin Q4 |
| Seven segment display pin F | 74HC595 shift register pin Q5 |
| Seven segment display pin G | 74HC595 shift register pin Q6 |
| Seven segment display pin DP | 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 |
| Seven segment display pin A | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 3.3 V |
| Seven segment display pin COM2 | Seven segment display pin COM1 |
| Resistor, 220 Ω pin 1 | Seven segment display pin COM1 |
| Resistor, 220 Ω pin 2 | Ground |
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.
# Number display: the 74HC595 (DS GP0, STcp GP1, SHcp GP2) sets the
# segments of a common-cathode digit, Q0 on segment a to Q7 on the dot.
# Each digit's pattern is one byte.
from machine import Pin
import time
DIGITS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F]
ds = Pin(0, Pin.OUT)
latch = Pin(1, Pin.OUT)
clock = Pin(2, Pin.OUT)
def show(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)
while True:
for n, pattern in enumerate(DIGITS):
show(pattern)
print("digit", n)
time.sleep_ms(500)
Code walkthrough
The key functions of the sketch, and what each one does.
show main.py
It sends one byte to the 74HC595, from the highest bit to the lowest. For each bit it sets the data pin, then pulses the clock to shift the bit in. After all eight bits it pulses the latch pin, and the outputs change at once.
def show(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)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The segments show the wrong shapes. This project
Check that Q0 goes to segment A and Q1 to B, on up to Q6 on G and Q7 on the dot. - The display stays blank. This project
OE must be on ground, MR on 3.3 V, and the display's common pins must reach ground through the 220 ohm resistor. - Digits appear one step late or scrambled. This project
Check GP1 goes to STCP (latch) and GP2 goes to SHCP (clock), not the other way around. - 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. - 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. - 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.