Arduino Projects

Tutorial DS1307 RTC Module With Arduino


In this tutorial, we will work with the RTC (Real Time Clock) Module DS1307 RTC Module With Arduino. we will be able to know the exact time, day month year, and even the day of the week, in turn, thanks to an EEPROM that the module brings we can save the data without clear when you turn off the Arduino.

Thank You NextPCB:

This project is successfully finished because of the service and support from NextPCB. NextPCB is one of the most professional PCB manufacturers in Global and has specialized in the PCB and assembly industry for over 16 years. Not only could NextPCB deliver the most creative printed circuit boards and assembly technologies in the most elevated quality standards, but the fastest delivery turnaround was also as fast as 24 hours.

Guys if you have a PCB project, please visit their website and get exciting discounts and coupons. if you sign-up using this link you will get free coupons worth $100. Checkout out NextPCB now and get amazing discounts on your first order.

What is a DS1307 module?

ds1307 e1652942968152

The DS1307 from Dallas Semiconductor (Maxim) is a device known as a “Real Time Clock” ( RTC ) that operates through the I2C bus and that, in addition to giving us the time with minutes and seconds, has a calendar that includes leap years until the end of the century, that is, until the year 2100.

The module we will be using is the low-cost DS1307 RTC module with the AT24C32 I2C EEPROM from ATMEL. Both integrated circuits share the same communication bus with the I2C Protocol. AT24C32 EEPROM memory allows you to store 32Kbits (4K Bytes) of data permanently.

DS1307 sensor

On the back, it has a socket to insert a 3V battery, which allows the RTC to keep the time and date in operation when the Arduino is turned off or the main power is removed. This battery, depending on the brand and the time it is off, can last from 3 to 10 years approximately.

DS1307 RTC functions:

  • Time: Seconds, minutes, hours
  • Date: day number, day of the week, month, and year
  • Leap year compensation
  • Output pin at 1 Hz to carry timing on other components

DS1307 RTC Module Spec

  • Supply voltage: 3.3V – 5V DC
  • Main chips: DS1307 and AT24C32
  • Digital communication interface: I2C
  • DS1307 I2C address: Read(11010001) Write(11010000)
  • AT24C32 EEPROM memory (4K * 8bit = 32Kbit = 4KByte)
  • Can be used in cascade with another I2C device, the address of AT24C32 can be modified (default is 0x57)
  • The battery can keep the RTC working for 10 years.
  • Dimensions: 28*27mm
  • Weight: 5 grams

DS1307 RTC Pinout

DS1307 module e1652943098962

This module communicates with the Arduino through the I2C bus and a bus that it shares with the DS1307, in this way we only need 2 Arduino pins to control the module. RTC and EEPROM of the Module.

The communication between the Arduino and the RTC Module is through I2C (SDA and SCL pins), It also has a pin (SQ) that sends a pulse every second (1 Hz), which can be used to synchronize other components or to make a counter of seconds.

Connections between DS1307 RTC Module With Arduino

As you can see, the connection is like any I2C connection. the TinyRTC module requires the following connections to the Arduino board. Note that the “DS” pin of the TinyRTC module is not used, it corresponds to the 1-wire temperature sensor that is not factory installed. Only the SDA, SCL lines, and the 5-volt power supply are necessary:

  • SCL -A5
  • SDA -A4
  • VDC – 5V
  • GND – GND

Programming And Library

For this example we will use the RTClib library from Adafruit, a library that can be downloaded from here:

After downloading the library import this library into the Arduino IDE.

Correcting the time and date of DS1307 RTC

When we receive a DS1307 module, without a battery, it does not have the updated time, every time we remove the battery the DS1307 loses the time, establishing itself as time 0:0:0 and date 00/00/00,

This example is only used to set our DS1307 to the time:

If you want to update at an exact time and date, you must change the following line of code:

Reading Time and Date from DS1307 RTC Module 

In this sample, we will read the seconds, minutes, and hours of the DS1307 RTC module, and in turn, we will also obtain the date.

To perform this sample we use the below code:

As you can see, the code is easy to understand.

In the void Setup, we only initialize the RTC and in the  void loop() we read the DS1307

Here is the serial monitor for this example:


Automatic Relay on and off

In this sample, we will show the code to turn on a light or any electronics appliance. etc, at a time decided by our program.

For example, to turn on the light every day from 6:00 p.m. to 10:00 p.m., or to turn on a pump to select a watering schedule in a garden, etc.

The example shown below activates relay from Monday to Friday from 6:30 pm to 11:30 pm

To modify the schedule, you only have to change the condition, note that we convert the minutes to hours to work the condition in a shorter way.

The println() serials are only used for demonstration purposes because it is only to monitor the status:

Related Articles

Leave a Reply

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

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker