MicroPython 7-segment digit through a 74HC595 on an ESP32-DevKitC V4
MicroPython on an ESP32: a 74HC595 on GPIO 15, 2 and 4 drives a common-anode 7-segment digit through 220 ohm each, counting 0 to F. 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 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $11.15. On Mokxi they're free.
What you'll learn
- A shift register and its clock and latch pins
- Common anode displays, which light on a 0
- Storing digit shapes as bytes
- Putting hardware code in its own module
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: ESP32-DevKitC V4
- Parts: 15
How it works
A 74HC595 shift register gives the ESP32 eight outputs from three pins: DS on GPIO 15, the latch on GPIO 2 and the clock on GPIO 4. The eight outputs drive the segments of a seven segment digit through 220 ohm resistors.
The helper file hc595.py sets one data bit, pulses the clock, and repeats for all eight bits. Then it pulses the latch so the new byte appears on the outputs. The main loop sends one pattern from a list every half a second, counting 0 to F.
The display is common anode, with its common pins on 5 V. A segment lights when its output is 0, so the patterns look upside down: 0xc0 shows a 0.
Parts list
15 parts, plus the jumper wires. They retail for about $11.15; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × Seven segment display
- 8 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 4 × Power, 5 V
Kits that can build it
These 3 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 SHCP | ESP32-DevKitC V4 pin 4 |
| 74HC595 shift register pin STCP | ESP32-DevKitC V4 pin 2 |
| 74HC595 shift register pin DS | ESP32-DevKitC V4 pin 15 |
| Resistor, 220 Ω (7) pin 1 | 74HC595 shift register pin Q1 |
| Resistor, 220 Ω (6) pin 1 | 74HC595 shift register pin Q2 |
| Resistor, 220 Ω (5) pin 1 | 74HC595 shift register pin Q3 |
| Resistor, 220 Ω (4) pin 1 | 74HC595 shift register pin Q4 |
| Resistor, 220 Ω (3) pin 1 | 74HC595 shift register pin Q5 |
| Resistor, 220 Ω (2) pin 1 | 74HC595 shift register pin Q6 |
| Resistor, 220 Ω (1) 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 Ω (8) pin 1 | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 5 V |
| Resistor, 220 Ω (5) pin 2 | Seven segment display pin E |
| Resistor, 220 Ω (4) pin 2 | Seven segment display pin D |
| Seven segment display pin COM1 | 5 V |
| Resistor, 220 Ω (3) pin 2 | Seven segment display pin C |
| Resistor, 220 Ω (8) pin 2 | Seven segment display pin DP |
| Resistor, 220 Ω (7) pin 2 | Seven segment display pin G |
| Resistor, 220 Ω (6) pin 2 | Seven segment display pin F |
| Seven segment display pin COM2 | 5 V |
| Resistor, 220 Ω (1) pin 2 | Seven segment display pin A |
| Resistor, 220 Ω (2) pin 2 | Seven segment display pin B |
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: the digit counts 0 to F, half
# a second each. The display is common anode, so a segment lights when
# its output is 0. Bits go out with DP first and A last, so Q7 drives A.
from hc595 import HC595
import time
chip = HC595(15, 2, 4) # DS on 15, ST_CP on 2, SH_CP on 4
# bit 7 is DP ... bit 0 is A, so these read as DP G F E D C B A
digits = [0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e]
while True:
for n, pattern in enumerate(digits):
chip.write(pattern, msb_first=False)
print("digit %X" % n)
time.sleep_ms(500)
# 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.
- The digit shows garbage shapes. This project
The segment order matters. Q7 drives segment A and Q0 drives DP, so check each resistor goes to the right segment. - The display is dark. This project
The display's common pins must be on 5 V, and the chip's OE pin on ground and MR on 5 V. - Python says it cannot import hc595. This project
The file hc595.py has to be on the board next to main.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 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.