arduino real time clock tutorial

Arduino Real Time Clock Tutorial

How to use a real time clock with arduino might sound like a lot of work, but hey, it’s not that difficult and not only is it fun, but you’ll learn microcontroller programming along the way!

We’ll assume that your still new to the world of microcontrollers but at the same time you are energized and can see the endless projects that you would like to make and are eager to learn more.

Here we will not only look at how to setup a real time clock module, but also build an LCD digital clock where we output the data from the real time clock to an LCD screen.



The DS1307 Real Time Clock (RTC)

We will be using the DS1307 real time clock which is a low power, binary coded decimal (BCD) clock calendar. It provides seconds, minutes, hours, day, date, month and year information. It adjusts the date for leap years and it also adjusts the months which have less than 31 days in them.

The time format can be selected to either 12 or 24 hours and a battery backup is used to keep time when power is removed from the circuit.

The DS1307 can be used in your projects such as data logging to provide a time stamp or for scheduling purposes like an irrigation controller.

The communications between the DS1307 and the microcontroller is performed serially using the I2C protocol which is bidirectional.

The time and date information is set by sending the information to the on board registers, and once set we can read the registers to get the latest time and date information.

 ds1307 real time clock module

Components Required to Build the DS1307 RTC

  • Arduino Uno
  • LCD Shield (that uses a 4 bit parallel interface)
  • Real Time Clock (RTC) DS1307 module
  • Prototype Breadboard
  • Dupont or solid core hook up wires
  • Toggle button (Optional)
  • Computer with Arduino IDE installed
  • USB cable


We wire up the clock by connecting the ground and power pins to the microcontroller’s ground and power pins. Then the I2C wiring is connected using the clock’s SDA and SCL pins to the microcontrollers SDA and SCL pins which are analogue pin 5 for SCL and analogue pin 4 for SDA.

You may notice we have an extra yellow wire that connects the board’s pin 2 to ground which we will look at a bit further down the page.

wiring diagram of RTC and arduino

How to Setup the DS1307 – RTC Time and Date

When you look at the code, initially it does seem like a lot but don’t let that deter you. We have not used a clock library but are instead are controlling the clock directly using the wire.h library. This way you can understand how the communication works when we set and read the information on the clock.

We first set the time and date variables to the time and date you would like the clock to start at. We entered the byte variables in decimal form as the code will convert them into binary coded decimal before the values are sent to the clock’s registers.

Once the clock is set we can then read the latest clock data, then output them to the LCD screen.

Earlier up the page we mentioned the extra yellow wire connecting the board’s pin 2 to ground. We have introduced an “If-Else” statement so the clock does not reset every time during power up.

So, after we upload the program, we set pin 2 high and the clock will be set at power up. After that, we can connect pin 2 to ground and leave it that way until we want to reset the clock again.

To make it easier you could place a button that goes high when pressed. In order to reset the time, the button is held down just before the board is powered up, and once the time starts to display on the LCD we can release the button.


RTC Project Code


#include “Wire.h”
#include <LiquidCrystal.h>
#define DS1307_ADDRESS 0x68
LiquidCrystal lcd(8,9,4,5,6,7);

int button_pin = 2;
int buttonState = 0;

byte set_second = 0;
byte set_minute = 58;
byte set_hour = 13;
byte set_day = 1;
byte set_date = 2;
byte set_month = 12;
byte set_year = 16;

byte read_second;
byte read_minute;
byte read_hour;
byte read_day;
byte read_date;
byte read_month;
byte read_year;

byte bcd_to_Dec(byte val)
  return( (val/16*10) + (val%16) );

byte dec_to_bcd(byte val)
  return( (val/10*16) + (val%10) );

void setup() {
  pinMode(button_pin, INPUT);
  buttonState = digitalRead(button_pin);

  if (buttonState == HIGH) {
  } else {}


void loop() {
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.requestFrom(DS1307_ADDRESS, 7);
  read_second = bcd_to_Dec(;
  read_minute = bcd_to_Dec(;
  read_hour = bcd_to_Dec(;
  read_day = bcd_to_Dec(;
  read_date = bcd_to_Dec(;
  read_month = bcd_to_Dec(;
  read_year = bcd_to_Dec(;
   if (read_hour<10)
  if (read_minute<10)
  if (read_second<10)


Using a real time clock in your projects can be very useful allowing you to advance your projects further to report real time and date information.

We hope this tutorial was useful to you.

If you liked the post, please share it with your friends, and remember to visit our blog regularly for our latest posts and hobby electronics information.

Before you move on, I would love for you to suggest some topics you would like to see covered on the website this year by leaving a comment in the comments section in the youtube channel, or send us a message through one of the social media links on the about page.