RTC Module 1307 time and date not updating

Hi, i have a problem about my RTC module, Im using RTC module 1307. To update the Time and Date it will be needed to upload again in Arduino.

After how many hours/day/s, Time and Date is not updating after un-pluging the arduino in the laptop and connecting it to a power bank. check the image below

Date should be, Oct 12 2022 Time 11:00:16 AM
datetime

Im using the code below

#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
int P = A3; //Assign power pins for RTC
int N = A2;
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
pinMode(P, OUTPUT);
pinMode(N, OUTPUT);
digitalWrite(P, HIGH);
digitalWrite(N, LOW);
bool parse = false;
bool config = false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
delay(200);
if (parse && config) {
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
} else if (parse) {
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
} else {
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec;
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
tm.Hour = Hour;
tm.Minute = Min;
tm.Second = Sec;
return true;
}
bool getDate(const char *str)
{
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthName[monthIndex]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}

Do u guys have any clue what is the problem?

I'm not clear from your description if the problem is that the time & date are not updating at all when your sketch is running or whether the problem is that the time & date get reset each time you apply power.

I uploaded the code in arduino, and unplugged it and connect to a power bank. Time and Date still works, but after how many hours/day/s, it gets delayed. check the image below

Date should be, Oct 12 2022 Time 11:00:16 AM
datetime

Does your clock have the backup battery installed and if so is it flat ?
Is the time drifting over a period of time or just suddenly showing a different time ?

Without going through your code , it may have some issues ( wonder why int is defined two ways ?) and why two clock libraries ( is timelib getting it’s time from the 1307 or the system clock ?)? . Why not just run one of the examples with the 1307 library to see if it is a clock issue or your code.

Btw the ds3231 is a better clock and uses same instructions as the 1307

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