In this post we are going to construct a digital clock using RTC or Real Time Clock module. We will understand what “RTC” module is, how to interface with Arduino and what it does.
By: Girish Radhakrishnan
RTC module is a circuit, which keeps track of current time accurately. It does two functions, it communicates with microcontrollers and microprocessors to give current time and act as backup circuit for maintaining time in case of power failure, since it has build-in battery backup system.
We can find RTC in any electronic devices where time is an important function of the gadget.
For instance, our computer or laptop maintains its time even after power is plugged off or battery is removed. On the motherboard of any computer we can find a CMOS battery, which powers the RTC circuit.
Similar kind of circuit we are going to use in this project.
RTC module is an inexpensive device which can be found on any E-commerce sites and your local electronic project shops.
Illustration of typical RTC module DS1307:
Most of the RTC modules come with battery (CR2032) at the time of purchase. There are different of size and models, the above illustrated may not be the same for you. But make sure the model number is DS1307. The code written in this post is only compatible with DS1307.
Now you know something about RTCs. Now let’s move on to the digital clock design. Before proceeding with this project you need to download the library from the following links and install on your IDE:
Other two libraries would have been preinstalled on Arduino IDE, if you are on latest version.
The circuit connection between arduino and LCD display is standard, which we can find similar connection on other LCD based projects. The only additional component is the RTC.
To reduce wire congestion during prototype, the RTC can be inserted to the analogue pins directly of the arduino. Solder the SCl, SDA, Vcc, and GND with male header pins and insert it A2 to A5 pins as shown in the prototype.
How to properly insert RTC on Arduino:
If your RTC has different pin locations and could not replicate as illustrated above, you can always use wires for connection. Now your hardware setup is complete, let’s move to the software part of the project.
How to set time:
Once the RTC module is programmed, it maintains the time even it is removed from the arduino. The battery should last at least a couple of years.
There is no button to adjust the time; the following program will set the time in RTC. The time automatically synchronized with time of your computer, while compiling the code, so make sure your computer is set to correct time, before uploading the programs.
Upload this “SetTime” code to set the time with RTC plugged in:
Once this code is uploaded, open the serial monitor and a success message should pop up saying the time has been set.
This signifies that your connection between the RTC and arduino is correct and time is set.
Now upload the following code for displaying the time in LCD.
Once this is done you should see the time and date is displayed on LCD and running.
Note: The “SetTime” code is modified from example code of DS1307RTC to optimize wire connections for RTC module, uploading original code won’t set time.
Digital Alarm Clock Using Arduino
In this above we learned how to build the basic Arduino clock using RTC module, in the following secion we investigate how this can be upgraded into a digital alarm clock circuit using Arduino.
There are some people who need no alarm clock, they wake up naturally and there are some people who wake up after alarm clock rings few times and there are some people who press snooze button a bunch of times and go for their college / work late with some excuses.
The proposed fun little alarm clock project may confront the problem with laziness during morning wake. Most alarm clocks have a snooze button and pre-determined cut-off time for alarm if user didn’t respond.
We designed this alarm clock without a lazy button (snooze button) and the alarm won’t turn off until the user push a button.
This clock can show time in 12 hour format and date in DD/MM/YYYY format.
The time and date will be showcased on 16 x 2 LCD display. An RTC or real time clock time module will take care of tracking the time and can retain correct time even after a long power cut.
There are 5 buttons provided who’s function will be explained shortly. The brain of the project Arduino can any model of your choice, we would recommend Arduino pro mini or Arduino nano because of its compact size.
Now let’s dive into schematics.
The above is the schematic is for Arduino to display connection, adjust the display contrast by rotating the 10K potentiometer.
The below is the rest of the circuit:
The circuit can be powered 9V 500mA wall adapter.
S1 - This is used for stop the alarm (it is also reset button).
S2 - Is used for setting alarm. By long pressing S2 you will get to alarm setting menu.
S3 - It is used to increment hours.
S4 – it is used to increment minutes.
S5 – Is used for toggling the alarm status. If “*” exists on LCD display on right side bottom corner the alarm is ON, if the “*” does not exists the alarm stats is OFF.
More detail on how to set alarm is explained at bottom of the article.
Download the library files below:
Now, we have to set time to RTC module, the time will be synchronized from you PC to RTC module.
Upload the below code to set time and open Serial monitor:
Now you have successfully set time to RTC
Next, you need to upload the following main code:
After uploading the above code you must see correct time and date running on the display.
Now let’s see how to set alarm:
• Long press S2 until you can see Alarm menu.
• Press S3 and S4 to adjust hours and minutes respectively.
• After the desired time set, press S2 once again. It will say “The alarm is set for xx:xx hours”.
• If the alarm is ON you can see “*” symbol on the display, if the alarm is OFF there will be no “*” symbol.
• You can turn on / off the alarm by pressing S5 for half a second. Don’t long press until “*” vanishes (it comes back again), just press half a second long to toggle alarm status.
The most common mistake while setting an alarm on any clock is unintentional toggling AM / PM, which results in not ringing of alarm at our desire time.
To counter this issue the proposed alarm clock setting is designed on 24 hour clock format.
The time displayed on the LCD will be 12 hour with AM / PM format but, when you set an alarm with this project you have to set in 24 hour format from 0 to 23 hours.
For example: if you want to set alarm at 9:00 PM you have to set 21 hours and 0 minutes. For, 5 AM: 5 hours and 0 minutes and so on.
Do you like this project? Have any question regarding this project, feel free to express in the comment, you may receive a quick reply.