Built-in project · Arduino Uno R3

Stopwatch on a four-digit display, on an Arduino Uno

A four-digit display wired straight to the Uno, segments a to dp on pins 2 to 9 and digits 1 to 4 on pins 13 to 10: it counts the seconds since reset, multiplexed. 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.

Teaching? Assign it to your class in one click.

Advanced About 30 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Stopwatch on a four-digit display · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Multiplexing: sharing segment wires between digits
  • Common cathode digit selection with LOW
  • Splitting a number into digits with / and %
  • Reading time with millis
  • Difficulty: Advanced
  • Time: About 30 minutes
  • Board: Arduino Uno R3
  • Parts: 11

How it works

A four-digit display has only one set of segment wires shared by all four digits. Segments a to g and the dot go through eight 220 ohm resistors to pins 2 to 9. Each digit also has its own common pin, wired to pins 13, 12, 11 and 10 for the left to the right digit.

A digit lights only while its common pin is LOW. So the sketch shows one digit for 4 ms, then the next, and so on, round and round. Your eye sees all four at once. The number shown is the seconds since the Uno was reset, and it wraps back to 0 after 9999.

Parts list

11 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

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 pinConnects to
Four-digit seven segment, multiplexed pin DIG1Arduino Uno R3 pin 13
Four-digit seven segment, multiplexed pin DIG2Arduino Uno R3 pin 12
Four-digit seven segment, multiplexed pin DIG3Arduino Uno R3 pin 11
Four-digit seven segment, multiplexed pin DIG4Arduino Uno R3 pin 10
Resistor, 220 Ω (8) pin 1Arduino Uno R3 pin 9
Resistor, 220 Ω (7) pin 1Arduino Uno R3 pin 8
Resistor, 220 Ω (6) pin 1Arduino Uno R3 pin 7
Resistor, 220 Ω (5) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (4) pin 1Arduino Uno R3 pin 5
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 4
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 3
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 2
Resistor, 220 Ω (5) pin 2Four-digit seven segment, multiplexed pin E
Resistor, 220 Ω (4) pin 2Four-digit seven segment, multiplexed pin D
Resistor, 220 Ω (8) pin 2Four-digit seven segment, multiplexed pin DP
Resistor, 220 Ω (3) pin 2Four-digit seven segment, multiplexed pin C
Resistor, 220 Ω (7) pin 2Four-digit seven segment, multiplexed pin G
Resistor, 220 Ω (2) pin 2Four-digit seven segment, multiplexed pin B
Resistor, 220 Ω (6) pin 2Four-digit seven segment, multiplexed pin F
Resistor, 220 Ω (1) pin 2Four-digit seven segment, multiplexed pin A

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A stopwatch: the seconds since reset on a four-digit display.
//
//   segments a, b, c, d, e, f, g, dp -> pins 2 to 9, each through 220 ohm
//   digit commons DIG1, DIG2, DIG3, DIG4 -> pins 13, 12, 11, 10
//
// The four digits share their segment wires. Each digit lights only while
// its common cathode is LOW, so the sketch lights one digit at a time, a few
// milliseconds each: fast enough that your eye sees all four together.
// That is multiplexing.

const int SEGMENTS[8] = {2, 3, 4, 5, 6, 7, 8, 9};  // a to g, then dp
const int DIGITS[4] = {13, 12, 11, 10};          // DIG1 (left) to DIG4

//                      gfedcba
const byte PATTERNS[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

unsigned long shownSecond = 0;

void showDigit(int position, int value) {
  for (int d = 0; d < 4; d++) digitalWrite(DIGITS[d], HIGH);
  for (int s = 0; s < 8; s++) digitalWrite(SEGMENTS[s], bitRead(PATTERNS[value], s));
  digitalWrite(DIGITS[position], LOW);
}

void setup() {
  for (int s = 0; s < 8; s++) pinMode(SEGMENTS[s], OUTPUT);
  for (int d = 0; d < 4; d++) {
    pinMode(DIGITS[d], OUTPUT);
    digitalWrite(DIGITS[d], HIGH);
  }
  Serial.begin(9600);
  Serial.println("Mokxi Uno: stopwatch");
}

void loop() {
  unsigned long seconds = millis() / 1000;
  int n = seconds % 10000;
  int values[4] = {n / 1000, n / 100 % 10, n / 10 % 10, n % 10};
  for (int position = 0; position < 4; position++) {
    showDigit(position, values[position]);
    delay(4);
  }
  if (seconds != shownSecond) {
    shownSecond = seconds;
    Serial.println(seconds);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

showDigit sketch.ino

Lights one digit. It first turns all four digit commons HIGH, which switches every digit off. Then it writes the eight segment pins from the bits of that number's pattern. Last, it pulls the chosen digit's common LOW, so only that digit shows its number.

void showDigit(int position, int value) {
  for (int d = 0; d < 4; d++) digitalWrite(DIGITS[d], HIGH);
  for (int s = 0; s < 8; s++) digitalWrite(SEGMENTS[s], bitRead(PATTERNS[value], s));
  digitalWrite(DIGITS[position], LOW);
}

setup sketch.ino

Sets the eight segment pins and four digit pins as outputs, and starts with every digit common HIGH so the display is dark. Then it opens serial at 9600.

void setup() {
  for (int s = 0; s < 8; s++) pinMode(SEGMENTS[s], OUTPUT);
  for (int d = 0; d < 4; d++) {
    pinMode(DIGITS[d], OUTPUT);
    digitalWrite(DIGITS[d], HIGH);
  }
  Serial.begin(9600);
  Serial.println("Mokxi Uno: stopwatch");
}

loop sketch.ino

Turns millis() / 1000 into seconds, keeps it under 10000, and splits it into thousands, hundreds, tens and ones. A for loop shows each digit with showDigit and waits 4 ms. When the second changes, it also prints the seconds to serial.

void loop() {
  unsigned long seconds = millis() / 1000;
  int n = seconds % 10000;
  int values[4] = {n / 1000, n / 100 % 10, n / 10 % 10, n % 10};
  for (int position = 0; position < 4; position++) {
    showDigit(position, values[position]);
    delay(4);
  }
  if (seconds != shownSecond) {
    shownSecond = seconds;
    Serial.println(seconds);
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The digits flicker or look uneven. This project
    The delay(4) after each digit sets the refresh. A much longer delay makes the flicker visible, so keep it short and do not add other delays in loop.
  • Digits appear in the wrong places. This project
    DIG1 (pin 13) is the leftmost digit and DIG4 (pin 10) the rightmost. Check these wires.
  • Digits look dim, flicker, or show ghosts of other digits. Four-digit seven segment
    Refresh each digit quickly in a loop, at least 100 Hz for the whole set. Turn all segments off before switching digits. Do not use delay() for long pauses while displaying.
  • Segments burn out or the pins are overloaded. Four-digit seven segment
    Use a resistor of about 220 ohms on each segment line. Check whether the display is common cathode or common anode, since that sets which level turns a segment on.
  • 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.