ESP32 7-Segment Display Simulator: Wiring, Code and a Live Circuit
This is a single-digit common cathode 7-segment display wired to an ESP32 DevKit and counting from 0 to 9, live: the drawing shows the lit segments and the serial monitor names the digit.
- 3.3 V logic
- No library
- Segments on 13, 14, 27, 26, 25, 33, 32
- Common cathode, 220 ohm
Wiring the 7-Segment Display to the ESP32
Eight connections: seven segment pins from GPIO 13 (D13) onward, and the common through a 220 ohm resistor to GND. Open it in the editor and change the DIGITS table to draw letters.
Every connection in the circuit above, with the board’s own pin names.
| 7-Segment Display pin | ESP32 pin | Why |
|---|---|---|
| Segment a | GPIO 13 (D13) | Segments are named a to g, clockwise from the top, with g across the middle. |
| Segment b | GPIO 14 (D14) | |
| Segment c | GPIO 27 (D27) | |
| Segment d | GPIO 26 (D26) | |
| Segment e | GPIO 25 (D25) | |
| Segment f | GPIO 33 (D33) | |
| Segment g | GPIO 32 (D32) | |
| Common (COM) | GND, through 220 ohm | A common cathode display: every segment returns its current through this one resistor. |
Power
On a 3.3 V pin a red segment gets about 1.3 V across the resistor and a little under 6 mA, which is dim but plain to see. One resistor on the common is a shortcut: the more segments are lit, the less each one gets, so an 8 is dimmer than a 1. The better wiring is one resistor per segment.
Pins to leave alone on the ESP32
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
On a real bench
The thirty-pin DevKit straddles a breadboard with one row of holes free on each side, which is where the 7-Segment Display’s jumpers go. Many ESP32 boards are wider and cover both rows; then use two breadboards side by side. Hold BOOT while uploading if the IDE cannot connect.
The code, and what it does.
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
// Segments a to g, in order. The common cathode goes to GND through 220 ohm.
const int SEG_PINS[] = {13, 14, 27, 26, 25, 33, 32};
// One bit per segment: bit 0 is a, bit 6 is g.
const byte DIGITS[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
void show(byte mask) {
for (int i = 0; i < 7; i++) {
digitalWrite(SEG_PINS[i], (mask >> i) & 1);
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 7; i++) pinMode(SEG_PINS[i], OUTPUT);
}
void loop() {
for (int d = 0; d < 10; d++) {
show(DIGITS[d]);
Serial.print("Showing ");
Serial.println(d);
delay(400);
}
}
A seven-segment digit is seven LEDs and a decimal point in one package, with the cathodes joined into a common pin. Each digit is stored as one byte with a bit per segment, so 0x06 lights b and c and shows a 1, and 0x7F lights all seven for an 8.
show() writes each bit of the byte to its pin. The loop walks the ten bytes and holds each for 400 ms, and the serial monitor says which digit it is on. On a common anode display the common goes to the supply instead and every bit is inverted.
Serial.begin(115200) opens the serial monitor. Serial is UART0 on the USB bridge. ESP32 sketches usually run it at 115200 baud, which this one does.
Common mistakes with the 7-Segment Display on the ESP32
The wrong display type
A common anode display needs the common on the supply and every bit inverted. Run common cathode code on it and each digit shows as its own negative. The part has a common property in the editor.
Wiring by position, not by letter
The pins on the package are not in segment order. Wire each by its letter, from the pin table, not from the order they sit on the body.
Putting a segment on an input-only pin
GPIO 34, 35, 36 and 39 on the ESP32 have no output driver at all. pinMode() to OUTPUT on one of them does nothing and the line never moves, which looks exactly like a dead part. GPIO 4 (D4) is a safe choice; so are GPIO 13, 14, 16 to 19, 23, 25 to 27 and 32 to 33.
Questions and answers
Can I simulate a 7-segment display with an ESP32 DevKit?
Yes. The circuit at the top of this page is an ESP32 DevKit with a 7-segment display, running in your browser on a simulated ESP32 (Xtensa LX6). The segments are solved as real LEDs, so the brightness changes with the resistor and with how many segments are lit. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
How many pins does a 7-segment display need?
Seven for the segments, an eighth for the decimal point if you use it, and the common. Drive more digits with a shift register or a driver such as the TM1637, which this site also covers.
How do I show the decimal point?
It is an eighth LED on the DP pin. Wire it to another output pin through its own resistor and write it HIGH. This page leaves it unused.
Why does an 8 look dimmer than a 1?
Because all the lit segments share one resistor on the common pin. Put a 220 ohm resistor in series with each segment instead and every digit is equally bright.
Build your own 7-Segment Display project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.