How to wire a 7-segment display to an ESP32
To wire a 7-segment display to an ESP32, connect A to GPIO 13 (D13), B to GPIO 14 (D14), C to GPIO 27 (D27), D to GPIO 26 (D26), E to GPIO 25 (D25), F to GPIO 33 (D33), G to GPIO 32 (D32) and COM1 to GND. One 220 ohm resistor on the common pin limits the current to a little under 6 mA per segment at 3.3 V, which is dim but plain to see. The sketch below uses no library beyond what the board includes. It counts through the digits and shows each one.
- 3.3 V logic
- Library: none
- 8 wires, 1 helper parts
7-segment display to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| 7-segment display pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| Segment a | GPIO 13 (D13) | amber | Segments are named a to g, clockwise from the top, with g across the middle. |
| Segment b | GPIO 14 (D14) | green | |
| Segment c | GPIO 27 (D27) | cyan | |
| Segment d | GPIO 26 (D26) | violet | |
| Segment e | GPIO 25 (D25) | red | |
| Segment f | GPIO 33 (D33) | blue | |
| Segment g | GPIO 32 (D32) | gray | |
| Common (COM) | GND, through 220 ohm | gray | A common cathode display: every segment returns its current through this one resistor. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| R1 (220 Ω) pin 2 | GND | blue |
Power and logic levels for 7-segment display and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
This pair
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
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.
The working code for the ESP32
Library: none, the sketch drives each segment with digitalWrite(). This is the sketch the circuit above runs.
// 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);
}
}
What you should see
The display steps through the digits. The serial monitor, at 115200 baud, prints Showing and the digit as it changes.
This is a common cathode display, so the common pin goes to ground. A common anode display needs the common pin on power and the segment levels inverted.
The serial monitor prints this, recorded from the circuit above running:
Showing 0
Showing 1
Showing 2
Showing 3
Showing 4
Showing 5Common mistakes wiring a 7-segment display to an 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.
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.