Seven-segment digit on pins 4 to 11, on an Arduino Uno
A common-cathode seven-segment display wired straight to the Uno, a on pin 7, b on 6, c on 5, d on 11, e on 10, f on 8, g on 9 and the dot on 4: it counts from 0 to 9. The whole build, an Arduino Uno and 2 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.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- How a seven-segment display works
- Common-cathode wiring
- Storing patterns as bits in a byte
- Using an array of pin numbers
- Reading single bits with
bitRead
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A seven-segment digit is seven small LEDs shaped like an 8, plus a dot. Here each segment has its own Uno pin: a on pin 7, b on 6, c on 5, d on 11, e on 10, f on 8, g on 9 and the dot on 4. The common cathode goes to ground through one 220 ohm resistor.
Each digit 0 to 9 is stored as one byte, with one bit per segment. The sketch reads the bits of that byte and sets each segment pin to match.
It counts 0 to 9, one digit per second, and prints the number to serial. The dot lights on odd digits.
Parts list
4 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Seven segment displayIn Display Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
These 8 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 |
|---|---|
| Resistor, 220 Ω pin 2 | Arduino Uno R3 pin GND |
| Seven segment display pin D | Arduino Uno R3 pin 11 |
| Seven segment display pin E | Arduino Uno R3 pin 10 |
| Seven segment display pin G | Arduino Uno R3 pin 9 |
| Seven segment display pin F | Arduino Uno R3 pin 8 |
| Seven segment display pin A | Arduino Uno R3 pin 7 |
| Seven segment display pin B | Arduino Uno R3 pin 6 |
| Seven segment display pin C | Arduino Uno R3 pin 5 |
| Seven segment display pin DP | Arduino Uno R3 pin 4 |
| Resistor, 220 Ω pin 1 | Seven segment display pin COM1 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A seven-segment display driven straight from eight pins: 0 to 9.
//
// segment a -> pin 7 b -> pin 6 c -> pin 5 d -> pin 11
// segment e -> pin 10 f -> pin 8 g -> pin 9 dp -> pin 4
// each through its own 220 ohm; the common cathode -> GND
//
// Each segment is an LED. A digit is a pattern of lit segments, kept here as
// one byte per digit with segment a in the lowest bit. The sketch walks the
// seven bits and sets each segment's pin from its bit.
// gfedcba
const byte DIGITS[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
const int SEGMENT_PINS[7] = {7, 6, 5, 11, 10, 8, 9}; // a to g
const int DOT_PIN = 4;
void show(int n) {
for (int s = 0; s < 7; s++) digitalWrite(SEGMENT_PINS[s], bitRead(DIGITS[n], s));
}
void setup() {
for (int s = 0; s < 7; s++) pinMode(SEGMENT_PINS[s], OUTPUT);
pinMode(DOT_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: seven segments on pins 4 to 11");
}
void loop() {
for (int n = 0; n <= 9; n++) {
show(n);
digitalWrite(DOT_PIN, n % 2);
Serial.println(n);
delay(1000);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
show sketch.ino
It takes a number n and loops over the seven segments. For each one, bitRead pulls that bit out of the DIGITS byte, and digitalWrite sets the matching pin from SEGMENT_PINS. A 1 lights the segment and a 0 turns it off.
void show(int n) {
for (int s = 0; s < 7; s++) digitalWrite(SEGMENT_PINS[s], bitRead(DIGITS[n], s));
}setup sketch.ino
It makes all seven segment pins outputs with a for loop, then does the same for DOT_PIN. After that it opens serial at 9600 and prints a start message.
void setup() {
for (int s = 0; s < 7; s++) pinMode(SEGMENT_PINS[s], OUTPUT);
pinMode(DOT_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: seven segments on pins 4 to 11");
}loop sketch.ino
It counts n from 0 to 9. Each time it calls show(n), sets the dot to n % 2 so it lights on odd digits, prints the number, and waits one second. Then it starts again at 0.
void loop() {
for (int n = 0; n <= 9; n++) {
show(n);
digitalWrite(DOT_PIN, n % 2);
Serial.println(n);
delay(1000);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A segment is missing or the wrong one lights This project
Check each pin against the list: a is pin 7, b 6, c 5, d 11, e 10, f 8, g 9. They are not in order. - Nothing lights This project
This display is common cathode. The shared pin must go through the resistor to GND, not to 5V. - Segments stay dark, or light when they should be off. Seven segment display
Check whether the display is common cathode or common anode. Common cathode: common pin to GND, drive segments HIGH. Common anode: common pin to VCC, drive segments LOW. - One segment is very bright or the LEDs burn out. Seven segment display
Each segment is an LED, so it needs its own resistor, about 220 ohm to 330 ohm on 5 V. One resistor on the common pin makes brightness change with the number of lit segments. - 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. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.