Introduction
The RTC stands for Real-Time Clock. RTC modules are TIME and DATE remembrance systems with a battery configuration that keeps the module operating without external power. The TIME and DATE are kept up to date due to this. As a result, we can get precise TIME and DATE from the RTC module at any time. There are different RTC modules available. This tutorial will interface the ” DS1307 RTC Module with Arduino UNO”.
DS1307 RTC Module
The DS1307 is a low-power Full Binary (BCD) RTC with 56 bytes of SVRAM that uses the I2C protocol to communicate. The IC is usually powered from Vcc and will automatically switch to battery power as needed.
Technical Specifications
- I2C Interface RTC IC
- Operating Voltage: 5V
- Less than 500nA current when operating with a battery
- 56bytes SVRAM
- Operates in power or battery mode
- Programmable square wave output pin
- Available in PDIP and SO package
Pinout
Pin Number | Pin Name | Description |
1,2 | X1, X2 | Crystal Oscillator should be connected to these pins |
3 | V-Bat | Connected to the Positive terminal of the battery |
4 | Ground | Ground pin of the IC |
5,6 | SCL and SDA | Pins for I2C communication with CPU |
7 | SQW / Out | Square wave output driver pin to obtain square wave frequencies. |
8 | Vcc | Powers the IC typically 5V |
Hardware Required
S.no | Component | Value | Qty |
---|---|---|---|
1. | Arduino | UNO | 1 |
2. | USB Cable Type A to B | – | 1 |
3. | Jumper Wires | – | – |
4. | RTC Module | DS1307 | 1 |
5. | Bread Board | – |
Circuit Diagram
Connection Table
Arduino | DS1307 RTC Module |
---|---|
A5 | SCL |
A4 | SDA |
5V | VCC |
GND | GND |
Arduino Code
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup ()
{
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC lost power, lets set the time!");
// Comment out below lines once you set the date & time.
// Following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Following line sets the RTC with an explicit date & time
// for example to set January 27 2017 at 12:56 you would call:
// rtc.adjust(DateTime(2017, 1, 27, 12, 56, 0));
}
}
void loop ()
{
DateTime now = rtc.now();
Serial.println("Current Date & Time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println("Unix Time: ");
Serial.print("elapsed ");
Serial.print(now.unixtime());
Serial.print(" seconds/");
Serial.print(now.unixtime() / 86400L);
Serial.println(" days since 1/1/1970");
// calculate a date which is 7 days & 30 seconds into the future
DateTime future (now + TimeSpan(7,0,0,30));
Serial.println("Future Date & Time (Now + 7days & 30s): ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
Working Explanation
Connect the circuit according to the schematic to interface theDS1307 RTC Module with Arduino. In your Arduino, paste the code above. You should then upload that code. To see the readings, open the Serial monitor. There you will see the date, time, and other details.
Code Explanation
- To communicate with the module, the code begins by adding the wire.h and RTClib.h libraries. Then, to hold days information, we construct an RTClib library object rtc and declare the daysOfTheWeek 2D character array.
- In the void setup, we use different functions. For example, The begin() function checks if the RTC module is connected, while the isrunning() function examines the DS1307’s internal I2C registers to see if the chip has lost time. If this function answers false, we may set the date and time using the adjust() function.
- In the voidloop, We print the values on the serial monitor by using different functions. The function now() returns the current date and time. Its return value is typically saved in a DateTime datatype variable; year() returns the current year; month() returns the current month; day() returns the current day; dayOfTheWeek() returns the current day of the week. This function is often used as an index into a two-dimensional character array that maintains information about days; hour() delivers the current hour; minute() returns the current minute; second() returns the current seconds; and unixtime() returns the current unix time in seconds. Unix time is a way of describing a specific point in time. To add/subtract time from/to the current time, use the TimeSpan() method. Days, hours, minutes, and seconds can all be added or subtracted.
Application and Uses
- The RTC records information such as seconds, minutes, hours, days, dates, months, and years. Hence, can be used in digital clocks