Projects / Stopwatch with lap times

Stopwatch with lap times

Start, stop and lap buttons control a stopwatch that stores the last five lap times and marks the best lap.

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNOHelloPCF8574

    Scan the LCD address

    Run an I2C scanner, note 0x27 or 0x3F.

  2. UNO

    Wire the three buttons

    D2, D3 and D4 to GND.

  3. UNOHelloPCF8574

    Wire the LCD

    SDA and SCL on A4 and A5.

  4. UNO

    Upload the sketch

    Display shows 00:00.0 STOP.

  5. UNO

    Press Start

    Time counts up.

  6. UNO

    Press Lap a few times

    Lap numbers appear and print on the serial monitor.

  7. UNO

    Compare against a phone stopwatch

    Run for two minutes.

  8. UNO

    Press Stop then Reset

    Everything clears.

In short

How do you build a stopwatch with lap times?

Start, stop and lap buttons control a stopwatch that stores the last five lap times and marks the best lap. You need Arduino Uno R3, Tactile push button 6 mm × 3, Resistor 10 kΩ × 3, 16x2 LCD with I2C backpack (PCF8574), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Start and stop buttonUno D2 to GNDInternal pull-up, pressed reads LOW
Lap buttonUno D3 to GNDRecords a lap
Reset buttonUno D4 to GNDClears everything when stopped
LCD I2C SDA and SCLUno A4 and A5Display
LCD I2C VCC and GNDUno 5V and GNDPower
Optional 10 k resistorsFrom each button pin to 5VNot needed, the sketch uses the internal pull-ups

Code

The Uno counts time with millis(), which is accurate to about a few seconds per day on a standard board crystal, plenty for a hand-held stopwatch. The start button toggles running and stopping and keeps the time already counted, so you can pause and continue. The lap button saves the time since the previous lap into a small array that keeps the last five, tracks the best (shortest) lap, and also prints each lap with a best marker on the serial monitor so you can copy the results. The reset button only works while stopped, to prevent accidents. The display redraws ten times a second, which is smooth enough to read and light on the processor. A press is detected on the change from HIGH to LOW, so holding a button down counts only once, and a short delay after each press removes switch bounce. The internal pull-ups mean no external resistors are required. Your own reaction time adds about a tenth of a second to each press, so for exact timing use sensors, such as an IR beam, in a later version. Setup: Arduino IDE, board "Arduino Uno". Install LiquidCrystal_I2C.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const uint8_t B_START = 2, B_LAP = 3, B_RESET = 4;
LiquidCrystal_I2C lcd(0x27, 16, 2);          // use 0x3F if the screen stays blank

bool running = false;
unsigned long startedAt = 0, elapsedBefore = 0, lastLapAt = 0;
unsigned long laps[5];
uint8_t lapCount = 0;
unsigned long bestLap = 0;
bool prev[3] = {HIGH, HIGH, HIGH};
unsigned long lastDraw = 0;

unsigned long elapsed() { return running ? elapsedBefore + (millis() - startedAt) : elapsedBefore; }

void fmt(unsigned long ms, char *out) {       // out must hold at least 9 chars: MM:SS.T
  unsigned long t = ms / 100;                 // tenths of a second
  unsigned int tenth = t % 10;
  unsigned long s = t / 10;
  sprintf(out, "%02u:%02u.%u", (unsigned int)(s / 60 % 100), (unsigned int)(s % 60), tenth);
}

bool pressed(uint8_t idx, uint8_t pin) {      // true once per press
  bool now = digitalRead(pin);
  bool hit = (prev[idx] == HIGH && now == LOW);
  prev[idx] = now;
  if (hit) delay(25);                         // simple debounce
  return hit;
}

void draw() {
  char buf[10];
  fmt(elapsed(), buf);
  lcd.setCursor(0, 0);
  lcd.print(buf);
  lcd.print(running ? " RUN  " : " STOP ");
  lcd.setCursor(0, 1);
  if (lapCount == 0) {
    lcd.print("No laps yet     ");
  } else {
    fmt(laps[(lapCount - 1) % 5], buf);
    lcd.print("L"); lcd.print(lapCount); lcd.print(' ');
    lcd.print(buf);
    lcd.print("     ");
  }
}

void setup() {
  pinMode(B_START, INPUT_PULLUP);
  pinMode(B_LAP, INPUT_PULLUP);
  pinMode(B_RESET, INPUT_PULLUP);
  lcd.init(); lcd.backlight();
  Serial.begin(9600);
  draw();
}

void loop() {
  if (pressed(0, B_START)) {
    if (running) { elapsedBefore = elapsed(); running = false; }
    else { startedAt = millis(); running = true; }
  }
  if (pressed(1, B_LAP) && running) {
    unsigned long now = elapsed();
    unsigned long lap = now - lastLapAt;
    lastLapAt = now;
    laps[lapCount % 5] = lap;                 // keeps the last five
    lapCount++;
    if (bestLap == 0 || lap < bestLap) bestLap = lap;
    char buf[10];
    fmt(lap, buf);
    Serial.print("Lap "); Serial.print(lapCount); Serial.print(": "); Serial.print(buf);
    if (lap == bestLap) Serial.print("  (best)");
    Serial.println();
  }
  if (pressed(2, B_RESET) && !running) {
    elapsedBefore = 0; lastLapAt = 0; lapCount = 0; bestLap = 0;
    lcd.clear();
  }
  if (millis() - lastDraw > 100) { lastDraw = millis(); draw(); }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Start, wait 60 secondsAbout 01:00.0
Press Lap three timesL1, L2, L3 on the second line
Stop, then StartContinues from the stopped time
Press Reset while runningNothing happens
Stop, Reset00:00.0 and No laps yet

If it does not work

LCD blank

Try address 0x3F, or turn the contrast screw.

Buttons double count

Increase the debounce delay, or check for loose wires.

Time runs off compared with a phone

Check for long delay() calls in changes you made.

Garbage characters

The row text was too long, check the padding spaces.

Laps do not register

Lap only works while running.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the stopwatch with lap times?

Arduino Uno R3, Tactile push button 6 mm × 3, Resistor 10 kΩ × 3, 16x2 LCD with I2C backpack (PCF8574), Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the stopwatch with lap times take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the stopwatch with lap times works?

Start, wait 60 seconds: About 01:00.0. Press Lap three times: L1, L2, L3 on the second line. Stop, then Start: Continues from the stopped time.

LCD blank: what should I check?

Try address 0x3F, or turn the contrast screw.

Buttons double count: what should I check?

Increase the debounce delay, or check for loose wires.

Time runs off compared with a phone: what should I check?

Check for long delay() calls in changes you made.

Garbage characters: what should I check?

The row text was too long, check the padding spaces.