RTC on ESP8266 not working

Hi guys! I'm working on an alarm clock using ESP8266, nextion touch display, and buzzer. I'm first trying to interface RTC DS1302 to ESP8266. Here's the pin configuration and code of the RTC DS1302:

// 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(57600);

    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(10000); // ten 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);
}


I just ran the Rtc by Makuna sample code. I have a new RTC and a new battery. However in the serial monitor, it says "RTC lost confidence in the DateTime!"

Thank you for the help!

The first time the RTC is powered after it is manufactured, it usually defaults to a clock disabled mode.
To get out of that mode, you usually set the time again.

hi amitf, by setting the time again, you mean how?

Try this

#include <Ds1302.h> // include the DS1302RTC library
#include <Time.h> // include the Time library

// Define the DS1302 connections
const int RTC_CE_PIN = 2; // RTC chip enable
const int RTC_IO_PIN = 4; // RTC data line
const int RTC_CLK_PIN = 5; // RTC clock line

DS1302RTC rtc(RTC_CE_PIN, RTC_IO_PIN, RTC_CLK_PIN); // create an instance of the DS1302RTC class

void setup() {
  // Set the serial communication baud rate
  Serial.begin(9600);
  // Start the DS1302
  rtc.begin();
  // Set the time to 10:00 AM on Monday, April 28th, 2023
  tmElements_t tm;
  tm.Year = 123; // year 2023 - 1970 = 53, so 2023 is year 53 in the tmElements_t structure
  tm.Month = 4;
  tm.Day = 24;
  tm.Hour = 10;
  tm.Minute = 0;
  tm.Second = 0;
  tm.Wday = 1; // Monday
  rtc.write(tm); // write the time to the DS1302
}

void loop() {
  // Read the current time from the DS1302 and print it to the serial monitor
  tmElements_t tm = rtc.get();
  Serial.print("Time: ");
  Serial.print(tm.Hour);
  Serial.print(":");
  Serial.print(tm.Minute);
  Serial.print(":");
  Serial.print(tm.Second);
  Serial.print(" ");
  Serial.print(tm.Day);
  Serial.print("/");
  Serial.print(tm.Month);
  Serial.print("/");
  Serial.println(tm.Year + 1970); // year 53 + 1970 = 2023
  delay(1000); // wait for 1 second before reading the time again
}

Can you post a pic of your connection

may i ask what library u used for the ds1302rtc?

You cannot use a bare ds1302 chip like that. Other components are required, such as a crystal and capacitors. You mentioned a battery before, but I don't see it in your diagram.

ahh i meant a coin cell battery for rtc

i'm using this module

Always give a diagram that accurately shows the components/modules you are using, otherwise you could waste time of the people trying to help you.

If you can't find the correct component in your drawing package, and have to use some other component, at least put a note explaining what you did on the diagram.

Can your NodeMCU connect to wi-fi and internet where it will be used? If so, no RTC is needed. The NodeMCU can get time from time servers on the internet (NTP servers) every few hours and use it's internal clock to keep track of time between.

oh okayy i'll take note of that. thank you!

i'm aware of ntp server. however, it doesnt work without wifi connectivity. and part of the requirement is have it running even without wifi

1 Like

Download from here -> <Ds1302.h>
Time.h

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.