How To Make A Digital Clock With Arduino UNO
Advertisement
In the previous blog, we learned how to connect the LCD Display to the Arduino UNO and how to Display Text on LCD Screen. In this blog, we will learn how to make a digital clock in Arduino UNO using a 16X2 LCD Screen.
Ever thought about how a digital clock is programmed to show the time on a display.
Now let’s put some jumper wires and resistors to make this project.
Display units play an important role in establishing good communication between the human and machine worlds. And so they are a big part of embedded systems.
Introduction
The 16×2 LCD Screen will have a total of 32 characters, 16 in the 1st line, and a further 16 in the 2nd line. In 16×2 LCD there are 16 pins overall if there are backlight, 14 pins if there is no backlight. In each character, there are 50 pixels.
Components Required for Digital Clock in Arduino UNO
Hardware:Â
- ARDUINO UNO
- power supply (5v)
- 16×2 LCD Screen
- 100uF capacitor
- Breadboard
Software:
- Arduino IDE
- LiquidCrystal Library
Digital Clock Circuit Diagram Arduino UNO
Now, let’s build the circuit. A circuit layout can be described in several ways. For this project (How To Connect LCD 16×2 Display To Arduino), we are using a physical layout diagram similar to the one shown above.
By comparing the wiring diagram with the sketched functions, You should start making sense of the circuit.
- LCD RS pin to digital pin 12
- Enable pin of LCD to digital pin 11
- LCD D4 pin to digital pin 5
- D5 pin of LCD to digital pin 4
- LCD D6 pin to digital pin 3
- D7 pin of LCD to digital pin 2
- LCD R/W pin to ground
- VSS pin of LCD to ground
- LCD VCC pin to 5V
- 10K resistor
- ends to +5V and ground
- wiper to LCD VO pin (pin 3)
If you want to see the stimulation of the project. Click here https://www.tinkercad.com/things/5nSdmAXX4Aj
Digital Clock Arduino UNO Code
In this blog, we understood how to connect the LCD screen to Arduino UNO.
Now, we will cover the code on how to display a clock on an LCD screen using Arduino IDE.
#include
LiquidCrystal lcd(12,11,5,4,3,2);
int h=12;
int m;
int s;
int flag;
int TIME;
const int hs=8;
const int ms=9;
int state1;
int state2;
void setup()
{
lcd.begin(16,2);
}
void loop()
{
lcd.setCursor(0,0);
s=s+1;
lcd.print("TIME:" );
lcd.print(h);
lcd.print(":");
lcd.print(m);
lcd.print(":");
lcd.print(s);
if(flag<12) lcd.print(" AM");
if(flag==12) lcd.print(" PM");
if(flag>12) lcd.print(" PM");
if(flag==24) flag=0;
delay(1000);
lcd.clear();
if(s==60) {
s=0;
m=m+1;
}
if(m==60)
{
m=0;
h=h+1;
flag=flag+1;
}
if(h==13)
{
h=1;
}
lcd.setCursor(0,1);
lcd.print("HACKTHEDEVELOPER");
//-----------Time setting----------//
state1=digitalRead(hs);
if(state1==1)
{
h=h+1;
flag=flag+1;
if(flag<12) lcd.print(" AM");
if(flag==12) lcd.print(" PM");
if(flag>12) lcd.print(" PM");
if(flag==24) flag=0;
if(h==13) h=1;
}
state2=digitalRead(ms);
if(state2==1) {
s=0;
m=m+1;
}
}
In this program, we have discussed many functions like delay(), digitalWrite(), pinMode().
It’s time to learn what are these functions and how they work.
delay()
 is a blocking function. It is the syntax of writing for giving hold between ON and OFF or 0 and 1 signal to the Arduino.
In the above code, we have provided 1000ms to the delay() function that is equal to 1 second.
digitalWrite()
 – This function helps in to write the command on the microcontroller i.e which should give LOW output or which pin should give HIGH output. digitalWrite(6, LOW); this line means pin number 6 will be LOW or OFF and digitalWrite(8, HIGH); pin number 8 will be HIGH or ON.
pinMode() – This function helps to assign the pin number and clarify that a specific pin number is given OUTPUT or take INPUT from the sensors or the components.
LiquidCrystal.h is a type of header file. SetCursor is a function that is present in the LiquidCrystal.h header file. With the help of this function, we fix the coordinate in the LCD Display.
h,m,s,flag,TIME,hs,ms,state1,state2 are the variables in the program.
Lcd.begin(16,2)
, 16×2 means that the LCD has 2 lines, and can display 16 characters per line. Therefore, a 16×2 LCD screen can display up to 32 characters at once.
lcd.clear()
helps to clear the screen.
lcd.print()
the function helps to print something on the LCD screen.
Hope you like it!
Take a look at other interesting Arduino Projects.
About Author
I am a hardworking and ambitious individual with a great passion for the Robotics and the IoT. I am currently in the second year of B-tech (CSSE) at KIIT University.
Wonderful piece of information 🤩
how can we change the minutes ??
How can i change the minute or hour, there should be a push button.