Arduino projects: Building an Arduino countdown timer

Here is a little Arduino project (beginner’s level) with the aim to help you have a glimpse of things the Arduino Uno board can help your achieve.

We will be building a 24 hours countdown timer using the Arduino Uno board, a potentiometer (10 kΩ variable resistor), a half-size breadboard, some jumper wires, the LCD Display (16×2 characters) and a header strip.

Basic Arduino countdown timer

Parts needed

Arduino Uno Board x 1
Potentiometer x 1
LCD display x 1
Half-size breadboard x 1
Jumper wire pack x 1
Header strip x 1

arduino1arduino-countdown-timerarduino-countdown-timerarduino-countdown-timerarduino-countdown-timer
arduino-countdown-timer

Breadboard Layout

There are more than few connections to be made.

lcd

4 pins of the LCD will be left unused. (you can find the data sheet of this LCD here)

The LCD is basically a parallel port LCD featuring 16 X 2 character display meaning we could display up to 16 characters on each of the two lines.

The following diagram will help you read connections more easily, but if you feel more comfortable with the one above, the following one can still help for verification.

schematic

source: sunfounder.com

In this project, the potentiometer serves to tune the contrast of the LCD. You might need to play with it a little to see anything appearing on the screen. A common mistake is to have it totally turned down, and having the illusion the connections have not been made correctly.

You can find out more about the wiring process here.

Sketch (The code)

#include <LiquidCrystal.h> 

int S = 59; // count seconds 
int M = 59; // count minutes
int H = 23; // count hours
//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(4,6,10,11,12,13); // pins connected to LCD

void setup()
{
 lcd.begin(16,2);//set up the LCD's number of columns and rows
}
void loop()
{
 lcd.setCursor(1,0); 
 lcd.print ("Tutorial45.com");
 lcd.setCursor(6,1);
 lcd.print(":");
 lcd.setCursor(9,1);
 lcd.print(":");

 S--;
 delay(1000);
 
 if(S<0)
 {
 M--;
 S=59;
 }
 if(M<0)
 {
 H--;
 M=59;
 }
 if(H<0)
 {
 H=23;
 M=59;
 S=59;
 }
 if(M>9)
 {
 lcd.setCursor(7,1);
 lcd.print(M);
 }
 else
 {
 lcd.setCursor(7,1);
 lcd.print("0"); 
 lcd.setCursor(8,1);
 lcd.print(M);
 lcd.setCursor(9,1);
 lcd.print(":");
 }
 
 if(S>9)
 {
 lcd.setCursor(10,1);
 lcd.print(S);
 }
 else
 {
 lcd.setCursor(10,1);
 lcd.print("0"); 
 lcd.setCursor(11,1);
 lcd.print(S);
 lcd.setCursor(12,1);
 lcd.print(" ");
 }
 
 if(H>9)
 {
 lcd.setCursor(4,1);
 lcd.print (H);
 }
 else
 {
 lcd.setCursor(4,1);
 lcd.print("0"); 
 lcd.setCursor(5,1);
 lcd.print(H);
 lcd.setCursor(6,1);
 lcd.print(":");
 }
}

The code explained

 lcd.setCursor(1,0);
lcd.print("tutorial45.com");

Set the writing head at the position 1,0 and print tutorial45.com from that position, each character in a cell.

 lcd.setCursor(6,1); 
lcd.print(":");

Set the writing head at the position 6,1 and print :

 lcd.setCursor(9,1); 
lcd.print(":");

Set the writing head at the position 9,1 and print :

The following picture perfectly illustrates what the above lines do.

arduino-countdown-timer

Other lines of the program use the same principle at the only difference that the values of the six cells where hours, minutes and seconds are displayed are dynamic and need to change under some conditions, thus the multiple if conditions you see in the program.

We are giving a delay of 1000 ms in every iteration, considering the execution time for each other line in the loop is zero second.

The countdown timer we’ve just built starts at 23 hours, 59 minutes and 59 seconds. It counts down to zero and starts over. You can change the starting value of H, M and S in the sketch to let it count down from wherever you want.

tchouken2

Related Posts