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 Runs in your browser. Free, and no account needed.
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);
}
}
Parts list
4 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
10 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Resistor, 220 Ω pin 2
- Arduino Uno R3 pin 11; Seven segment display pin D
- Arduino Uno R3 pin 10; Seven segment display pin E
- Arduino Uno R3 pin 9; Seven segment display pin G
- Arduino Uno R3 pin 8; Seven segment display pin F
- Arduino Uno R3 pin 7; Seven segment display pin A
- Arduino Uno R3 pin 6; Seven segment display pin B
- Arduino Uno R3 pin 5; Seven segment display pin C
- Arduino Uno R3 pin 4; Seven segment display pin DP
- Seven segment display pin COM1; Resistor, 220 Ω pin 1
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.