How to design a digital clock using Arduino
In the past to calculate the time the analogue clocks were used which had a dial having numbers ranging from 1 to 12 and the dial had needles. But now the digital clocks are mostly used as they are compact in size, more accurate and less power consuming. To understand the working of the digital clock we have created a digital clock using Arduino Uno.
The Arduino program compiled for digital clock is given followed by the schematic for building a digital clock using Arduino:
Hardware implementation
These are the components that we have used for designing a simple clock using Arduino
- Jumper wires
- One Potentiometer having a value of 10K
- 16×2 liquid crystal display (LCD)
- Two push buttons
For assembling the circuit we have used the breabroad through which all components are connected with each other. Moreover we have given a figure below that further clears the connection of the components:
Arduino code for designing a digital clock using Arduino Uno
The Arduino code compiled for making the digital clock is given as
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); // pins of Arduino for LCD
// initializing the variables
int hrs = 12; // hours
int mins = 0; // minutes
int sec = 0; // seconds
int TIME = 0; // variable for checking the time
const int bhrs = A4; // push button setting the hours
const int bmins = A5; // push button pin for setting the minutes
int state1 = 0; // variable for storing the state of hour push button
int state2 = 0; // variable for storing the state of minute push button
void setup()
{
lcd.begin(16, 2); // initializing the dimensions of LCD
// mode for the push buttons
pinMode(bhrs, INPUT_PULLUP);
pinMode(bmins, INPUT_PULLUP);
}
void loop()
{
lcd.setCursor(0, 0);
sec = sec + 1;
// displaying the time
lcd.print("TIME:" );
lcd.print(hrs);
lcd.print(":");
lcd.print(mins);
lcd.print(":");
lcd.print(sec);
// checking for AM and PM as the status changes After 12o’clock
if (TIME 12) lcd.print(" PM");
if (TIME == 24) TIME = 0;
delay(800);
lcd.clear();
if (sec == 60) /* seconds is equal to 60 then again start from zero and add an increment of one in the minute value */
{
sec = 0;
mins = mins + 1;
}
if (mins == 60)
{
/* if minute is equal to 60 then again start from zero and add an increment of one in the hour value */
mins = 0;
hrs = hrs + 1;
TIME = TIME + 1;
}
/* if hour value is 13 then replace its value from 13 to 1 to change it to 12 hour format*/
if (hrs == 13)
{
hrs = 1;
}
lcd.setCursor(0, 1);
lcd.print("Simple Clock ");
// read the state of the button for hours setting
state1 = digitalRead(bhrs);
/* if the state of the button is low then add one in the hour and display the time*/
if (state1 == 0)
{
hrs = hrs + 1;
TIME = TIME + 1;
if (TIME 12) lcd.print(" PM");
if (TIME == 24) TIME = 0;
if (hrs == 13)
hrs = 1;
}
// read the state of the button for hours setting
state2 = digitalRead(bmins);
/* if the state of the button is low then add one in the minute value and display the time*/
if (state2 == 0)
{
sec = 0;
mins = mins + 1;
}
}
In the Arduino code first, we have defined the library for the display module and the pins of Arduino are assigned to LCD. Next we have declared the separate variables for hours, minutes and seconds. Also variables for the push buttons are declared with a pin to which they will connect. Similarly, there are two variables for the state of the buttons and one variable for the checking of the time.
In the setup function the mode to the push buttons is INPUT_PULLUP and the dimensions of the LCD are initialized.
Coming to the loop function first the format in which the clock is displayed is printed on the LCD then the TIME variable is used to determine if it is AM or PM. Since the status of the AM and PM changes after the 12 o’clock so the if conditions are made accordingly.
As we know that there are only 60 minutes in an hour and 60 seconds in one minute so whenever the value of the seconds reaches 60 it will make an increment of one in the value of minute and the same is the case with the hour value.
In the last the functions for the push buttons used for the setting the time are defined when the hourly button is pressed it will change the hour value. Similarly, when the minute button is pressed it will change the minute value.
Simple Arduino Uno Clock simulation
To demonstrate the working of digital clock we have created a simulation which can be seen in the figure below
Simple Arduino Uno Clock hardware demonstration
The figure for the actual hardware output for the circuit to make digital clock is given below:
Conclusion
The digital clocks are the advanced form of the analogue clocks which are more precise and less power consuming. Similarly, these clocks have display modules embedded in them on which the time is displayed in the form of numbers or digits. To understand the design and working of the digital clock we have created a digital clock using Arduino Uno.