Arduino

How Do I Start and Stop a Timer in Arduino?

Arduino is free software that allows you to program and perform multiple functions that you can use according to your requirements. You can easily program to start or stop a timer in Arduino. In this article, you will find how to start and stop timers in Arduino.

How Do I Start and Stop a Timer in Arduino?

To start and stop a timer in Arduino, you can use the inbuilt function millis() which returns the value of time that has passed since the board has started. In this article, a practical example is given.

Creating a Stopwatch Timer in Arduino Using LCD Display and Push Buttons

The stopwatch is used to start a timer and stop a timer, So we will create a stopwatch that will start and stop a timer. There are some software and hardware requirements as given below.

Software Requirements

Arduino IDE should be installed on your PC. In the Arduino IDE, you must have LCD Library that <LiquidCrystal.h>

Hardware Requirements

  • Arduino UNO Board
  • 16×2 LCD Display
  • Two Push Buttons
  • Connecting Wires
  • Breadboard

Circuit Diagram

You will make the Circuit Diagram according to the connections given below.

Arduino and LCD Connections

The interfacing of the Arduino and the LCD will be done according to the following connections.

Arduino Pins LCD Pins
5V Vs
7 RS
6 E
5 D4
4 D5
3 D6
2 D7
5V A
GND K

Arduino and Push Button Connection

The Arduino and push buttons will be connected according to the given pin connections.

Arduino Pins Push Buttons Pins
9 The input of Push Button 1
8 The input of Push Button 2
GND GND of both Push Buttons

Power Supply to the Circuit

The power supply to the circuit will be given through the default method of connecting Arduino to the PC through a USB cable connector.

Code

After making the circuit, upload the given code in Arduino. In this code, the LCD is set up with the Arduino, and push buttons are used to give HIGH or LOW input to the Arduino so that it could start and stop the timer accordingly.

#include <LiquidCrystal.h>

 

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

 

void setup() {

  lcd.begin(16, 2);
  lcd.clear();

  Serial.begin(9600);

  pinMode(8, INPUT);
  digitalWrite(8, HIGH);
  pinMode(9, INPUT);
  digitalWrite(9, HIGH);
}
double i = 0;
double a = millis();
double c;

void loop() {
  lcd.clear();
  lcd.print("press start");
  delay(100);

  if (digitalRead(8) == LOW) {

    lcd.clear();
    a = millis();
    while (digitalRead(9) == HIGH) {

      c = millis();
      i = (c - a) / 1000;
      lcd.print(i);
      lcd.setCursor(7, 0);
      lcd.print("Sec's");
      lcd.setCursor(0, 0);
      Serial.println(c);
      Serial.println(a);
      Serial.println(i);
      Serial.println("......");
      delay(100);
    }

    if (digitalRead(9) == LOW) {
      while (digitalRead(8) == HIGH) {
        lcd.setCursor(0, 0);
        lcd.print(i);
        lcd.setCursor(11, 0);
        lcd.print("");
        lcd.setCursor(0, 0);
        delay(100);
      }
    }
  }


}

Output

Initially, Press Start is printed on the LCD screen. When Push Button 1 is pressed, the millis() function is used to calculate time and the timer starts running on the LCD screen unless the second push button is pressed, which stops the timer. If you want to reset the timer, the RESET button on the Arduino can be pressed. The output screenshots are shown below.

In the above figure, the circuit has turned ON and Press Start can be seen on the LCD screen.

When the first Push Button is pressed, the timer starts and is counting in seconds. Here it shows 2.42 seconds.

After some time, when 17 seconds have passed and the second push button is pressed, the timer stops.

Conclusion

We have acquired an understanding of how to start and stop the timer in Arduino. It is very simple to design a stopwatch timer in the Arduino using an LCD Display and two push buttons. The LCD prints the time which has passed. One push button is used to start the timer and the second push button is used to stop the timer. The Arduino RESET button can be used to reset the timer so that it starts counting the time from zero again.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.