MicroPython 74HC595 flowing light on an LED bar, on an ESP32-DevKitC V4
MicroPython on an ESP32: a 74HC595 on GPIO 14, 12 and 13 walks one lit segment along eight bars of an LED bar graph and back. The whole build, an ESP32-DevKitC V4 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 $11.55. On Mokxi they're free.
What you'll learn
- MicroPython on an ESP32
- Splitting code into a module and a main program
- Shift registers: data, clock and latch
- Bit shifting to move a single lit LED
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: ESP32-DevKitC V4
- Parts: 13
How it works
This MicroPython program runs on an ESP32 and drives a 74HC595 shift register with three pins. The chip's eight outputs go through 220 ohm resistors to an LED bar graph.
The main.py loop sends a byte with a single bit set. That one lit LED walks from the first output to the eighth, then back toward the first, waiting 300 ms at each step.
The hc595.py file holds a small helper class. It sets each bit on the data pin, pulses the clock, then pulses the latch so all eight outputs update at once.
Parts list
13 parts, plus the jumper wires. They retail for about $11.55; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 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 DS | ESP32-DevKitC V4 pin 14 |
| 74HC595 shift register pin STCP | ESP32-DevKitC V4 pin 12 |
| 74HC595 shift register pin SHCP | ESP32-DevKitC V4 pin 13 |
| Resistor, 220 Ω (2) pin 2 | 74HC595 shift register pin Q1 |
| Resistor, 220 Ω (3) pin 2 | 74HC595 shift register pin Q2 |
| Resistor, 220 Ω (4) pin 2 | 74HC595 shift register pin Q3 |
| Resistor, 220 Ω (5) pin 2 | 74HC595 shift register pin Q4 |
| Resistor, 220 Ω (6) pin 2 | 74HC595 shift register pin Q5 |
| Resistor, 220 Ω (7) pin 2 | 74HC595 shift register pin Q6 |
| Resistor, 220 Ω (8) pin 2 | 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 2 | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 3.3 V |
| LED bar graph, 10 segments pin A2 | LED bar graph, 10 segments pin A1 |
| Resistor, 220 Ω (8) pin 1 | LED bar graph, 10 segments pin A3 |
| Resistor, 220 Ω (7) pin 1 | LED bar graph, 10 segments pin A4 |
| Resistor, 220 Ω (6) pin 1 | LED bar graph, 10 segments pin A5 |
| Resistor, 220 Ω (5) pin 1 | LED bar graph, 10 segments pin A6 |
| Resistor, 220 Ω (4) pin 1 | LED bar graph, 10 segments pin A7 |
| Resistor, 220 Ω (3) pin 1 | LED bar graph, 10 segments pin A8 |
| Resistor, 220 Ω (2) pin 1 | LED bar graph, 10 segments pin A9 |
| Resistor, 220 Ω (1) pin 1 | LED bar graph, 10 segments pin A10 |
| LED bar graph, 10 segments pin C10 | Ground |
| LED bar graph, 10 segments pin C9 | Ground |
| 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 C1 | LED bar graph, 10 segments pin C2 |
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.
# Flowing water light through a 74HC595: one lit LED walks along the
# eight outputs and back, three GPIOs doing the work of eight.
from hc595 import HC595
import time
chip = HC595(14, 12, 13) # DS on 14, ST_CP on 12, SH_CP on 13
while True:
for i in range(8):
chip.write(1 << i)
time.sleep_ms(300)
for i in range(6, 0, -1):
chip.write(1 << i)
time.sleep_ms(300)
print("back and forth")
# hc595.py: send a byte to a 74HC595 on three pins.
# data is DS, latch is ST_CP and clock is SH_CP. Each bit is set on DS
# and clocked in on a rising SH_CP; a rising ST_CP shows all eight.
from machine import Pin
class HC595:
def __init__(self, data, latch, clock, oe=None):
self.data = Pin(data, Pin.OUT, value=0)
self.latch = Pin(latch, Pin.OUT, value=0)
self.clock = Pin(clock, 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, value, msb_first=True):
for i in range(8):
bit = 7 - i if msb_first else i
self.data.value((value >> bit) & 1)
self.clock.value(1)
self.clock.value(0)
def write(self, value, msb_first=True):
self.latch.value(0)
self.shift(value, msb_first)
self.latch.value(1)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- No LEDs light This project
Check that the chip's OE pin goes to ground and its MR pin goes to 3.3V, and that it has 3.3V on VCC. - The light jumps around instead of flowing This project
Check the three signal wires: DS on GPIO 14, latch on 12 and clock on 13. Swapped wires scramble the bits. - ImportError for hc595 This project
Make surehc595.pyis on the board next tomain.py. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.