How to Create a Countdown Timer with Arduino

A timer is a type of clock that is usually used to measure time intervals. There are two types of timers: one counts up from zero to measure elapsed time, known as a stopwatch, while the second type counts down from a specified duration provided by the user, commonly referred to as a countdown timer.

In this tutorial, we will show you how to create a countdown timer using the Arduino development board. In this article, we do not use any real-time clock (RTC) modules to keep track of time. The duration is set by the user via a keypad and displayed on a 1602 LCD. An alarm sound will be emitted through a buzzer when the timer reaches zero.

Materials Required

● Arduino UNO development board

● LCD display module 1602

● 4 * 4 matrix keypad

● Buzzer

● Button

● Potentiometer (10k)

● Resistors (10k, 100 ohm)

● Connecting wires

Circuit Diagram

How to Create a Countdown Timer with Arduino

Here, the Arduino Uno development board serves as the main controller. The keypad is used to set the duration, and the LCD display module 1602 is used to show the countdown. The button is used to start the timer. For how to connect the Arduino board with the 4×4 matrix keypad, please refer to: https://www.yiboard.com/thread-780-1-1.html.

Code and Explanation

At the end of this article, the complete Arduino timer code is provided.

In the code below, we are initializing the libraries for the keypad and LCD, as well as the variables used in the code.

  1. #include <LiquidCrystal.h>

  2. #include <Keypad.h>

  3. long int set1;

  4. long int set2;

  5. long int set3;

  6. long int set4;

  7. long int j;

  8. int t1, t2, t3, t4, t5, t6;

  9. int r1, r2, r3;

  10. char key;

  11. String r[8];

  12. String hours;

  13. String minutes;

  14. String seconds;

Copy Code

In the code below, we initialize the number of rows and columns to define the keypad matrix.

  1. const byte ROWS = 4; // Four rows

  2. const byte COLS = 4; // Three columns

  3. char keys[ROWS][COLS] = {

  4. {‘1′,’2′,’3′,’A’},

  5. {‘4′,’5′,’6′,’B’},

  6. {‘7′,’8′,’9′,’C’},

  7. {‘*’,’0′,’#’,’D’}

  8. };

Copy Code

To connect the 4 * 4 matrix keypad with the Arduino development board, we must define the pins used for the rows and columns. Thus, in the code below, we have defined the pins for the keypad and the 1602 module.

  1. byte rowPins[ROWS] = { 6, 7, 8, 9 }; // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins

  2. byte colPins[COLS] = { 10, 11, 12, 13 }; // Connect keypad COL0, COL1 and COL2 to these Arduino pins

  3. LiquidCrystal lcd(A0, A1, 5, 4, 3, 2); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

Copy Code

The following code declares a keypad array:

  1. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

Copy Code

In the void setFeedingTime() function, after pressing the button, we can input the timer’s time, and after input is complete, we must press ‘D’ to start the countdown.

  1. void setFeedingTime()

  2. {

  3. feed = true;

  4. int i=0;

  5. lcd.clear();

  6. lcd.setCursor(0,0);

  7. lcd.print(“Set feeding Time”);

  8. lcd.clear();

  9. lcd.print(“HH:MM:SS”);

  10. lcd.setCursor(0,1);

  11. while(1){

  12. key = kpd.getKey();

  13. char j;

  14. if(key!=NO_KEY){

  15. lcd.setCursor(j,1);

  16. lcd.print(key);

  17. r[i] = key-48;

  18. i++;

  19. j++;

  20. if (j==2 || j == 5)

  21. {

  22. lcd.print(“:”); j++;

  23. }

  24. delay(500);

  25. }

  26. if (key == ‘D’)

  27. {key=0; break; }

  28. }

  29. lcd.clear();

  30. }

Copy Code

In the void setup() function, we initialize the LCD and serial communication, and in the code below, we define the pins as INPUT and OUTPUT.

  1. void setup()

  2. {

  3. lcd.begin(16,2);

  4. Serial.begin(9600);

  5. pinMode(A0, OUTPUT);

  6. pinMode(A1, OUTPUT);

  7. pinMode(A3, INPUT);

  8. pinMode(A4, OUTPUT);

  9. }

Copy Code

The working principle of this Arduino countdown timer is quite simple, but the code is a bit complex.

Initially, it will print “Arduino Timer” on the LCD screen until you press the button. As soon as the button is pressed, it will prompt for the countdown time by calling the “setFeedingTime” function. Then you can input the duration using the keypad. After that, you need to press ‘D’ to save the time and start the countdown. In the void loop() function, we have done some calculations to decrement the time second by second and display the corresponding values of Hour, Minutes, and Seconds (HH:MM:SS) based on the remaining time. You can see the complete code below.

How to Create a Countdown Timer with Arduino

When the timer reaches zero, the buzzer starts beeping and will beep only 100 times. To stop the buzzer, press and hold the button. You can stop the timer at any time using the button.

How to Create a Countdown Timer with Arduino

For more exciting tutorials about the Arduino development board, please click “Read the Original“.

Leave a Comment

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