DS3231 not keeping time when powered off

Hello, I am using a pro-mini and a DS3231 module which I'd like to use in order to manage interrupts.
Mine seems to belong to the type with the charging circuit issue

This

What I did so far:

-Measured Vbat between the Vbat and Ground pin directly on the pin, reading 3,11 V
-Destroyed the charging circuit Diode in order to skip the cargin issue

  • Manually included in the Sketch's setup part commands to keep the oscillator register set to low which should keep the oscillator going

Still having, when unplugging and pluggin usb cable, time reset to the value passed when the sketch was firstly uploaded. Of course I do comment the setTime command after uploading the sketch the first time.

I think I am out of bullets right now.
Does anybody have a hint?

Thanks a lot

Including my sketch

#include <Wire.h>
#include <DS3232RTC.h> 
#include<TimeLib.h>

DS3232RTC rtc ; 
   

void setup() {
  Serial.begin(57600);
  delay(3000);
  Wire.beginTransmission(0x68);
  Wire.write(0xF);                            // Address the Status register
  Wire.write(0x00);                           //  Zero the Status register
  Wire.endTransmission(); 
  Wire.beginTransmission(0x68);
  Wire.write(0xE);                            // Address the Status register
  Wire.write(0x00);                           //  Zero the Status register
  Wire.endTransmission(); 

  Wire.begin();
  rtc.begin();
    

setTime(20, 9, 10, 14, 7, 2020);
   
}

//------------------------------------------------------------

void loop() {

 digitalClockDisplay();
 delay(1000);
}

void digitalClockDisplay() {
 // digital clock display of the time
 Serial.print(hour());
 printDigits(minute());
 printDigits(second());
 Serial.print(" ");
 Serial.print(day());
 Serial.print(" ");
 Serial.print(month());
 Serial.print(" ");
 Serial.print(year());
 Serial.println();
}

void printDigits(int digits) {
 // utility function for digital clock display: prints preceding colon and leading 0
 Serial.print(":");
 if (digits < 10)
 Serial.print('0');
 Serial.print(digits);
}

Have you tried running the example sketches that come with the RTC library?

If it's the TimeLib that I've just looked at, then there is an example on how to use the library with a real rtc. I think that there's a line of code you are missing in setup() that tells TimeLib to use a real rtc.

Thank your for the reply.
I did, but still getting the same results.
Uploaded the Ds3231 example that is already in the IDE: I had to slightly edit it in order to set up time not only when RTC is not working (like in the original sketch). I commented the line I edited

This is what uploaded, which is pretty much barebones the original sketch

/ Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);

  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //edited by me

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop () {
    DateTime now = rtc.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since midnight 1/1/1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now + TimeSpan(7,12,30,6));
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}

This is the output: as you can see, I unplugged the usb cable from the FT232 adapter between 22:02:49 and 22:03:13. What occurred is the time slipping 9 seconds back instead of keeping its supposed pace

22:02:40.436 -> 2020/7/14 (Tuesday) 22:2:21
22:02:40.468 ->  since midnight 1/1/1970 = 1594764141s = 18457d
22:02:40.537 ->  now + 7d + 30s: 2020/7/22 10:32:27
22:02:40.569 -> 
22:02:43.496 -> 2020/7/14 (Tuesday) 22:2:24
22:02:43.530 ->  since midnight 1/1/1970 = 1594764144s = 18457d
22:02:43.596 ->  now + 7d + 30s: 2020/7/22 10:32:30
22:02:43.629 -> 
22:02:46.559 -> 2020/7/14 (Tuesday) 22:2:27
22:02:46.592 ->  since midnight 1/1/1970 = 1594764147s = 18457d
22:02:46.659 ->  now + 7d + 30s: 2020/7/22 10:32:33
22:02:46.693 -> 
22:02:49.616 -> 2020/7/14 (Tuesday) 22:2:30
22:02:49.647 ->  since midnight 1/1/1970 = 1594764150s = 18457d
22:02:49.683 ->  now + 7d + 30s: 2020/7/22 10:32:36
22:02:49.748 -> 
22:03:13.359 -> 2020/7/14 (Tuesday) 22:2:21
22:03:13.392 ->  since midnight 1/1/1970 = 1594764141s = 18457d
22:03:13.427 ->  now + 7d + 30s: 2020/7/22 10:32:27
22:03:13.460 -> 
22:03:16.417 -> 2020/7/14 (Tuesday) 22:2:24
22:03:16.417 ->  since midnight 1/1/1970 = 1594764144s = 18457d
22:03:16.484 ->  now + 7d + 30s: 2020/7/22 10:32:30
22:03:16.519 -> 
22:03:19.441 -> 2020/7/14 (Tuesday) 22:2:27
22:03:19.473 ->  since midnight 1/1/1970 = 1594764147s = 18457d
22:03:19.540 ->  now + 7d + 30s: 2020/7/22 10:32:33
22:03:19.573 -> 
22:03:22.496 -> 2020/7/14 (Tuesday) 22:2:30
22:03:22.531 ->  since midnight 1/1/1970 = 1594764150s = 18457d
22:03:22.601 ->  now + 7d + 30s: 2020/7/22 10:32:36
22:03:22.630 ->

I don't know what this coul really depend on.
Thanks

Thanks Mark, am I really missing something?

I thought using DS3232RTC as an object name is what that library expects, or am I wrong?

I do declare the object in the setup here

DS3232RTC rtc ;

Do you think I should change something?
Thank you very much

The line

rtc.adjust(DateTime(F(DATE), F(TIME))); //edited by me

in setup() should only be executed if the rtc is not running.

rtc.adjust() also overwrites the flag that rtc.lostPower() is looking for.

I would comment out your rtc.adjust() call as rtc.lostPower() will call it anyway if the rtc is not running.

Try running the code without that line for, say, 30 seconds. Then press the reset button on your Arduino. Don't remove power. See if the time has carried on counting or if it has reset.

If it keeps counting correctly, try removing power and restarting. If the rtc goes back to the old time, that suggests that the backup battery voltage isn't getting to the rtc. I think we can discount the battery as you have already measured its voltage on the vbatt pin.

Thanks Mark,
it actually worked but in a strange way.

With regards to the second example I tried, the one using RTClib and Wire, it only worked running the sketch first letting my line in, just to set the clock time, then if I tried to comment my line bringing it back to the original example it gave the problem above and didn't keep time. If i keep the line, set the time, close the IDE and load the original example again (not containing my line) then the sequence you suggested works. Basically you have to run the modified sketch once in order to set the time and then shut everything down and load it back without the time setting line, which is bizarre. I got the result I aimed but in an awkward way.

Referring to my first example though, the one using TImelib and DS3232 library, I still doesn't work and gives me 1/1/2000, but I guess if I'd rather use that library instead of RTClib, I should set the time newly using the DS3232 method which doesn't read the right time in case it was previously been set from a different library (RTC lib in this case).

Thanks