Interfacing Real Time Clock module (RTC) with Arduino [DS1307]

Interfacing Real Time Clock module (RTC) with Arduino [DS1307]

While doing some project you may come across a scenario where you need to execute a command at certain days or certain times of the day or log data at a certain interval of time. You won’t be able to achieve this by using delay() because whenever Arduino resets it starts to execute the program from the beginning. In such a scenario a real-time time (RTC) can do the job. Let’s see how

Components you need

Before we begin a big shoutout to sponsor of this content Digitspace

DigitSpace is an open-source hardware provider catering to future creators. They supply innovative, user-friendly open-source hardware products ranging from boards to corresponding components and kits for worldwide community of engineers, designers, inventors, and makers who love

What is RTC ?

Real-Time Clock (RTC) is an integrated circuit that keeps the track of real-time. RTC is found in almost all electronic devices nowadays which requires maintaining accurate time. time like in PC, laptops. There are some benefits for using RTC such as low power consumption, Releasing system time from time calculation, great accuracy.

How does it work?

RTC comes with an alternative power source most commonly a lithium coin-cell (CR2032). So even if the main power source is not available then also RTC will be working. RTC uses crystal oscillator most of the times a 32.768 kHz (2^15)

RTC Coincell lifespan


According to datasheet a lithium cell with 48mAh capacity can backup the device for more than 10 years. which is quite good for any hobby project.

DS1307 Features

  • Manages All Timekeeping Functions, Seconds, Minutes, Hours, Date, Month, Day of Week, Year, Year with leap year
  • I2C Communication
  • Lower Power consumption, Consumes Less than 500nA
  • Power failure detect and switch circuitry
  • Operational temperature 40°C to +85°C

Typical Working Circuit of DS1307

source : https://datasheets.maximintegrated.com/en/ds/DS1307.pdf

DS1307 Pinout

Serial Clock pin for the I2C interface and is used to sync data.
Serial data pin for I2C serial interface.
power supply pin for the module. (3.3V to 5.5V.)
goes a ground of the circuit

Wiring Diagram

Wiring diagram
DS1307 RTC Pin Arduino Pin
GND GND
VCC VCC
SDA A4
SCL A5

Installing Library

For making the programming complexity to super simple there a library called <a rel="noreferrer noopener" href="https://github.com/PaulStoffregen/DS1307RTC" target="_blank">DS1307RTC</a>. You can easily download the it from the Arduino IDE library manager itself. DS1307RTC by Michael Margolis

Also, install TimeLib library to Arduino IDE, you can download the zip file from here

IMPORTANT : Before uploading the sketch make sure your pc/laptop time is correct because here we will taking time from PC

Once both libraries are installed upload the SetTime example
File > Example > DS1307RTC > SetTime

Upload SetTime Example Sketch


Open Serial Monitor

If your connection is proper you will get a message in serial monitor like shown above.
DS1307 configured Time=XX:XX:XX, Date=XXX XX XXXX

If there is some issue in the circuit it will be showing
DS1307 Communication Error :-{<br>Please check your circuitry

To check the time set by the code, upload ReadTest sketch from DS1307RTC examples.
File > Example > DS1307RTC > ReadTest

ReadTest sketch

After successfully uploading the sketch open serial monitor. It will be showing the current time in the serial monitor.

ReadTest Serial monitor output

Leave a Reply

Your email address will not be published. Required fields are marked *