MicroPython four-digit display, multiplexed, on an ESP32-DevKitC V4
MicroPython on an ESP32: a 74HC595 on GPIO 15, 2 and 4 sets the segments, GPIO 25, 26, 27 and 14 pick the digit, and 0123 is multiplexed. 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.
Advanced About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $11.75. On Mokxi they're free.
What you'll learn
- Shift registers turn three pins into eight outputs
- Multiplexing: one digit at a time, fast
- Seven segment codes stored as bytes
- Persistence of vision
- Current limiting resistors for segments
- Difficulty: Advanced
- Time: About 30 minutes
- Board: ESP32-DevKitC V4
- Parts: 13
How it works
Four digits share the same seven segment lines, so only one can be lit at a time. A 74HC595 shift register holds the pattern for the segments, and four ESP32 pins (GPIO 25, 26, 27 and 14) pick which digit gets it.
The program lights each digit for about a millisecond, in turn, over and over. That is fast enough that your eye sees all four lit, showing 0123. Before each digit it blanks the segments so no ghost of the last digit shows.
Parts list
13 parts, plus the jumper wires. They retail for about $11.75; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × 74HC595 shift registerIn LED Cube Kit.
- 1 × Four-digit seven segment, multiplexed
- 8 × 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 |
|---|---|
| Four-digit seven segment, multiplexed pin DIG1 | ESP32-DevKitC V4 pin 25 |
| Four-digit seven segment, multiplexed pin DIG2 | ESP32-DevKitC V4 pin 26 |
| Four-digit seven segment, multiplexed pin DIG3 | ESP32-DevKitC V4 pin 27 |
| Four-digit seven segment, multiplexed pin DIG4 | ESP32-DevKitC V4 pin 14 |
| 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 | 3.3 V |
| 74HC595 shift register pin OE | Ground |
| Resistor, 220 Ω (8) pin 1 | 74HC595 shift register pin Q0 |
| 74HC595 shift register pin VCC | 3.3 V |
| Resistor, 220 Ω (5) pin 2 | Four-digit seven segment, multiplexed pin E |
| Resistor, 220 Ω (4) pin 2 | Four-digit seven segment, multiplexed pin D |
| Resistor, 220 Ω (8) pin 2 | Four-digit seven segment, multiplexed pin DP |
| Resistor, 220 Ω (3) pin 2 | Four-digit seven segment, multiplexed pin C |
| Resistor, 220 Ω (7) pin 2 | Four-digit seven segment, multiplexed pin G |
| Resistor, 220 Ω (2) pin 2 | Four-digit seven segment, multiplexed pin B |
| Resistor, 220 Ω (6) pin 2 | Four-digit seven segment, multiplexed pin F |
| Resistor, 220 Ω (1) pin 2 | Four-digit seven segment, multiplexed pin A |
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.
# Four digits, one 74HC595: the chip sets the segments and GPIO 25, 26,
# 27 and 14 pick which digit is on. Each digit is lit for a moment in
# turn, fast enough that all four look lit: 0123.
from machine import Pin
from hc595 import HC595
import time
chip = HC595(15, 2, 4)
digits = [0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e]
select = [Pin(n, Pin.OUT, value=0) for n in (25, 26, 27, 14)]
show = [0, 1, 2, 3]
frames = 0
while True:
for place, n in enumerate(show):
chip.write(0xff, msb_first=False) # blank, so no ghost of the last digit
for i, s in enumerate(select):
s.value(1 if i == place else 0)
chip.write(digits[n], msb_first=False)
time.sleep_ms(1)
frames += 1
if frames % 200 == 0:
print("showing 0123")
# 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.
- Digits ghost into each other This project
The segments must be blanked before the digit changes. Keep the blank write at the start of each pass. - The display stays dark This project
Check that MR is on 3.3 V and OE is on ground, so the outputs are enabled. Then check DS on GPIO 15, STCP on 2 and SHCP on 4. - Segments show in the wrong places This project
Q0 to Q7 must go through the resistors to segments A to G and DP in order. - 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. - Digits look dim, flicker, or show ghosts of other digits. Four-digit seven segment
Refresh each digit quickly in a loop, at least 100 Hz for the whole set. Turn all segments off before switching digits. Do not use delay() for long pauses while displaying. - Segments burn out or the pins are overloaded. Four-digit seven segment
Use a resistor of about 220 ohms on each segment line. Check whether the display is common cathode or common anode, since that sets which level turns a segment on. - 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.