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 Runs in your browser. Free, and no account needed.

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.

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);
  }
}

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 DIG1
  • Arduino Uno R3 pin 12; Four-digit seven segment, multiplexed pin DIG2
  • Arduino Uno R3 pin 11; Four-digit seven segment, multiplexed pin DIG3
  • Arduino Uno R3 pin 10; Four-digit seven segment, multiplexed pin DIG4
  • 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.