Arduino countdown LCD display code hour:minute:second format
This is a basic code for countdown display in the format HH:MM: SS; Hour:Minute:Second.
The code uses millis() function of the Arduino to calculate the time, the millis() returns the time in milliseconds passed since the board is ON. It is then used to subtract from the set time value and is converted to a Digital time format.
This code is with a fixed time which has to be initialized with the required values of the hour, minute, and seconds before uploading the sketch. The countdown will always start once the board is ON, if the starting of the countdown needs to be controlled it can be done by an additional input with a simple “if” condition.
Here the code is programmed to display Hour: minute: second as 00:00:00.
Refer: Arduino LCD display interface
Code
#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); long hour = 23, minute = 59, second = 59; long countdown_time = (hour*3600) + (minute * 60) + second; void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(4, 0); lcd.print("HH:MM:SS"); } void loop() { long countdowntime_seconds = countdown_time - (millis() / 1000); if (countdowntime_seconds >= 0) { long countdown_hour = countdowntime_seconds / 3600; long countdown_minute = ((countdowntime_seconds / 60)%60); long countdown_sec = countdowntime_seconds % 60; lcd.setCursor(4, 1); if (countdown_hour < 10) { lcd.print("0"); } lcd.print(countdown_hour); lcd.print(":"); if (countdown_minute < 10) { lcd.print("0"); } lcd.print(countdown_minute); lcd.print(":"); if (countdown_sec < 10) { lcd.print("0"); } lcd.print(countdown_sec); } delay(500); }
In the code, the total time provided in hours, minutes, and seconds are converted to seconds using the formula that obtains the value of the variable “countdown_time”. It is then subtracted with the seconds values obtained from the Arduino function millis() (millis() divided by 1000). Then it is again converted back to hours, minutes, and seconds using the formulas used for variables “countdown_hour”, “countdown_minute, and “countdown_sec” respectively. These values are displayed on the LCD which shows countdown in HH:MM:SS format.
Countdown timer LCD with buzzer
This code is the same as above, the only difference is an additional output to switch ON an LED or buzzer when the countdown reaches Zero.
#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); long hour = 0, minute = 10, second = 10; long countdown_time = (hour * 3600) + (minute * 60) + second; int indication = 10; //Connect LED or Buzzer to digital pin 10 void setup() { pinMode(indication, OUTPUT); lcd.init(); lcd.backlight(); lcd.setCursor(4, 0); lcd.print("HH:MM:SS"); } void loop() { long countdowntime_seconds = countdown_time - (millis() / 1000); if (countdowntime_seconds >= 0) { long countdown_hour = countdowntime_seconds / 3600; long countdown_minute = ((countdowntime_seconds / 60) % 60); long countdown_sec = countdowntime_seconds % 60; lcd.setCursor(4, 1); if (countdown_hour < 10) { lcd.print("0"); } lcd.print(countdown_hour); lcd.print(":"); if (countdown_minute < 10) { lcd.print("0"); } lcd.print(countdown_minute); lcd.print(":"); if (countdown_sec < 10) { lcd.print("0"); } lcd.print(countdown_sec); if (countdowntime_seconds == 0) { digitalWrite(indication, HIGH); } } delay(500); }
LCD countdown display with Keypad input
With the same above countdown method, an additional feature can be added to set the time via the keypad using a 4×4 or 4×3 keypad.
Refer: Arduino 4×4 keypad and LCD display
Code
#include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); #include <Keypad.h> const byte ROWS = 4; const byte COLS = 4; char keys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[ROWS] = {2, 3, 4, 5}; byte colPins[COLS] = {6, 7, 8, 9}; Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); String action; long timein[6], countdown_time = 0, initialsecond = 0; int i = 0; void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(4, 0); lcd.print("HH:MM:SS"); lcd.setCursor(4, 1); lcd.print("00:00:00"); } void loop() { char key = keypad.getKey(); if (key) { switch (key) { //If key is C set time. case 'C' : action = "set_time"; lcd.setCursor(4, 1); lcd.blink(); i = 0; break; //If key is D start countdown. case 'D' : action = "start_countdown"; lcd.noBlink(); break; default : if (action == "set_time") { i++; int c = i - 1; timein[c] = key - 48; initialsecond = 0; long hour = (timein[0] * 10) + timein[1]; long minute = (timein[2] * 10) + timein[3]; long second = (timein[4] * 10) + timein[5]; //second countdown_time = (hour * 3600) + (minute * 60) + second; lcd.print(key); if (i % 2 == 0 && i < 6) { lcd.print(":"); } break; } } } if (action == "start_countdown") { if (initialsecond == 0) { initialsecond = millis() / 1000; } long countdowntime_seconds = countdown_time - (millis() / 1000) + initialsecond; if (countdowntime_seconds >= 0) { long countdown_hour = countdowntime_seconds / 3600; long countdown_minute = ((countdowntime_seconds / 60) % 60); long countdown_sec = countdowntime_seconds % 60; lcd.setCursor(4, 1); if (countdown_hour < 10) { lcd.print("0"); } lcd.print(countdown_hour); lcd.print(":"); if (countdown_minute < 10) { lcd.print("0"); } lcd.print(countdown_minute); lcd.print(":"); if (countdown_sec < 10) { lcd.print("0"); } lcd.print(countdown_sec); } delay(500); } }
Operations to set time and start the countdown.
- Press “C” to set the time.
- Then enter time as 6 digit input, for example
For 10minutes 30 seconds enter in HHMMSS -> 001030
For 1 hour 59minutes 59seconds enter in 015959
The code can also modify to add the navigation to the LCD cursor position to select and change the hour, minute, second values separately.
- Now press D, the countdown begins from the set time.
Once the countdown began, any random button press including the number keys, C or D, D after C nothing interrupt the counting. To change counting the sequence needs to be followed as button C > time input > button D.
The same can be done using a 4×3 keypad by assigning the * and # buttons as key “C” and “D”.
Folks … I’m using the original code for a fixed 5 minute countdown timer. When the countdown hits zero, I need to count up in elapsed minutes and seconds. Has anyone done this do you know?
Thanks, Brian
In the given code the if condition “if (countdowntime_seconds >= 0)” stops the counter when it reaches zero. Remove that and use the absolute function “abs” to convert the negative counting to a positive value. A Negative sign can be also added to indicate the elapsed time.
Hi, does anybody know how to add a pause feature to the timer? Whereby the timer pauses upon achieving a certain situation and then resumes after the situation is over.
Try the below code, the if condition for pause and resume can be modified for your required condition. Currently, it pauses and resumes for alternate HIGH state at input pin 5, which can be applied using a push switch.
Hi, how to repeat the countdown, with 1 minute intervals. so every 1 minute the buzzer runs
Inside the code area, add a line as shown below,
if (countdowntime_seconds == 0) {
digitalWrite(indication, HIGH);
// Add below line
countdown_time = 60 + (millis() / 1000)
}
So it gives an countdown_time with extra 1 minute.
The buzzer has to be turned off after required ON time, it can be before or after the begining of next countdown.
hi, I’m a noob.. can anyone explain this part of the code?
if (countdowntime_seconds >= 0) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60) % 60);
long countdown_sec = countdowntime_seconds % 60;
why countdown_seconds is divided to 3600? and what about the %60?
Thanks
Here we are converting the time value in seconds to hours, minutes, and seconds.
For example, you have a time value of 10000 seconds.
1 Hour = 60 minutes; 1 minute = 60 seconds
So, 1 hour = 60 x 60 = 3600 seconds.
In 10000 seconds, 10000/3600 = 2.777sec, hence it has 2 hours and remaining minutes and seconds.
To calculate the remaining minute from 10000 seconds you have to convert it to minutes and then remove no of times it can be dividable by 60, as every 60 minutes goes under hours. 10000/60 = 166.66 minutes, divide 166 by 60 and the remainder gives the minutes (46 minutes) and the quotient is the hours; % is Modulus Operator which gives the remainder after an integer division.
To get of countdown take the reminder of whole seconds divided by 60, that is 40 seconds.
SO the total seconds 10000 is 2 hours, 46 minutes, and 40 seconds.
Hello, I don’t know if it was answered but when everything is at zero it does not reset. do you have a solution?
Could you explain the issue in more detail? (Including the code you are referring to and when the issue is happening.)
hay i want to add a DAY on it. is this correct?
#include
#include
LiquidCrystal_I2C lcd(0x27, 20, 4);
long day = 14, hour = 23, minute = 59, second = 59;//second
long countdown_time = (day*86400)+(hour*3600) + (minute * 60) + second;
void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(4, 0);
lcd.print(“HH:MM:SS”);
}
void loop() {
long countdowntime_seconds = countdown_time – (millis() / 1000);
if (countdowntime_seconds >= 0) {
long countdown_day = countdowntime_seconds / 86400;
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60)%60);
long countdown_sec = countdowntime_seconds % 60;
lcd.setCursor(4, 1);
///////////////////////////////////////Hari
if (countdown_day < 10)
{
lcd.print("0");
}
lcd.print(countdown_day);
lcd.print(":");
///////////////////////////////////////Jam
if (countdown_hour < 10)
{
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");
////////////////////////////////////////Menit
if (countdown_minute < 10)
{
lcd.print("0");
}
lcd.print(countdown_minute);
lcd.print(":");
////////////////////////////////////////Detik
if (countdown_sec < 10)
{
lcd.print("0");
}
lcd.print(countdown_sec);
}
}
Yes, your logic is correct. Test it practically.
but the hour start making hundred number. am i must devide the long countdown_hour = ((countdowntime_seconds / 3600)%24); like this?
i think i must make long countdown_hour = ((countdowntime_seconds / 3600)%24);
Yes, if you need to show only hours remaining from the complete days.
long countdown_day = countdowntime_seconds / 86400; // Number of Full days (24 hours) remaining.
long countdown_hour = ((countdowntime_seconds / 3600)%24); // Returns remaining hours of the day(<24 hour).
long countdown_hour = countdowntime_seconds / 3600; // Shows complete hours.
Thank You for this great project. According to the pretty comment that came out successfully. But how can this code come to play like (the timer code will be set, upload and save memory with EEPROM into the Arduino board) without three buttons. Counting down timer to execute a task. The time should be written in the code to upload once.
Refer : https://mechatrofice.com/arduino/arduino-store-values-using-eeprom-memory
hi, how to make it completely stop when a push button is pressed
Try setting the variable “countdown_time” to zero when the push button is pressed.
Thank you for this project! but i’m trying to extend it with a button when pressed the timers starts going, so not immediately. And when pressed it’s pauzed and pressed again it starts again
Hello, could you please make it so the timer begins to countdown from 1 hour when you press a button on pin 3 and then reset it when you press a button on pin 8 with a buzzer on pin 11? Thank you very much
Use the code under “Countdown timer LCD with buzzer”. And try the below changes
In the code, replace pin number of indication output, indication = 11; //Connect Buzzer to digital pin 11
Inside void loop replace first 2 lines with, also initialise long ontime=0; int onstatus = 0;
if(digitalRead(3) == HIGH){
ontime = millis() / 1000;
onstatus = 1;// enable if condition to display countdown
}
if(digitalRead(8) == HIGH){
onstatus = 0;// disbale countdown
lcd.clear();
}
long countdowntime_seconds = countdown_time – (millis() / 1000) + ontime;
if (countdowntime_seconds >= 0 && onstatus == 1) {
#include
#include
#include
#include
int ktcSO = 10;
int ktcCS = 11;
int ktcCLK = 12;
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);
boolean var = true;
const byte ROWS = 4;
const byte COLS = 4;
char keymap[ROWS][COLS] =
{
{‘1’, ‘2’, ‘3’, ‘A’},
{‘4’, ‘5’, ‘6’, ‘B’},
{‘7’, ‘8’, ‘9’, ‘C’},
{‘*’, ‘0’, ‘#’, ‘D’}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int cursorColumn = 0;
String inputString;
String Inputstr;
long Data;
long Input;
#define RT0 10000 // Ω
#define B 3950 // K
float a = A0;
#define VCC 5 //Supply voltage
#define R 10000 //R=10KΩ
float RT, VR, ln, Temp, T0, Read;
const int Solenoid = 13;
String action;
long timein[6], countdown_time = 0, initialsecond = 0;
int i = 0;
void setup()
{
lcd.backlight();
lcd.init();
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(Solenoid, OUTPUT);
T0 = 25 + 273.15;
lcd.print(“SS Fab “);
lcd.setCursor(0, 1);
lcd.print(“Metal Services”);
delay(3000);
lcd.clear();
}
void loop()
{
char Keypressed = myKeypad.getKey();
if (Keypressed) {
switch (Keypressed) {
//If key is C set time.
case ‘C’ :
action = “set_time”;
lcd.setCursor(4, 1);
lcd.blink();
i = 0;
break;
//If key is D start countdown.
case ‘D’ :
action = “start_countdown”;
lcd.noBlink();
break;
default :
if (action == “set_time”) {
i++;
int c = i – 1;
timein[c] = Keypressed – 48;
initialsecond = 0;
long hour = (timein[0] * 10) + timein[1];
long minute = (timein[2] * 10) + timein[3];
long second = (timein[4] * 10) + timein[5]; //second
countdown_time = (hour * 3600) + (minute * 60) + second;
lcd.print(Keypressed);
if (i % 2 == 0 && i = 0) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60) % 60);
long countdown_sec = countdowntime_seconds % 60;
lcd.setCursor(4, 1);
if (countdown_hour < 10) {
}
lcd.print(countdown_hour);
lcd.print(":");
if (countdown_minute < 10) {
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec = ‘0’ && Keypressed 0);
{
Data = inputString.toInt(); // YOU GOT AN INTEGER NUMBER
inputString = “”; // clear input
lcd.clear();
lcd.print(“temperature set”);
lcd.setCursor(0, 1);
lcd.print(Data);
lcd.print(” c”);
}
}
}
if (Data = Temp)
{
digitalWrite(Solenoid, HIGH);
delay(200);
}
}
Not sure but you can try a few changes,
-Add a minimum delay of half seconds inside the void loop.
-Disable parts of your additional code and try to find any possible error.
-Add variable to increment along with print to find whether multiple loops are occurring, also try placing the increment variable at different positions to find the exact area of looping.
Hello, we found your code helpful to us because we are adding a timer to our device may i ask if we can set a timer automatically using also a keypad for example we want to use letter “A” on the keypad if the user press A there is an automatic time that will be set for example A = 1 minute and 30 sec (1:30) is it possible to do with this code? Thank you for answering we are in a pinch right now.
Yes you can do it by adding an additional case in the switch statement like,
case ‘A’ : // executes when key ‘A’ is pressed.
countdown_time = 90; //eg:- 00:01:30 or 1 minute and 30 sec
lcd.setCursor(4, 1);
lcd.print(“00:01:30”);
break;
The above case will assign the variable “countdown_time” with the default time value and print the value.
Then if the key ‘D’ is pressed it starts the countdown.
This is just a simple and direct method. To set the time value in a particular format or to access hours, minutes, and seconds values separately, you can use an array to save the time value.
Thank you for your help we managed to set an automatic timer. but we are wondering if we can serial print a certain time frame for example if the time hits 55 seconds and we want to serial print a text what part in the code we need to get? sorry for asking were just confused on which code or variable to call to make a serial print when a certain time occurs is there a way to do it or we need to add new code and variables to it? Thank you again for your answers we really appreciate it.
Whatever time parameter you have to check for a time frame, just use an “if” condition inside void loop like,
if(countdowntime_seconds <= 55){ Serial.print("TEXT"); }
Thank you for the information you have given… I am trying to make use of your coding, where when I push button 1, then the countdown start… if I button 2, the countdown pause, and button 3 the countdown resumes…. I am able to do for button 2 but I am having difficulties to make function of button 1. Any solution?
Could you explain more about the exact issue related to button 1 for starting the countdown?
The countdown has already started before I pushed button 1….. button 2 and 3 have no problem pausing and resuming
Can you share your code?
forgot to mention that I am using Firebase to when the button are pushed. here are the codes… I am also facing a new problem, when command set to pause, countdown pauses but when command changed to resume, nothing happens (countdown stays paused)…
A few things I can mention from your code is,
1. your “pause”, “resume” conditions are inside the if condition for “start”. So whenever the command changes from “start”, everything is out; are you getting the text “WORK PAUSED”?
You have to separate the if condition for “start”, “pause”, “resume” as an if-else ladder.
2. The millis() function of the Arduino starts to count since the board is ON. So you have to subtract the time lapsed till the countdown is started.
Refer to the code under “LCD countdown display with Keypad input”.
Thank you for the information!!!
Hi,
Thank you for the code, its a great help with my project.
I require for the timer once hitting 00:00:00 to start again straight away.
With the timer set at 12:00:00 to start, it counts down to 00:00:00 – this all works perfectly
But how do I make it to reset at 00:00:00 back to 12:00:00 and begin counting down again?
Thanks for your help in advance.
You could refer the code in “LCD countdown display with Keypad input”.
Initialise a variable start_time.
long countdowntime_seconds = countdown_time – (millis() / 1000) + start_time;// modify the equation
if(digitalwrite(reset_pin)==HIGH){ // Add code for reset_pin
start_time = millis() / 1000;
}
When the reset pin is pressed the instantaneous value of the seconds will be added with the “countdown_time” value which makes the counter starts to count again for the time value of “countdown_time”.
i want circuit diagram
sir can i get your personal contact please? theres so many things i wanna ask to you. hope u read my message. thank you
do you know how to make the code from fuzzy logic controller (FLC) output timer to make counting down to turn on the relay ?
Mail to: admin@mechatrofice.com
Does anyone know how to add a message at the end of the timer?
I think you can do it by adding an else condition like below,
if (countdowntime_seconds >= 0) {
……..
} else {
lcd.print(“Message”);
}
Greetings! I dont know if you still notice this forum.
I would like to ask im trying to make a countdown timer and with coin acceptor i would like to ask on how can i the code that everytime i insert the coin it will automatically increase the time in the lcd.
you can try a method similar to this,
if (coinaccept == true) { // condition to check the coin is received
extraseconds += 3000; // time value to be added.
}
long countdowntime_seconds = countdown_time – (millis() / 1000) + extraseconds; //extra seconds will be added to the current countdown timer.
hi i would like to make it so when the timer runs out it turns my servo motor can you help me do so
Attach servo with Arduino, then add codes for including library, declare and initialize the servo. Refer: https://mechatrofice.com/arduino/servo-motor
In the code, inside if (action == “start_countdown”) add another “if” condition like the one given below,
if (countdowntime_seconds == 0) {
myservo.write(position);
}
}
Hello, i would like to know, how to make it count again from that time it was counting from begining. When it reach zero, i cant make it count from that time i set. I used your first basic counting program.
is it possible to set a time and than start the timer? When you have two buttons where you can choose how long the timer is?
Hey Admin, can you plz help me with the circuit of LCD with buzzer countdown using arduino.
Your code is easy to understand but not working with my circuit.
Please share the issue.
The code for LCD countdown display with Keypad input is not working properly when adapter is connected to aurdino uno it is accepting random numbers even if user is not pressing any key. What is the problem in these??
hi boss am working on the same project but in my case am using a 10k ohm variable resistor to set my time. I have a Start button to activate the timer and reset button clear the timer.
Any help would be highly appreciated thanks in advance. David
regards
Hi,
I am using your example for a countdown clock (24:00:00) it starts counting down (23:59:59) etc…
This all works great.
At some point I want to change the count down to a different hour. e.g. 12:00:00. But if the countdown is at say 15:30:45 and counting, when I change the hour to 12, it still displays 12:30:45 and counting.
I need the whole time to reset to 12:00:00 and start counting down from 12:00:00…11:59:59 etc.
I can do the hour change, but it doesn’t reset.
Can you help?
Thanks
Hi,
Is it possible to make the timer start over with a servo?