Real time clock RTC DS1307 Arduino

Posted by

Real time clock RTC DS1307 is a chip that works via i2c communication protocol. It can supply and maintain current hour, minute, secod, day, month and year in a 12 or 24 hour format. It features (in the case of the shield used in this tutorial) a CR2032 battery backup.

Connections to Arduino UNO are as follow, it is only necessary to provide 5V and GND along with i2c (SCL A5 and SDA A4 of Arduino UNO).

DS1307 RTC module connections. Source: https://www.makerhero.com/blog/relogio-rtc-ds1307-arduino/
DS1307 RTC module connections. Source: https://www.makerhero.com/blog/relogio-rtc-ds1307-arduino/
DS1307 on a breadboard with Arduino
DS1307 on a breadboard with Arduino

I first got code from this blog and the library from this Github. Here is the code and the serial monitor result:

//Program : Clock with RTC DS1307
//Author : MakerHero
 
//Load RTC DS1307's library
#include <DS1307.h>
 
//i2c ports in Arduino UNO 
DS1307 rtc(A4, A5);
 
void setup()
{
  //Turn the clock on
  rtc.halt(false);
   
  //The lines below set the date and time
  //uncomment them, use once and comment them again
  /*rtc.setDOW(WEDNESDAY);      //Define the day of week
  rtc.setTime(19, 47, 00);     //Define time
  rtc.setDate(3, 7, 2024);   //Define day, month, year*/
   
  //SQW/Out pin definition
  rtc.setSQWRate(SQW_RATE_1);
  rtc.enableSQW(true);
   
  Serial.begin(9600);
}
 
void loop()
{
  //Show info on Serial Monitor
  Serial.print("Hour (24h) : ");
  Serial.print(rtc.getTimeStr());
  Serial.print(" ");
  Serial.print("Date : ");
  Serial.print(rtc.getDateStr(FORMAT_SHORT));
  Serial.print(" ");
  Serial.println(rtc.getDOWStr(FORMAT_SHORT));
   
  //Wait 1s and do it all over
  delay (1000);
}
Date and time at the Arduino serial monitor, DS1307
Date and time at the Arduino serial monitor, DS1307

Notice that time is in 24h format (it is showing 7:57 PM) and date is not in American format (hence July 3rd/2024). To test it I removed Arduino power completely for a couple of minutes. After restablishing it the time and date remained current, so the CR2032 battery really works its purpose.

Remember you have to change the lines below with your current date and time. Do it only once then you have to comment or remove the lines.

rtc.setDOW(WEDNESDAY);      //Define the day of week
  rtc.setTime(19, 47, 00);     //Define time
  rtc.setDate(3, 7, 2024);   //Define day, month, year

Regarding the piece of code below, it seems to be a square wave generated every second (SQW_RATE_1). I disabled the entire two lines because had no visible use to it.

//SQW/Out pin definition
  rtc.setSQWRate(SQW_RATE_1);
  rtc.enableSQW(true);

Apparently it is used to inform your microcontroller that the time is passing. So that it can have its own control of time without reading the DS1307. We are not going to use it, despite the DS1307 module we are using having the SQ pin available.

Battery usage and duration

According to the DS1307 datasheet, a 48mAh battery will retain time and date on the chip for 10 years. So most likely it will take a long time before you have to replace the module battery.

There are 56 bytes of information which the battery will backup, including hour, minute, second, day, month and year. That includes automatic number of days in the month calculations and also leap years. The module’s logic is guaranteed to work until 2100.

Final words

The DS1307 chip a bit old already, there are better options available. Specially in the 2100 time limitation. It remains an easy to use and cheap real time clock module for hobby and students projects.

If you want to follow along and replicate the experiment, buy a DS1307 via my Aliexpress affiliate link, here. A bit unrelated but still talking about energy consumption, check my article about lowering relay power consumption.

2 responses

Leave a Reply

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