Reason for Date Time not keeping time


I know this has been asked before and I have read these threads but none of them seem to related to myself.

I have a DS3231 RTC board with 3v battery all attached to a Mega

When I set the date/time with the command rtc.adjust(DateTime(F(DATE), F(TIME))); the RTC keeps time and there is a red led on the RTC. I then comment out this line and re-upload the script. Let's say the Date/Time is set at 27th Dec 2018 @ 13:00

If I then unplug the USB (and thus no power) and leave the board say for an hour and then apply power the Date time I should expect the Date/time to 27th Dec 2018 @ 14:00 but it is still at 27th Dec 2018 @ 13:00.

I have tried different RTC Boards, different 3v batteries (checked voltage 3.40v), Modified the RTC battery contacts to get better connections, tried on 2 Uno's and also another Mega. None of which keep the time.

Is there any other issues to look at?

Thank you.

maybe post the simplest code you have which demonstrates the problem and say from where you got the RTC library.

Run the library example to see it that works ok .
I take it you are using a flat type cell fitted onto the DS3231 board , and this is not connected to anything else.

Your error must be there somewhere

If the RTC weren't running at all, it wouldn't stay at the set time. If it is some random glitch it would be a zillion to one that that random error would hit the time you used to set the RTC to. I'd say the best bet is that something went wrong with the upload of the script after you commented that line out. Each time you connect the Serial port, a reset is caused and the original program is resetting the clock to the requested time.

This is the sample code. Uncomment the rtc.adjust and upload to set the Date/time, then comment the rtc.adjust and reupload so that RTC should now have the correct time.

I then unpowered the Mega for an hour and then plugged it back in and brought up the serial monitor and I get the time it was turned off instead of an hour later as expected.

The library is the RTCLib 1.2.0 from Adafruit
The RTC board is from Banggood

Let me please iterate the issue to be clear. The RTC keeps perfect time whilst the board is powered. When it is unpowered it stores the time when the board is switched off and when the board is next powered up it starts the time from the stored time and does not KEEP(increment the time) time when unpowered. ie it is 15:00, unpower the board. wait some time (say 30mins) and power up board. the time is 15:00 and not the proper time which should be 15:30

// RTC Vars
#include "RTClib.h"
RTC_DS3231 rtc;

void setup() {
 // Initialize the RTC
 if (!rtc.begin()) {
   Serial.println("Couldn't find RTC");
   while (1);
 // Enable this rtc.adjust only when wanting to reset the time on compile
 //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
   Serial.print("Date ");

void loop() {             

void getDate() {
    DateTime now =;
    Date_Year[0] = (now.year() / 1000) % 10 + '0'; //To get 1st digit from year()
    Date_Year[1] = (now.year() / 100) % 10 + '0'; //To get 2nd digit from year()
    Date_Year[2] = (now.year() / 10) % 10 + '0'; //To get 3rd digit from year()
    Date_Year[3] = now.year() % 10 + '0'; //To get 4th digit from year()
    Date_Month[0] = now.month() / 10 + '0'; //To get 1st digit from month()
    Date_Month[1] = now.month() % 10 + '0'; //To get 2nd digit from month()
    Date_Day[0] = / 10 + '0'; //To get 1st digit from day()
    Date_Day[1] = % 10 + '0'; //To get 2nd digit from day()
    Date_Time[0] = now.hour() / 10 + '0'; //To get 1st digit from day()
    Date_Time[1] = now.hour() % 10 + '0'; //To get 2nd digit from day()
    Date_Time[2] = now.minute() / 10 + '0'; //To get 1st digit from day()
    Date_Time[3] = now.minute() % 10 + '0'; //To get 2nd digit from day()
    Date_Time[4] = now.second() / 10 + '0'; //To get 1st digit from day()
    Date_Time[5] = now.second() % 10 + '0'; //To get 2nd digit from day()

Hope this helps.

have you tried replacing the battery ?

All the RTC DS3231 modules i have bought have no reverse polarity protection, i bought 3 last this year in total, all because i had mistakenly cross the wires at some point, if the red light stays on when u disconnect the power or add a new battery its dead you killed the cmos chip and thus it will never save the values.

Check the oscillator enable bit is set correctly for backup operation.

You can set it so that the oscillator (and clock) runs OK when powered from VCC, but stops (so the clock freezes with no time updates) when VCC is off and its running from Vbackup.

See datasheet.

Lots of interesting things in datasheets.

Thanks for the replies.

I have given up and going to buy new RTC boards. They don't cost that much.