Contents

Interfacing DS1302 RTC Module with Arduino

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 () { 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 .

Liked What You See?​
Get Updates And Learn From The Best​

Comments (25)

  • Narenthira Prasath D Reply

    Will the RTC keeps updating the time when the controller is restarted?

    September 22, 2021 at 8:49 am
    • Mehran Maleki Reply

      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.

      September 25, 2021 at 6:53 am
  • Javier Reply

    Mil gracias!!!!

    October 20, 2022 at 2:18 pm
  • Jack Reply

    Merci beaucoup

    February 2, 2023 at 12:18 am
    • Ali Abdolmaleki Reply

      Hi.
      your welcome.

      March 4, 2023 at 11:25 am
  • Vlada Reply

    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

    February 2, 2023 at 3:57 pm
    • Ali Abdolmaleki Reply

      Hi Dear
      please be sure that you install DS1302 library.
      this function that not declared had been defined in DS1302 library

      February 19, 2023 at 5:30 am
  • miles Reply

    hi
    it says that the library threewire.h doesnt exist.
    i dont know how to fix this.
    thanks for the support.

    February 26, 2023 at 9:54 am
  • Martin Reply

    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?

    August 10, 2023 at 10:57 am
    • Mohammad Damirchi Reply

      Hi Martin,
      Which one resets? Esp32 or DS1302? If DS1302, check its battery (the voltage should range from 2.7 to 3.3)

      August 13, 2023 at 9:15 am
  • jxcjsa Reply

    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.

    December 6, 2023 at 10:50 am
    • Mohammad Damirchi Reply

      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

      December 9, 2023 at 6:21 am
  • nana Reply

    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?

    December 13, 2023 at 1:16 pm
    • Mohammad Damirchi Reply

      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.

      December 16, 2023 at 1:11 pm
  • agnes Reply

    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?

    December 18, 2023 at 6:11 pm
  • JM Sto. Domingo Reply

    Good day, Sir! The compiled time is correct, but the following time says 1/29/2094.

    December 27, 2023 at 6:02 am
  • Mohammed Yasar Reply

    Showing the same time as compilation time…

    January 18, 2024 at 11:18 am
    • Mohammad Damirchi Reply

      Hi Mohammad,
      Check your module’s connection and battery voltage.

      January 20, 2024 at 5:55 am
  • Evan GGG Reply

    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

    May 21, 2024 at 3:14 am
    • Mohammad Damirchi Reply

      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.

      May 25, 2024 at 8:30 am
  • Mathew Alvero Reply

    Good day sir, the time does not increment on the serial monitor. What could be the cause of this? Thank you for your reply

    May 21, 2024 at 11:11 pm
    • Mohammad Damirchi 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.

      May 25, 2024 at 8:35 am
  • Anzelmo Reply

    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

    May 26, 2024 at 12:40 pm
    • Mohammad Damirchi Reply

      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.

      May 27, 2024 at 5:21 am

Leave a Reply

Your email address will not be published. Required fields are marked *