Arduino stopwatch with LCD display

This is a simple stopwatch project using Arduino and an LCD display which can be used to measure the amount of time that elapses between the pressing of start and stop buttons.

Refer: Arduino interface with LCD module

Here “millis()” an inbuilt function of the Arduino is used to get the time value. The millis() function returns the value of time in milliseconds that passed since the Arduino board is turned ON.

In the above circuit, it has two input push switches one for START and the other for the STOP function. When the start button is pressed the stopwatch starts to count in seconds it also displays 1/100 of a second (centisecond – unit of time equal to 0.01 seconds) between each second. The seconds and 1/100 of the seconds are obtained by converting the millisecond’s values using the two formulas in the code. When the stop button is pressed the stopwatch display instantly stops counting at the moment with time in seconds and 1/100 of the second.

The display of the stopwatch can be modified to the required time or counting formats.

Refer: Countdown display time format

When the start button is pressed again the stopwatch count again from 0. The millis() function cannot be reset, it always continues since the board is turned ON. So here we subtract the instant time value when the switch is pressed from the milli() value which gives the time after since the START button is pressed.

Code 1

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //16x2 display

const int startpin = 2, stoppin = 3;
String stopwatch = "stop";
long start_time = 0;
void setup()
{
  pinMode(startpin, INPUT);
  pinMode(stoppin, INPUT);
  lcd.init();// initialize the lcd
  lcd.backlight();// Backlight ON
  lcd.setCursor(0, 1);
  lcd.print("Press START");
}

void loop()
{
  if (stopwatch == "run") {
    //Display time
    lcd.setCursor(0, 0);
    long  seconds = (millis() - start_time) / 1000;
    long  centisecond = ((millis() - start_time) / 10) % 100;
    lcd.print(seconds);
    lcd.print(" : ");
    lcd.print(centisecond);
  }
  if (digitalRead(stoppin) == HIGH) {
    //Stop
    stopwatch = "stop";
    lcd.setCursor(0, 1);
    lcd.print("Press START");
  }
  else if (digitalRead(startpin) == HIGH && stopwatch == "stop") {
    // Start
    stopwatch = "run";
    start_time = millis();
    lcd.clear();
  }
  delay(10);
}

The above code1 was a simple start and stop (Reset), there was no option to PAUSE/RUN the stopwatch. Below is an improvised code2 that has the function for an additional PAUSE/RUN feature.

In this circuit press, the start switch at pin 3 to start the stopwatch. If the start switch is pressed again, it pauses the counting, and to resume the counting press again the same switch. To reset the counter, press the stop switch.

The push-button 3 works like a latching switch that performs opposite actions for the alternate press; Refer: Latch switch using Arduino.

Code 2

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //16x2 display

const int start_pause_pin = 2, resetpin = 3;
String stopwatch = "stop", start_pause = "low";
long start_time = 0, seconds = 0;
void setup()
{
  pinMode(start_pause_pin, INPUT);
  pinMode(resetpin, INPUT);
  lcd.init();// initialize the lcd
  lcd.backlight();// Backlight ON
  lcd.setCursor(0, 1);
  lcd.print("Press START");
}

void loop() {
  if (stopwatch == "run") {
    lcd.setCursor(0, 0);
    seconds = (millis() - start_time) / 1000;
    long  centisecond = ((millis() - start_time) / 10) % 100;
    lcd.print(seconds);
    lcd.print(" : ");
    lcd.print(centisecond);
  }
  if (digitalRead(resetpin) == HIGH) {
    //Reset stopwatch
    stopwatch = "stop";
    start_time = 0;
    lcd.setCursor(0, 1);
    lcd.print("Press START");
  }
  if (digitalRead(start_pause_pin) == HIGH && stopwatch == "stop") {
    //Start stopwatch
    start_time = millis();
    lcd.clear();
    stopwatch = "run";
    start_pause = "high";
  }
  else if (digitalRead(start_pause_pin) == HIGH && stopwatch == "run" && start_pause == "low") {
    //Pause stopwatch
    stopwatch = "pause";
    start_pause = "high";
  }
  else if (digitalRead(start_pause_pin) == HIGH && stopwatch == "pause" && start_pause == "low") {
    //Resume stopwatch
    start_time = millis() - (seconds * 1000);
    stopwatch = "run";
    start_pause = "high";
    lcd.clear();

  }
  else if (digitalRead(start_pause_pin) == LOW) {
    start_pause = "low";
  }
  delay(10);
}

In this code, the RESET or stop state of the stopwatch will be indicated by the text “Press START”. Any additional Text indications like START, STOP, PAUSE, RESUME, etc. can be added to the code for easy understanding of the current state of the stopwatch.

Leave a Reply

Your email address will not be published. Required fields are marked *