Interfacing DS1307 RTC Module With Arduino
Hello friends, welcome to this tutorial, in this tutorial we are going to discuss how to use DS1307 RTC module in your Arduino project. You can also follow this tutorial
Hello friends, welcome to this tutorial, in this tutorial we are going to discuss how to use DS1307 RTC module in your Arduino project. You can also follow this tutorial for other RTC modules such as DS2301.
What is DS1307 RTC module?
RTC – Real Time Clock – As is clear from the name itself, DS1307 RTC module is used as a module to remember TIME and DATE and, as I told you earlier, it has an inbuilt battery that keeps the RTC module running. Due to the battery being included, the module remains on and gives precise timing information whenever asked.
Features of DS1307 RTC Module
- Two-wire I2C interface
- Hour: Minutes: Seconds AM/PM
- Leap year compensation Accurate calendar up to the year 2100
- Consumes Less than 500nA in Battery-Backup
- 1Hz output pin
- 56 Bytes of Non-volatile memory available to the user 4KB of serial electrically erasable and programmable read-only memory (EEPROM)
- Embed DS18B20 temperature sensor interface with the pull-up resistor.
Interfacing DS1307 with Arduino
Hardware Components
Software Component
As mentioned in the features, this module uses I2C communication to communicate with the master (microcontroller). So, to communicate with this module, you have to establish I2C communication between DS1130 RTC and Arduino.
In the picture above, you can see that we have established a connection between the Arduino and the RTC module. In the picture above, we have connected the Arduino's SCL pin to the RTC module's SCL pin and the Arduino's SDA to the RTC module's SDA pin.
Since the operating voltage of the module is less than 3.3v, we are powering the RTC module through the Arduino.
Arduino Code for DS1307 RTC Module
/*
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*/
/*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();
}
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);
}
RTC Arduino code explanation
By using the above code you can save real-time in the module and read the exact time from the module. In this part of this blog, I will tell you how you can store time and then how you can read data from module. But before that, let's understand the usage of the Functions used in the code.
DS1307 rtc
Here DS1307 is the class name and "rtc" is the object we created for that class to access the data and functions of the DS1307 class.
rtc.set(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year)
The DS1307 class has a "set" method, this method is used to set TIME and DATE. Using the above line of code, we are giving the date and time to the function.
Ex: rtc.set(23, 12, 2, 19, 6, 1996)
Date- 19/06/1996(DD-MM-YYYY)
Time – 2-12-23 (HH-MM-SS)
rtc.start()
This function is used to start I2C communication with DS1307. It fetches the SEC and CH byte from the DS1307 class.
rtc.get(uint8_t *sec, uint8_t *min, uint8_t *hour, uint8_t *day, uint8_t *month, uint16_t *year)
This function of the DS1307 class gives information related to the time and date saved in the module.
How to save the time in RTC module?
To save the code you can run the following code:
/*
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*/
/*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();
}
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);
}
In the above code you see a function rtc.set(12, 20, 08, 24, 12, 2014);. This function is used to set the date and time.
After uploading the above code, please comment on the above line of code otherwise the TIME and DATE saved in the module will be reset each time you upload the code.
So, in this way, our RTC module is ready to share real-time and date with us and ready to use in any of your projects. In the following example, we have interfered the LCD with Arduino and are displaying real-time on it.
You can learn here the interfacing of 16x2 LCD with the Arduino.
Arduino code for LCD And DS1307 RTC module
/*
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>
#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
DS1307 rtc;
void setup()
{
/*init Serial port*/
Serial.begin(9600);
lcd.begin(16,2);
while(!Serial); /*wait for serial port to connect - needed for Leonardo only*/
/*init RTC*/
Serial.println("Init RTC...");
/*only set the date+time one time*/
//rtc.set(10, 20, 4, 4, 2, 2019); /*10:20:04 4.2.2019 //sec, min, hour, day, month, year*/
/*stop/pause RTC*/
// rtc.stop();
/*start RTC*/
rtc.start();
}
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*/
lcd.setCursor(0,0);
lcd.print("Time:");
Serial.print("\nTime: ");
lcd.setCursor(6,0);
lcd.print(hour);
Serial.print(hour, DEC);
lcd.setCursor(9,0);
lcd.print(":");
Serial.print(":");
lcd.setCursor(10,0);
lcd.print(min);
Serial.print(min, DEC);
lcd.setCursor(12,0);
lcd.print(":");
Serial.print(":");
lcd.setCursor(13,0);
lcd.print(sec);
Serial.print(sec, DEC);
lcd.setCursor(0,1);
lcd.print("Date:");
Serial.print("\nDate: ");
lcd.setCursor(6,0);
lcd.print(day);
Serial.print(day, DEC);
lcd.setCursor(9,0);
lcd.print(".");
Serial.print(".");
lcd.setCursor(10,0);
lcd.print(month);
Serial.print(month, DEC);
lcd.setCursor(11,0);
lcd.print(".");
Serial.print(".");
lcd.setCursor(12,0);
lcd.print(year);
Serial.print(year, DEC);
/*wait a second*/
delay(1000);
}
Conclusion
In this way, we learned to work with the DS1307 RTC module. If there is any doubt, please let me know in the comment section. In our next blog, we will discuss the top 25 sensors that we can use using the Arduino board