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.
DS1302 RTC Module Pinout
This module has 5 pins:
- VCC: Module power supply – 5V
- GND: Ground
- CLK: Clock pin
- DAT: Data pin
- RST: Reset (Must be HIGH for active mode / Active High)
You can see the pinout of this module in the image below.
Required Materials
Hardware Components
Software Apps
Interfacing DS1302 RTC Module with Arduino
To establish a connection between Arduino and the DS1302 module, follow these steps:
Step 1: Circuit Setup
The following circuit shows how you should connect Arduino to DS1302 module. Connect wires accordingly.
Step 2: Library Installation
Go to Library manager and install the Rtc by Makuna library.
Tip
If you need more help with installing a library on Arduino, read this tutorial: How to Install an Arduino Library
Step 3: Code Code Implementation
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>
ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
void setup ()
{
Serial.begin(9600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
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
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
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);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
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!");
}
delay(5000); // five seconds
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
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);
}
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.
Here’s the output you can expect to see:
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 .
Comments (25)
Will the RTC keeps updating the time when the controller is restarted?
Hi.
Yes, the RTC won’t be restarted and will keep updating the time when you restart or even power down the microcontroller, provided that you use a battery for it.
Mil gracias!!!!
Merci beaucoup
Hi.
your welcome.
I have problem.
Compilation error: ‘printDateTime’ was not declared in this scope
I dont know what can i do with this. Thanks for your support
Hi Dear
please be sure that you install DS1302 library.
this function that not declared had been defined in DS1302 library
hi
it says that the library threewire.h doesnt exist.
i dont know how to fix this.
thanks for the support.
Hi
please add below library to project path:
https://github.com/Makuna/Rtc/blob/master/src/ThreeWire.h
Hi, I’m using this with an ESP32 instead and something weird is happening; it goes through the setup part but then does a hard reset by itself.
Any ideas what could be causing this?
Hi Martin,
Which one resets? Esp32 or DS1302? If DS1302, check its battery (the voltage should range from 2.7 to 3.3)
Hi. Is there a way I could only get hours and minutes in some kind of variable. I need it for checking is time right for some actions to be performed. This is too complicated cause I dont know how time is stored, cuz I cant see the code inside the library.
Hi jxcjsa,
In the ‘loop()’ section, you can remove the line ‘printDateTime(now);’ and use ‘now.Hour();’ to obtain the Hour value. Similarly, for the minute, you can use ‘now.Minute();’ with the corresponding line
On the serial monitor it keeps showing RTC lost confidence in time and keeps showing that the time is 00/00/2000 00:00:01. Any common causes for this?
Hi Nana,
There are three things that could cause this issue:
1.Loose connection between the two modules.
2.Loose power connection.
3.Low battery in the module.
excuse me sir i have the same problem as Nana, on the serial monitor it keeps showing RTC lost confidence in time and keeps showing that the time is 00/00/2000 00:00:01.
I use a 3V battery on the rtc module. I have checked the connections and and everything seem okay. Can you help me please?
Good day, Sir! The compiled time is correct, but the following time says 1/29/2094.
Showing the same time as compilation time…
Hi Mohammad,
Check your module’s connection and battery voltage.
Why is the output of mine the first compilation of time is correct then the loop part of printing date and time is not the same on the first compilation output
like this:
01/14/2036 09:15:20
01/14/2036 09:15:25
01/14/2036 09:15:30
01/14/2036 09:15:35
01/14/2036 09:15:40
01/14/2036 09:15:45
01/14/2036 09:15:50
01/14/2036 09:15:55
01/14/2036 09:16:00
Hello,
When you compile the file at the line
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
, the compiler captures your system’s current time and uploads it to the Arduino. If you reset the device, the module resets to this time, creating a gap between the two times. To fix this, you can upload the code once, then remove lines 31 to 70, and upload it again. This may help.Also, check the battery voltage of the DS1302; it should be around 2.7 to 3 volts to function properly.
Good day sir, the time does not increment on the serial monitor. What could be the cause of this? Thank you for your reply
Hi Mathew,
Check your module’s connections and battery voltage (typically between 2.7 to 3 volts). Make sure to test the battery voltage when it is disconnected from power.
I have the same error as evryone above but i want it to display it on a lcd 16x 2 display and its not working
Hi Anzelmo,
What error are you encountering, and which troubleshooting methods have you tried?
To send data to the LCD, convert the time data to a string and print it on the LCD as text. Also, make sure to clear the LCD before printing the time to avoid overlapping data.