Overview
A seven-segment display is eight LEDs sharing one common connection. In a common-cathode (CC) display the cathodes are joined: connect COM to GND and drive a segment HIGH to light it. In a common-anode (CA) display the anodes are joined: connect COM to 5V and drive a segment LOW to light it.
You will need
- Arduino Uno R3
- Seven-segment display (either type)
- 8 × 330 Ω resistors
- Breadboard
Wiring
| Uno pin | Connects to | Notes |
|---|---|---|
| 2 | Segment a through 330 Ω | |
| 3 | Segment b through 330 Ω | |
| 4 | Segment c through 330 Ω | |
| 5 | Segment d through 330 Ω | |
| 6 | Segment e through 330 Ω | |
| 7 | Segment f through 330 Ω | |
| 8 | Segment g through 330 Ω | |
| 9 | Segment dp through 330 Ω | |
| GND or 5V | COM | GND for common cathode; 5V for common anode |
Finding out which you have
Connect COM to GND and touch any segment pin to 5V through a 1 kΩ resistor. If a segment lights, it is common cathode. If not, swap: COM to 5V, segment to GND through the resistor. If that lights it, it is common anode. Part numbers often hint too (for example 5161AS is usually CC and 5161BS CA), but check, because suppliers vary.
Diagram
The sketch
// Set to true for a common-anode display, false for common-cathode.
const bool COMMON_ANODE = false;
const int FIRST_SEGMENT_PIN = 2; // a=2, b=3, ... g=8, dp=9
// Segments a b c d e f g dp, 1 = lit
const byte DIGITS[10][8] = {
{1,1,1,1,1,1,0,0}, // 0
{0,1,1,0,0,0,0,0}, // 1
{1,1,0,1,1,0,1,0}, // 2
{1,1,1,1,0,0,1,0}, // 3
{0,1,1,0,0,1,1,0}, // 4
{1,0,1,1,0,1,1,0}, // 5
{1,0,1,1,1,1,1,0}, // 6
{1,1,1,0,0,0,0,0}, // 7
{1,1,1,1,1,1,1,0}, // 8
{1,1,1,1,0,1,1,0} // 9
};
void showDigit(int digit) {
for (int segment = 0; segment < 8; segment++) {
bool lit = DIGITS[digit][segment] == 1;
if (COMMON_ANODE) {
lit = !lit; // CA: LOW lights the segment
}
digitalWrite(FIRST_SEGMENT_PIN + segment, lit ? HIGH : LOW);
}
}
void setup() {
for (int pin = FIRST_SEGMENT_PIN; pin < FIRST_SEGMENT_PIN + 8; pin++) {
pinMode(pin, OUTPUT);
}
}
void loop() {
for (int digit = 0; digit <= 9; digit++) {
showDigit(digit);
delay(700);
}
}How the code works
The data stays in "1 = lit" form and only the final write is inverted, so the same tables work for both display types. Note that "all off" on a CA display means writing every pin HIGH, which is why the LUCE and LAOS sketches (written for CC) show inverted letters on a CA part.
What you should observe
- The digits 0 to 9 count up, 0.7 s each.
- Inverted digits: flip
COMMON_ANODE. Scrambled digits: segment wires swapped.