DS1302 RTC Module Features For Accurate Timekeeping
DS1302 RTC Module Features For Accurate Timekeeping
The DS1302 real time clock module is a cheap module with high accuracy that can be used in different
projects. This RTC module provides seconds, minutes, hours, day, date, month, and year information. In this
module, date is set automatically based on whether the month is 29, 30 or 31 days and also it is leap year or
not. (That’s only valid until the year 2100)
Note
Please be aware that this module does not use I2C communication. Interfacing the DS1302 with a
microcontroller is done using a synchronous 3-wire serial communication.
You can see the pinout of this module in the image below.
Required Materials
Hardware Components
Arduino UNO R3 × 1
Arduino IDE
The following circuit shows how you should connect Arduino to DS1302 module. Connect wires
accordingly.
Step 2: Library Installation
Upload the following code to Arduino. After that open Serial Monitor.
/*
Modified on Nov 25, 2020
Modified by MehranMaleki from Arduino Examples
Home
*/
// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND
#include <ThreeWire.h>
#include <RtcDS1302.h>
void setup ()
{
Serial.begin(9600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
void loop ()
{
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power
line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}
Arduino
Copy
In this code, at first, the time information is given to module as the starting point. Then module starts
working and the updated time appears on Serial Monitor every 5 seconds.
By following these steps, you can successfully interface the DS1302 RTC module with Arduino and achieve
accurate timekeeping in your projects. For more information and tutorials on Arduino and other related
topics, explore our website .