DS1307 RTC Not Retaining Time After Power Loss

Hello,
I'm currently working on a project involving the DS1307 RTC. I’ve designed a custom PCB incorporating this RTC, but I'm encountering an issue where the module does not retain the time after power is removed. Instead of maintaining the current time, it resumes from the point where it was last running when power is restored.

Has anyone experienced a similar issue or can suggest potential causes for this behavior? Any insights would be appreciated.

I am using esp32 s.
These are the functions I use to setup and run the rtc on my code :

#include <Wire.h>

char t[32];

void RTCSetup() {
  Wire.begin();
  rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  //rtc.adjust(DateTime(2025, 3, 27, 14, 51, 10));
  
   
}


void RTCloop() {
  
  DateTime now = rtc.now();
  sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());  
  Serial.print(F("Date/Time: "));
  Serial.println(t);
  delay(1000);
}

Note : This code works well and retains the time when using the external rtc DS-1307 module but I face these issues when running the same code on my custom rtc on my PCB.

This is the rtc module schematic :

This is my schematic on my PCB :

Thanks for trying to use code tags in your first post; it did not quite work out.

Code tags for a block of code are three back ticks (```), not a single back tick (`), before and after the code; they need to be on their own line !

I've fixed it for you.

What is this 680K resistor connected to X2?
Never see that before.

Are you using a rechargeable battery?

I’m curious about R25…

Your schematic is overly complicated

This is all you need

It is the same in the module schematic : RTC ds-1307 schematic
No I am not using rechargeable battery.

Well the schematic does not indicate what parts are actually populated.
I can see that R7 is not soldered on.
If you are using a non- rechargeable battery than just connect it to the Bat pin.
As I said all you need is shown in post #5

Got it from a reliable seller?

It does not compile.
Also see post #7

This is a working version of the code:

#include <RTClib.h>

RTC_DS1307 rtc;
#include <Wire.h>

char t[32];

void setup() {
  // put your setup code here, to run once:
RTCSetup();
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
RTCloop();
}


void RTCSetup() {
  //Wire.begin(SDA, SCL);
  Wire.begin();
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  //rtc.adjust(DateTime(2025, 3, 27, 14, 51, 10));
  
   
}


void RTCloop() {
  
  DateTime now = rtc.now();
  sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());  
  Serial.print(F("Date/Time: "));
  Serial.println(t);
  delay(1000);
}

That's fine.
Once you fix your board, it should work.

thank you,
I want to ask another question what is the prepose of the AT24 memory and can I use the ds1307 without it ?

It's a piece of non volatile memory (4Kb EEPROM) that's put on the board - probably to make the product more appealing to anybody wanting to do a small amount of data logging.

You don't need it. The RTC will work fine by itself.

The AT24 has nothing to do with the DS1307, it's not needed for the DS1307 to function.
Why they put it there? Who knows. Almost everything on the schematic is wrong or make no sense.

Again all you need is what is shown in post #5

Also make sure you are using the correct crystal. It must have a load capacitance of 12.5pF

Can you clarify - does the time freeze when power is lost, and then resume when it is restored? So if it has been running for one hour, then power is lost for 10 minutes, does it lose only the 10 minutes? If so, that's very puzzling. It means the oscillator is turned off when Vcc is missing. Since the DS1307 does not have a register setting that would produce that effect, I think the likely culprit is that 680K resistor R26. When Vcc is missing, that becomes a pull-down resistor, which could stop the crystal from oscillating. I can't think of another explanation. It might even have been a deliberate design. But I would certainly try taking out that resistor.

Well I agree with @jim-p that if you are not using a rechargeable cell, then you should just connect it to Vbat. You should remove R24, R25 and R27, as well as D2.