Arduino

How to Make a Digital Clock using RTC Module and Arduino Uno

Arduino is a diversified  platform  that provides beginners as well as experts to create different types of projects. The Arduino offers a variety of boards which differ from each other based on the specification of each board. The Arduino Uno is one the most commonly used boards of the Arduino family as it is compatible with a number of peripherals. We have made a real time clock by interfacing a real time clock module with Arduino Uno.

What is RTC Module

This is the real time clock module which saves the data of time and date and also displays it on the connected displaying screen. This module keeps the time and date updated and has a crystal oscillator which generates a signal at a constant frequency. Furthermore the RTC module has dedicated pins of the temperature sensor on its back side and there is a special pin that reads the output of the sensor.

The module also comes with a 3V lithium-ion battery which keeps the module running for quite some time if the main supply is disconnected.

There are 7 pins of the module in total but on the opposite side it has got the same 5 pins so you can solder the headers on any of the the both sides if there is battery placed in the back side of the module and you don’t want to use the SQ pin of the RTC module. The table below shows the pins of the RTC module:

Pins Description
1-(SQ) Pin for generating the  square wave
2-(DS) Pin that reads the output of the temperature sensor (DS18B20) if  installed on the RTC
2-(SCL) Clock pin for the Arduino
4-(SDA) Data pin for the Arduino
5-(Vcc) Pin for supplying the voltage to module
6-(GND) Pin for grounding the module
7-(BAT) Pin for connecting external battery as back-up

Making a digital clock using RTC with Arduino Uno

To design real time clock, you will be requiring the following components:

  • Arduino Uno
  • Jumper wires
  • Real time clock module (DS 1307)
  • LCD
  • Potentiometer
  • Breadboard

The schematic of the circuit for the digital clock using the RTC module is given below . Here in the schematic we have used the DS(1307) integrated chip which  is almost  similar to the DS3231 module.

Hardware assembly of digital clock using RTC

We have also implemented the digital clock circuit in the hardware in such a way that first we have placed all the components in the breadboard. Next we have used the connecting wires for the interfacing of the components with the Arduino. We have posted an image of the hardware assembly of the  circuit in which the blue wires are the data pins of the LCD connected to Arduino. The yellow wire is connected with a potentiometer for contrast adjustment. Moreover, the gray wires are the clock and data pins of the RTC module connected with Arduino.

Installing the library of RTC

To interface the RTC module with Arduino first we have to download the RTC  module library. First you  have to open the library manager of the Arduino IDE and after that  write the keyword rtclib in the search bar of the library manager. Next you have to find the RTClib by Adfruit and then click on the install icon in the blue to install the library.

When we click on the “INSTALL” button a  new dialog box appears for confirmation,  so  we have to click on the “Install all” icon.

Arduino code for digital clock using RTC 

The code for the creating a real time clock using Arduino is:

#include "RTClib.h"// defining library for the RTC module
#include <LiquidCrystal.h>// defining library for the LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// assigning Arduino pins for the LCD
RTC_DS3231 rtc;// declaring the RTC module

void updatingRTC() // function for updating the RTC for first time
{
  lcd.setCursor(0, 0);// setting the place for the data to be displayed
  lcd.print("Updating...");// displaying the data

  // ask user to enter new date and time
  const char txt[6][15] = { "year", "month ", "day ",
                            "hour ", "minutes", "seconds"};
  String str = "";// creating a string
  long RTC_Array[6];// defining a variable to store the input data

  while (Serial.available()) {
    Serial.read();  // reading the serial input
  }

  for (int i = 0; i < 6; i++) {// loop for taking 6 inputs form the user

    Serial.print("current ");// displaying the data on the serial
    Serial.print(txt[i]);
    Serial.print(": ");

    while (!Serial.available()) {
       // checking if there is input from the user
    }

    str = Serial.readString();  // reading the string of input
    RTC_Array[i] = str.toInt();   // converting the user input to number and saving it  to an array

    Serial.println(RTC_Array[i]); // printing the input of the user on Serial
  }

  // updating the RTC by the input of the user
  rtc.adjust(DateTime(RTC_Array[0], RTC_Array[1], RTC_Array[2], RTC_Array[3], RTC_Array[4], RTC_Array[5]));
  Serial.println("Time and date is Updated");
}
void Displayupdate()// function for updating the display of LCD
{//creating and array to convert the digit  to words starting from 0 to 6

  const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
// converting digit months in words starting from 1 to 12
 const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};

  //reading time and date  and saving them  in variables
  DateTime rtcTime = rtc.now();

  int sec = rtcTime.second();// seconds
  int mins = rtcTime.minute();// minutes
  int hrs = rtcTime.twelveHour();// hours
  int day = rtcTime.dayOfTheWeek();// day in word
  int dd = rtcTime.day();// day in number
  int mth = rtcTime.month();// month
  int yr = rtcTime.year();// year

 
  lcd.setCursor(0, 0);/*setting the place for displaying the date and day of week in words */

  // displaying the  date in DD-MM-YYYY format and days of week
  if (dd < 10) lcd.print("0");  
  lcd.print(dd);
  lcd.print("-");
  lcd.print(monthInWords[mth]);
  lcd.print("-");
  lcd.print(yr);
  lcd.print("  ");
  lcd.print(dayInWords[day]);
  lcd.setCursor(0, 1);/*setting the place for displaying the Time */
  // displaying the time on LCD
  if (hrs < 10) lcd.print("0");
  lcd.print(hrs);
  lcd.print(':');

  if (mins < 10) lcd.print("0");
  lcd.print(mins);
  lcd.print(':');

  if (sec < 10) lcd.print("0");
  lcd.print(sec);

  if (rtcTime.isPM()) lcd.print(" PM"); // displaying  AM/PM
  else lcd.print(" AM");
}
void setup()
{
  Serial.begin(9600); // initializing  serial communication

  lcd.begin(16,2);   // initializing the  LCD

  rtc.begin();  // initializing the  RTC module
}
void loop()
{
  Displayupdate();  // calling the function for updating the LCD

  if (Serial.available()) { //checking if there is any input from the  user
    char input = Serial.read();// reading the input from the user
    if (input == 'R') updatingRTC();  // if R is the input given call the update function for RTC
  }
}

The Arduino code for making a clock is compiled in such a way that first we have defined the library for the RTC module. After that we have defined the library for the LCD and assigned the pins of Arduino to LCD and declared that to use functions of the RTC we will use the keyword RTC.

We have created two functions, one for updating the RTC module when connected for the first time and the other function for updating the LCD.

In the updatingRTC function first we have declared a character  for the date and time of the week and created an empty string in which all the input from the user will be stored. Since there are 6 inputs required from the user to update the time and date, we have used a for loop to get the input from the user using the serial monitor of Arduino IDE.

Next we have used the rtc.adjust() function to save the  current date and time in the RTC Module.

Coming to the Displayupdate() function  at the start we have converted the number input for the days of week and the month of the year into character input and then  defined the format for the date  that is DD-MM-YYYY.

Next we have displayed the date and time on the LCD using the lcd.print() function.

In the setup function after initializing the serial communication both the RTC and LCD modules are initialized using the begin function.

Similarly, coming to the loop function, the first Displayupdate() function is called that displays the current saved date and time in the RTC module. To update the date and time we have used and if condition when the character  R is entered as a serial input then call the updatingRTC function and update the date and time of the module.

Remember the RTC module saves the time and date given the  first time and keeps updating that date and time even if the supply is turned off because it has a built-in battery that keeps the module running. Hence the date and time input is given for only the first time and it keeps the record of time until it is changed manually.

Hardware implementation for digital clock using RTC with Arduino Uno

To update the time and date we have to enter the character “R” in the serial monitor by pressing ctrl+Enter and the Arduino code will require the data for current date and time. we have posted the image below so you can be a clear picture of the required inputs:

After you have entered all the data the program will tell the RTC module is updated. While updating the time and date the LCD will display that RTC is being updated:

Once you have updated the date and time it will be displayed on LCD:

Conclusion

The Arduino Uno board is an advanced form of a microcontroller which has made interfacing of devices with microcontrollers easy. Using Arduino Uno we can build different varieties of projects. In this write-up we have designed a real time clock using the RTC module and the procedure is briefly explained with the help of Arduino code and simulation.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.