Pantech.AI

How to Interface DS1307 RTC Module with Arduino UNO

Overview of RTC

DS1307 RTC Module

A Real-Time Clock (RTC) is used for tracking time and maintaining a calendar.

To use an RTC, it must first be programmed with the current date and time. Once set, the RTC registers can be accessed at any time to retrieve the date and time.

The DS1307 is an RTC that operates using the I2C protocol, allowing data from its registers to be read by accessing specific addresses via I2C communication.

For more details on the DS1307 and its usage, refer to the RTC (Real Time Clock) DS1307 Module topic in the sensors and modules section.

Connection Diagram of DS1307 RTC Module with Arduino

Interfacing DS1307 RTC Module With Arduino UNO

Set Date and Time in DS1307 RTC and Read Using Arduino Uno

A Real-Time Clock (RTC) is used for tracking time and maintaining a calendar.

To use an RTC, it must first be programmed with the current date and time. Once set, the RTC registers can be accessed at any time to retrieve the date and time.

The DS1307 is an RTC that operates using the I2C protocol, allowing data from its registers to be read by accessing specific addresses via I2C communication.

For more details on the DS1307 and its usage, refer to the RTC (Real Time Clock) DS1307 Module topic in the sensors and modules section.

Code for Set and Read Time and Date In DS1307 using Arduino

/*
  DS1307 RTC (Real-Time-Clock) Example

  Uno       A4 (SDA), A5 (SCL)
  Mega      20 (SDA), 21 (SCL)
  Leonardo   2 (SDA),  3 (SCL)
 */

#include <Wire.h>
#include <DS1307.h>

DS1307 rtc;

void setup()
{
  /*init Serial port*/
  Serial.begin(9600);
  while(!Serial); /*wait for serial port to connect - needed for Leonardo only*/
  rtc.begin();
  /*init RTC*/
  Serial.println("Init RTC...");

  /*only set the date+time one time*/
  rtc.set(0, 0, 8, 24, 12, 2014); /*08:00:00 24.12.2014 //sec, min, hour, day, month, year*/

  /*stop/pause RTC*/
  // rtc.stop();

  /*start RTC*/
  rtc.start();
  delay(1000);
}


void loop()
{
  uint8_t sec, min, hour, day, month;
  uint16_t year;

  /*get time from RTC*/
  rtc.get(&sec, &min, &hour, &day, &month, &year);

  /*serial output*/
  Serial.print("\nTime: ");
  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(min, DEC);
  Serial.print(":");
  Serial.print(sec, DEC);

  Serial.print("\nDate: ");
  Serial.print(day, DEC);
  Serial.print(".");
  Serial.print(month, DEC);
  Serial.print(".");
  Serial.print(year, DEC);

  /*wait a second*/
  delay(1000);
}

Functions Used

DS1307 rtc

This creates an object named rtc from the DS1307 class.

rtc.set(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year)

This function sets the current date and time in the DS1307 RTC IC.

rtc.start()

This function initiates I2C communication with the DS1307 and retrieves the SEC and CH bytes from the DS1307 timekeeper registers.

rtc.get(uint8_t *sec, uint8_t *min, uint8_t *hour, uint8_t *day, uint8_t *month, uint16_t *year)

This function retrieves the time and date stored in the DS1307 IC.

Leave a Comment

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