Four-digit counter on an Arduino Uno
A bare four-digit seven-segment display on twelve pins, multiplexed: one digit at a time, four milliseconds each, and the eye sees all four. The whole build, an Arduino Uno and 9 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.
AdvancedRuns 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.
// Four-digit counter: a bare 5641AS display, multiplexed by the sketch.
//
// segments A B C D E F G DP -> pins 2 to 9, each through its own 220 ohm
// digits DIG1 to DIG4 -> pins 10 to 13
//
// Twelve legs for four digits: each segment leg goes to that segment in all
// four digits, and each digit has one common leg. So only one digit can be
// shown at a time, and the sketch shows them in turn, four milliseconds each,
// fast enough that the eye sees all four at once, each a quarter as bright.
//
// The display is common cathode: a digit is selected by pulling its common
// LOW, and a segment lit by driving its leg HIGH. Every common goes off before
// the next digit's segments change, or the old digit shows a faint ghost of
// the new one. Try moving that line and look closely.
//
// The count is tenths of a second, 000.0 to 999.9, with the point lit on the
// third digit.
const uint8_t SEGMENT_PINS[8] = {2, 3, 4, 5, 6, 7, 8, 9};
const uint8_t DIGIT_PINS[4] = {10, 11, 12, 13};
// Bit 0 is segment A, bit 6 is G, bit 7 the point.
const uint8_t FONT[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
void setup() {
for (uint8_t i = 0; i < 8; i++) pinMode(SEGMENT_PINS[i], OUTPUT);
for (uint8_t i = 0; i < 4; i++) {
pinMode(DIGIT_PINS[i], OUTPUT);
digitalWrite(DIGIT_PINS[i], HIGH); // common cathode: HIGH is off
}
}
static void show(uint8_t digit, uint8_t pattern) {
for (uint8_t d = 0; d < 4; d++) digitalWrite(DIGIT_PINS[d], HIGH); // all off first
for (uint8_t s = 0; s < 8; s++) {
digitalWrite(SEGMENT_PINS[s], (pattern >> s) & 1 ? HIGH : LOW);
}
digitalWrite(DIGIT_PINS[digit], LOW); // then this one on
delay(4);
}
void loop() {
unsigned long tenths = (millis() / 100) % 10000;
show(0, FONT[tenths / 1000]);
show(1, FONT[tenths / 100 % 10]);
show(2, (uint8_t)(FONT[tenths / 10 % 10] | 0x80));
show(3, FONT[tenths % 10]);
}
Parts list
11 parts, plus the jumper wires. Every one is in the editor's parts bin.
How it is wired
20 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.
- Arduino Uno R3 pin 13; Four-digit seven segment, multiplexed pin DIG4
- Arduino Uno R3 pin 12; Four-digit seven segment, multiplexed pin DIG3
- Arduino Uno R3 pin 11; Four-digit seven segment, multiplexed pin DIG2
- Arduino Uno R3 pin 10; Four-digit seven segment, multiplexed pin DIG1
- Arduino Uno R3 pin 9; Resistor, 220 Ω (8) pin 1
- Arduino Uno R3 pin 8; Resistor, 220 Ω (7) pin 1
- Arduino Uno R3 pin 7; Resistor, 220 Ω (6) pin 1
- Arduino Uno R3 pin 6; Resistor, 220 Ω (5) pin 1
- Arduino Uno R3 pin 5; Resistor, 220 Ω (4) pin 1
- Arduino Uno R3 pin 4; Resistor, 220 Ω (3) pin 1
- Arduino Uno R3 pin 3; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 2; Resistor, 220 Ω (1) pin 1
- Four-digit seven segment, multiplexed pin E; Resistor, 220 Ω (5) pin 2
- Four-digit seven segment, multiplexed pin D; Resistor, 220 Ω (4) pin 2
- Four-digit seven segment, multiplexed pin DP; Resistor, 220 Ω (8) pin 2
- Four-digit seven segment, multiplexed pin C; Resistor, 220 Ω (3) pin 2
- Four-digit seven segment, multiplexed pin G; Resistor, 220 Ω (7) pin 2
- Four-digit seven segment, multiplexed pin B; Resistor, 220 Ω (2) pin 2
- Four-digit seven segment, multiplexed pin F; Resistor, 220 Ω (6) pin 2
- Four-digit seven segment, multiplexed pin A; Resistor, 220 Ω (1) pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.