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
Arduino UNO R3 × 1
DS1302 RTC Module × 1
Male to Female jumper wire × 1
Software Apps
Arduino IDE
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 ()
{
[Link](9600);
[Link]("compiled: ");
[Link](__DATE__);
[Link](__TIME__);
[Link]();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
[Link]();
if (![Link]())
{
// 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
[Link]("RTC lost confidence in the DateTime!");
[Link](compiled);
}
if ([Link]())
{
[Link]("RTC was write protected, enabling writing now");
[Link](false);
}
if (![Link]())
{
[Link]("RTC was not actively running, starting now");
[Link](true);
}
RtcDateTime now = [Link]();
if (now < compiled)
{
[Link]("RTC is older than compile time! (Updating DateTime)");
[Link](compiled);
}
else if (now > compiled)
{
[Link]("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
[Link]("RTC is the same as compile time! (not expected but all
is fine)");
}
}
void loop ()
{
RtcDateTime now = [Link]();
printDateTime(now);
[Link]();
if (![Link]())
{
// Common Causes:
// 1) the battery on the device is low or even missing and the power
line was disconnected
[Link]("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"),
[Link](),
[Link](),
[Link](),
[Link](),
[Link](),
[Link]() );
[Link](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.
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 .