Built-in project · Arduino Uno R3

Countdown timer on a four-digit display, on an Arduino Uno

A kitchen timer: a 74HC595 (pins 8, 13 and 12) and four digit lines on pins 2 to 5 show minutes and seconds. Buttons on A3, 6 and A1 set and start it, and a buzzer on A2 sounds when it runs out. The whole build, an Arduino Uno and 14 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 60 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Multiplexing: lighting digits one at a time
  • Shift registers and the latch pin
  • Seven segment bit patterns for the digits 0 to 9
  • A state machine with setting, running and ringing
  • Why loop code must not use long delays
  • Difficulty: Advanced
  • Time: About 60 minutes
  • Board: Arduino Uno R3
  • Parts: 16

How it works

A four-digit display shares its segment lines between all four digits. A 74HC595 shift register (data on pin 8, clock on 13, latch on 12) sends the segment pattern. Pins 2 to 5 pick which digit is on. The sketch lights one digit at a time, so fast that all four look lit.

Three buttons set the timer: minute on A3, second on pin 6 and function on A1. Function starts the countdown, and pressing it again clears it. The sketch also counts down 5 seconds on its own at startup.

At zero, the buzzer on A2 and the red LED on A0 pulse four times a second until you press function.

Kits that can build it

These 5 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 pinConnects to
74HC595 shift register pin GNDArduino Uno R3 pin GND
74HC595 shift register pin OEArduino Uno R3 pin GND
Pushbutton (1) pin 2bArduino Uno R3 pin GND
Pushbutton (2) pin 2bArduino Uno R3 pin GND
Pushbutton (3) pin 2aArduino Uno R3 pin GND
Transistor (NPN or PNP) pin EArduino Uno R3 pin GND
Buzzer pin 2Arduino Uno R3 pin GND
74HC595 shift register pin SHCPArduino Uno R3 pin 13
74HC595 shift register pin STCPArduino Uno R3 pin 12
74HC595 shift register pin DSArduino Uno R3 pin 8
Pushbutton (3) pin 1aArduino Uno R3 pin 6
Resistor, 220 Ω (4) pin 2Arduino Uno R3 pin 5
Resistor, 220 Ω (3) pin 2Arduino Uno R3 pin 4
Resistor, 220 Ω (2) pin 2Arduino Uno R3 pin 3
Resistor, 220 Ω (1) pin 2Arduino Uno R3 pin 2
74HC595 shift register pin MRArduino Uno R3 pin 5V
74HC595 shift register pin VCCArduino Uno R3 pin 5V
LED, red pin AArduino Uno R3 pin 5V
Resistor, 220 Ω (5) pin 2Arduino Uno R3 pin A0
Pushbutton (2) pin 1bArduino Uno R3 pin A1
Buzzer pin 1Arduino Uno R3 pin A2
Pushbutton (1) pin 1bArduino Uno R3 pin A3
Four-digit seven segment, multiplexed pin B74HC595 shift register pin Q1
Four-digit seven segment, multiplexed pin C74HC595 shift register pin Q2
Four-digit seven segment, multiplexed pin D74HC595 shift register pin Q3
Four-digit seven segment, multiplexed pin E74HC595 shift register pin Q4
Four-digit seven segment, multiplexed pin F74HC595 shift register pin Q5
Four-digit seven segment, multiplexed pin G74HC595 shift register pin Q6
Four-digit seven segment, multiplexed pin DP74HC595 shift register pin Q7
Four-digit seven segment, multiplexed pin A74HC595 shift register pin Q0
Resistor, 220 Ω (4) pin 1Four-digit seven segment, multiplexed pin DIG4
Resistor, 220 Ω (3) pin 1Four-digit seven segment, multiplexed pin DIG3
Resistor, 220 Ω (2) pin 1Four-digit seven segment, multiplexed pin DIG2
Resistor, 220 Ω (1) pin 1Four-digit seven segment, multiplexed pin DIG1
Resistor, 10k Ω pin 1LED, red pin C
Transistor (NPN or PNP) pin CResistor, 10k Ω pin 2
Resistor, 220 Ω (5) pin 1Transistor (NPN or PNP) pin B

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 countdown timer: set it with two buttons, start it with a third.
//
//   74HC595 DS -> pin 8    SHCP -> pin 13    STCP -> pin 12
//   Q0 to Q7 -> segments a to g and the dot of a four-digit display
//   digit commons DIG1 to DIG4 -> pins 2, 3, 4, 5 (LOW lights a digit)
//   "minute" button  -> A3, to GND    "second" button -> pin 6, to GND
//   "function" button -> A1, to GND   (all three on INPUT_PULLUP)
//   LED -> A0: 220 ohm into an S8050's base, the S8050 sinks the LED from 5 V
//   through 10k                       buzzer -> A2
//
// The four digits share their segment lines, so the sketch lights one digit
// at a time, a few milliseconds each, round and round: fast enough that you
// see all four at once. That is multiplexing, and it is why the display code
// runs on every pass of loop() and nothing in loop() may delay() for long.
//
// Press "minute" to add a minute and "second" to add a second. Press
// "function" to start; press it again to clear. When the time runs out the
// buzzer sounds and the LED flashes until you press "function".
//
// The analog pins work as ordinary digital pins: A0 to A5 are pins 14 to 19.

const int DATA_PIN = 8;
const int LATCH_PIN = 12;
const int CLOCK_PIN = 13;
const int DIGIT_PINS[4] = {2, 3, 4, 5};
const int BUTTON_MINUTE = A3;
const int BUTTON_SECOND = 6;
const int BUTTON_FUNCTION = A1;
const int LED = A0;
const int BUZZER = A2;

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

enum State { SETTING, RUNNING, RINGING };
State state = SETTING;
long remaining = 0;  // seconds
unsigned long lastTick = 0;
int digit = 0;

struct Button {
  int pin;
  bool down;
  unsigned long changedAt;
};
Button buttons[3] = {{BUTTON_MINUTE, false, 0}, {BUTTON_SECOND, false, 0}, {BUTTON_FUNCTION, false, 0}};

// True once per press, after the contact has settled for 30 ms.
bool pressed(Button &b) {
  bool now = digitalRead(b.pin) == LOW;
  if (now != b.down && millis() - b.changedAt > 30) {
    b.down = now;
    b.changedAt = millis();
    return now;
  }
  return false;
}

void showDigit(int which, byte pattern) {
  for (int i = 0; i < 4; i++) digitalWrite(DIGIT_PINS[i], HIGH);
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);
  digitalWrite(LATCH_PIN, HIGH);
  digitalWrite(DIGIT_PINS[which], LOW);
}

void refresh() {
  int minutes = remaining / 60;
  int seconds = remaining % 60;
  byte patterns[4] = {DIGITS[minutes / 10 % 10], (byte)(DIGITS[minutes % 10] | 0x80), DIGITS[seconds / 10],
                      DIGITS[seconds % 10]};
  showDigit(digit, patterns[digit]);
  digit = (digit + 1) % 4;
}

void printTime() {
  Serial.print(remaining / 60);
  Serial.print(remaining % 60 < 10 ? ":0" : ":");
  Serial.println(remaining % 60);
}

void setup() {
  pinMode(DATA_PIN, OUTPUT);
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  for (int i = 0; i < 4; i++) pinMode(DIGIT_PINS[i], OUTPUT);
  pinMode(BUTTON_MINUTE, INPUT_PULLUP);
  pinMode(BUTTON_SECOND, INPUT_PULLUP);
  pinMode(BUTTON_FUNCTION, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: countdown timer. minute (A3) and second (6) set it, function (A1) starts it.");
  // Something to count down from straight away, so it is not a blank display.
  remaining = 5;
  state = RUNNING;
  lastTick = millis();
  Serial.println("start: 0:05");
}

void loop() {
  bool minute = pressed(buttons[0]);
  bool second = pressed(buttons[1]);
  bool function = pressed(buttons[2]);

  if (state == SETTING) {
    if (minute && remaining < 99L * 60) remaining += 60;
    if (second && remaining < 99L * 60 + 59) remaining += 1;
    if (minute || second) printTime();
    if (function && remaining > 0) {
      state = RUNNING;
      lastTick = millis();
      Serial.println("start");
    }
  } else if (state == RUNNING) {
    if (function) {
      state = SETTING;
      remaining = 0;
      Serial.println("cleared");
    } else if (millis() - lastTick >= 1000) {
      lastTick += 1000;
      remaining--;
      printTime();
      if (remaining <= 0) {
        remaining = 0;
        state = RINGING;
        Serial.println("time is up");
      }
    }
  } else {
    bool on = (millis() / 250) % 2 == 0;
    digitalWrite(LED, on ? HIGH : LOW);
    digitalWrite(BUZZER, on ? HIGH : LOW);
    if (function) {
      state = SETTING;
      digitalWrite(LED, LOW);
      digitalWrite(BUZZER, LOW);
      Serial.println("quiet");
    }
  }

  refresh();
  delay(3);
}

Code walkthrough

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

pressed sketch.ino

Checks one button. It returns true only once per press: when the pin reads LOW and 30 ms have passed since the last change. That ignores contact bounce and stops one press from counting many times.

// True once per press, after the contact has settled for 30 ms.
bool pressed(Button &b) {
  bool now = digitalRead(b.pin) == LOW;
  if (now != b.down && millis() - b.changedAt > 30) {
    b.down = now;
    b.changedAt = millis();
    return now;
  }
  return false;
}

showDigit sketch.ino

Turns all four digit pins HIGH (off), then shifts the segment pattern into the 74HC595 and latches it. Last, it pulls the chosen digit pin LOW to light that digit.

void showDigit(int which, byte pattern) {
  for (int i = 0; i < 4; i++) digitalWrite(DIGIT_PINS[i], HIGH);
  digitalWrite(LATCH_PIN, LOW);
  shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, pattern);
  digitalWrite(LATCH_PIN, HIGH);
  digitalWrite(DIGIT_PINS[which], LOW);
}

refresh sketch.ino

Splits remaining into minutes and seconds and builds the four patterns. The second digit gets the dot, which acts as the colon. It shows only the current digit and moves on to the next, so every pass of loop lights one digit.

void refresh() {
  int minutes = remaining / 60;
  int seconds = remaining % 60;
  byte patterns[4] = {DIGITS[minutes / 10 % 10], (byte)(DIGITS[minutes % 10] | 0x80), DIGITS[seconds / 10],
                      DIGITS[seconds % 10]};
  showDigit(digit, patterns[digit]);
  digit = (digit + 1) % 4;
}

loop sketch.ino

Reads the buttons, then acts by state. In setting, buttons add time. In running, it removes one second every 1000 ms. In ringing, it blinks the LED and buzzer. Every pass ends with refresh and a 3 ms delay.

void loop() {
  bool minute = pressed(buttons[0]);
  bool second = pressed(buttons[1]);
  bool function = pressed(buttons[2]);

  if (state == SETTING) {
    if (minute && remaining < 99L * 60) remaining += 60;
    if (second && remaining < 99L * 60 + 59) remaining += 1;
    if (minute || second) printTime();
    if (function && remaining > 0) {
      state = RUNNING;
      lastTick = millis();
      Serial.println("start");
    }
  } else if (state == RUNNING) {
    if (function) {
      state = SETTING;
      remaining = 0;
      Serial.println("cleared");
    } else if (millis() - lastTick >= 1000) {
      lastTick += 1000;
      remaining--;
      printTime();
      if (remaining <= 0) {
        remaining = 0;
        state = RINGING;
        Serial.println("time is up");
      }
  // ... 16 more lines in the editor

Troubleshooting

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

  • Only one digit shows, or the digits look dim or flicker This project
    Every pass of loop must reach refresh. A long delay or other slow code makes the display flicker.
  • Wrong segments light up This project
    Check the shift register outputs Q0 to Q7 go to segments A to G and DP in order.
  • Buttons do not respond This project
    Each button goes from its pin to ground, because the sketch uses INPUT_PULLUP.
  • Outputs show garbage, or the register never clears. 74HC595 shift register
    Tie OE low and SRCLR (pin 10) high. Left floating, these pins make the chip behave randomly. Then pulse the latch pin after shifting the byte.
  • The bits come out in the wrong order, or LEDs are dim. 74HC595 shift register
    Choose MSBFIRST or LSBFIRST in shiftOut to flip the order. Give each LED its own resistor, since each output can source only about 6 mA safely.
  • 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.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.