Count 0 to 9
Five wires and a sketch that counts from 0 to 9, a second each, with no library: shiftOut and a ten-byte table. The same sketch runs on an Uno, an ESP32 or an ESP32-S3, and there is a MicroPython version for the ESP32s and the Pico.
Five wires
GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the others. Then the three signals:
| Your board | LATCH | CLOCK | DATA |
|---|---|---|---|
| Arduino Uno | D8 | D12 | D11 |
| ESP32 | GPIO 27 | GPIO 26 | GPIO 25 |
| ESP32-S3 | GPIO 6 | GPIO 5 | GPIO 4 |
| Raspberry Pi Pico | GP2 | GP3 | GP4 |
NC gets no wire. Any three free outputs would do; these are the ones the sketches name, and they are free at boot on each board.
The sketch
show() does the three steps from
Shift, then latch:
LATCH low, shiftOut with MSBFIRST, LATCH high. DIGITS is the table from
One byte, one digit.
setup() sends 0 to blank the digit, since nothing else clears it at
power-up, and loop() shows 0 to 9, a second each, then the dot alone.
What you should see
The digit counts 0, 1, 2 up to 9, then shows only its dot for a second, and starts again. At 115200 the serial monitor prints each digit and its byte:
0 0x3F
1 0x6
2 0x5BIf the shapes are wrong but steady, see Which bit goes first.
The code
No library to install: shiftOut is built into every Arduino core. show() is the whole conversation with the chip: LATCH low, eight bits, LATCH high. Change only the three pin numbers if your board is not an Uno.
/*
74HC595 Segment LED - count 0 to 9 TK53 / /p/tk53
Wiring. Count from pin 1, printed GND-1 on the TinkerBlock board
(this board has no square pad), digit up, header at the bottom:
GND -> GND
VCC -> your board's logic supply: 5V on an Uno, 3V3 on an
ESP32, ESP32-S3 or Pico. At 5V the chip may not read
a 3.3 V board's HIGH, so never 5V beside a 3.3 V board.
NC -> nothing (unconnected on the board)
LATCH -> D8 on an Uno, GPIO 27 on an ESP32, GPIO 6 on an
ESP32-S3, GP2 on a Raspberry Pi Pico
CLOCK -> D12 on an Uno, GPIO 26 on an ESP32, GPIO 5 on an
ESP32-S3, GP3 on a Raspberry Pi Pico
DATA -> D11 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an
ESP32-S3, GP4 on a Raspberry Pi Pico
Arduino IDE
Tools > Board your board, e.g. Arduino Uno
Tools > Port the one that appears when you plug in
Tools > USB CDC On Boot Enabled (ESP32-S3 only)
No library to install: shiftOut is built in.
Serial Monitor at 115200.
*/
// The pins LATCH, CLOCK and DATA are wired to. These are the Uno's.
// ESP32: 27, 26, 25. ESP32-S3: 6, 5, 4. Pico: 2, 3, 4.
const int LATCH_PIN = 8;
const int CLOCK_PIN = 12;
const int DATA_PIN = 11;
// One byte per digit. Bit 0 is segment a (QA) and bit 7 the
// decimal point (QH). The digit is common cathode: a 1 lights.
const byte DIGITS[10] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
};
const byte DOT = 0b10000000; // the decimal point, QH
// Eight bits in, then one latch: the digit changes all at once.
void show(byte pattern) {
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);
digitalWrite(LATCH_PIN, HIGH); // the rising edge shows it
}
void setup() {
Serial.begin(115200);
pinMode(LATCH_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
show(0); // blank
}
void loop() {
for (int n = 0; n < 10; n++) {
show(DIGITS[n]);
Serial.print(n);
Serial.print(" 0x");
Serial.println(DIGITS[n], HEX);
delay(1000);
}
show(DOT); // a dot between rounds
delay(1000);
}The table is common cathode, a 1 lights, with segment a in bit 0 and the dot in bit 7, which is why shiftOut sends MSBFIRST. The Serial lines are only there so you can see which byte is on the digit.
View on GitHub · blocks/tk53-hc595-segment-led/arduino/hc595_segment_led/hc595_segment_led.ino @ v1.2The same count in MicroPython, for an ESP32, an ESP32-S3 or a Pico. show() is shiftOut written out: eight times, set DATA and pulse CLOCK, then raise LATCH.
"""
74HC595 Segment LED - count 0 to 9, MicroPython TK53 / /p/tk53
Wiring. Count from pin 1, printed GND-1 on the TinkerBlock board
(this board has no square pad), digit up, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: at 5V the chip may not read 3.3 V
as HIGH)
NC -> nothing (unconnected on the board)
LATCH -> GPIO 27 on an ESP32, GPIO 6 on an ESP32-S3, GP2 on a Pico
CLOCK -> GPIO 26 on an ESP32, GPIO 5 on an ESP32-S3, GP3 on a Pico
DATA -> GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3, GP4 on a Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine is built in.
"""
from machine import Pin
import time
# The GPIO numbers LATCH, CLOCK and DATA are wired to.
# ESP32: 27, 26, 25. ESP32-S3: 6, 5, 4. Pico: 2, 3, 4.
latch = Pin(6, Pin.OUT)
clock = Pin(5, Pin.OUT)
data = Pin(4, Pin.OUT)
# One byte per digit. Bit 0 is segment a (QA) and bit 7 the
# decimal point (QH). The digit is common cathode: a 1 lights.
DIGITS = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F]
DOT = 0x80
def show(pattern):
"""Eight bits in, most significant first, then one latch."""
latch.value(0)
for i in range(7, -1, -1):
data.value((pattern >> i) & 1)
clock.value(1) # the rising edge shifts it in
clock.value(0)
latch.value(1) # the rising edge shows it
show(0) # blank
while True:
for n in range(10):
show(DIGITS[n])
print(n, hex(DIGITS[n]))
time.sleep(1)
show(DOT) # a dot between rounds
time.sleep(1)There is no Uno here: an Uno cannot run MicroPython. The loop runs from bit 7 down to bit 0, which is MSBFIRST. The pins are the ESP32-S3's; the comment above them lists the others.
View on GitHub · blocks/tk53-hc595-segment-led/micropython/hc595_segment_led.py @ v1.2When it does not work
Check GND and VCC first, counting from GND-1 on the left. Then check LATCH goes to the pin the sketch names: until LATCH rises, the digit never changes. On an ESP32-S3, upload with the board's USB port selected.
The sketch's pin numbers are the Uno's, and GPIO 11 and 12 on an ESP32 are not free: 11 runs the flash. Change the three numbers to 27, 26 and 25, as the comment above them says.
Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port brings up no serial port at boot. The digit counts either way.
Check the shiftOut line says MSBFIRST and the table is the one in the sketch. A table from another tutorial is often common anode or written in the other bit order.
Dark, mirrored, inverted, random and dim: what each one means.
When the digit is wrong →Edit this page — content/books/hc595-segment-led/count-zero-to-nine.mdx
Questions about this product
See what other owners have asked, and read their solutions.
74HC595 Segment LED
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.