MicroPython 74HC595 flowing light on a Pico
MicroPython on a Raspberry Pi Pico: a 74HC595 on GP18, GP20 and GP21 walks one lit LED along eight bars of an LED bar graph and back. 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.55. On Mokxi they're free.
What you'll learn
- Expanding outputs with a 74HC595 shift register
- Data, shift clock and latch: the three signals of a 595
- Using a bit shift to light one output at a time
- Splitting MicroPython code into a main file and a small library
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Raspberry Pi Pico
- Parts: 13
How it works
A 74HC595 shift register turns three Pico pins into eight outputs. The Pico sends a byte one bit at a time on GP18, clocking each bit in with GP21. A pulse on GP20 then copies all eight bits to the outputs at once. GP19 is held low so the outputs stay on.
The program lights one bit and moves it across the eight outputs, 300 ms per step. Then it walks back, and prints sweep at the end of each pass. The outputs go through eight 220 ohm resistors to the LED bar graph.
Parts list
13 parts, plus the jumper wires. They retail for about $5.55; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × LED bar graph, 10 segmentsIn Spaceship Panel Kit.
- 8 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 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 |
|---|---|
| 74HC595 shift register pin SHCP | Raspberry Pi Pico pin GP21 |
| 74HC595 shift register pin STCP | Raspberry Pi Pico pin GP20 |
| 74HC595 shift register pin OE | Raspberry Pi Pico pin GP19 |
| 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 | 3.3 V |
| Resistor, 220 Ω (1) pin 1 | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 3.3 V |
| Resistor, 220 Ω (8) pin 2 | LED bar graph, 10 segments pin A1 |
| Resistor, 220 Ω (7) pin 2 | LED bar graph, 10 segments pin A2 |
| Resistor, 220 Ω (6) pin 2 | LED bar graph, 10 segments pin A3 |
| Resistor, 220 Ω (5) pin 2 | LED bar graph, 10 segments pin A4 |
| Resistor, 220 Ω (4) pin 2 | LED bar graph, 10 segments pin A5 |
| Resistor, 220 Ω (3) pin 2 | LED bar graph, 10 segments pin A6 |
| Resistor, 220 Ω (2) pin 2 | LED bar graph, 10 segments pin A7 |
| Resistor, 220 Ω (1) pin 2 | LED bar graph, 10 segments pin A8 |
| LED bar graph, 10 segments pin A10 | LED bar graph, 10 segments pin A9 |
| LED bar graph, 10 segments pin C9 | LED bar graph, 10 segments pin C10 |
| LED bar graph, 10 segments pin C8 | Ground |
| LED bar graph, 10 segments pin C7 | Ground |
| LED bar graph, 10 segments pin C6 | Ground |
| LED bar graph, 10 segments pin C5 | Ground |
| LED bar graph, 10 segments pin C4 | Ground |
| LED bar graph, 10 segments pin C3 | Ground |
| LED bar graph, 10 segments pin C2 | Ground |
| LED bar graph, 10 segments pin C1 | 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.
# A flowing light through a 74HC595: three pins (DS GP18, ST_CP GP20,
# SH_CP GP21) set eight LEDs of the bar graph. One lit bit walks across
# the eight outputs one way, then back. hc595.py is in the next tab.
# GP19 is the chip's OE, held low so the outputs are on.
import time
from hc595 import HC595
chip = HC595(ds=18, stcp=20, shcp=21, oe=19)
while True:
for i in range(8):
chip.write(0x01 << i)
time.sleep_ms(300)
for i in range(6, 0, -1):
chip.write(0x01 << i)
time.sleep_ms(300)
print("sweep")
# 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 that OE is on GP19. It must be low for the outputs to turn on. - The light jumps around instead of moving smoothly This project
Check DS is on GP18, STCP on GP20 and SHCP on GP21, and that none are swapped. - 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 are dim, uneven, or the chip is damaged. LED bar graph
Every segment is its own LED and needs its own current-limiting resistor, about 220 to 330 ohms at 5 V. One shared resistor makes brightness change with how many are lit. - The bar lights in reverse order or a segment never lights. LED bar graph
Check pin 1 and the orientation. Anode and cathode sides are on opposite rows. Some bars are numbered the other way around, so test with one segment at a time. - 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.