The DS3231 RTC module does not retain the time everytime I powered-off or reset

The DS3231 RTC module does not retain the time everytime I power-off or reset…
please help me to keep the saved time even after resetting the arduino

#include <MFRC522.h> 
#include <SPI.h> 
#include <SD.h> 
#include <RTClib.h> 


#define CS_RFID 10
#define RST_RFID 9

#define CS_SD 4 
#define RELAY 3

File myFile;


MFRC522 rfid(CS_RFID, RST_RFID); 


RTC_DS1307 rtc;


const int checkInHour = 9;
const int checkInMinute = 5;


int userCheckInHour;
int userCheckInMinute;


const int redLED = 6;
const int greenLED = 7;
const int buzzer = 5;

void setup() {
    digitalWrite(RELAY, HIGH);
 
  pinMode(redLED, OUTPUT);  
  pinMode(greenLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
   pinMode(RELAY, OUTPUT);
  // Init Serial port
  Serial.begin(9600);
  while(!Serial); 
  

  SPI.begin(); 
 
  rfid.PCD_Init(); 


  Serial.print("Initializing SD card...");
  if(!SD.begin(CS_SD)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while(1);
  }
  else {
    
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  if(!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
  }
}

void loop() {

  if(rfid.PICC_IsNewCardPresent()) 
  {
    readRFID();
    verifyCheckIn();
  }
  delay(10);
}

void readRFID() {
   
  rfid.PICC_ReadCardSerial();
  Serial.print("Tag UID: ");
  String content= "";
  for (byte i = 0; i < rfid.uid.size; i++) 
    {
     Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(rfid.uid.uidByte[i], HEX);
     content.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(rfid.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
   boolean authorized = false;
  char *name = "";
  if (content.substring(1) == "12 9C 7D 1C")  
  {
   authorized = true;
   name = "testing   jim testing"; 
  }
    if (content.substring(1) == "B3 DC B7 73")  
  {
   authorized = true;
   name = "YuKanatSiMi"; 
  }
     if (content.substring(1) == "11 76 04 A3")  
  {
   authorized = true;
   name = "Jim E. Espinola"; 
  }
  
  
  if (authorized) {
   Serial.print("Authorized access by ");
   Serial.println(name);
  tone(buzzer, 2000);
     digitalWrite(RELAY, LOW);
  delay(2000);        
  noTone(buzzer);
          digitalWrite(RELAY, HIGH);
  delay(500);


 
  digitalWrite(CS_SD,LOW);
  

  myFile=SD.open("DATA.txt", FILE_WRITE);


  if (myFile) {
    Serial.println("File opened ok");
    myFile.print(name);
    myFile.print(", ");   
    String content= "";
    
   
    DateTime now = rtc.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    
  
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    Serial.println("sucessfully written on SD card");
    myFile.close();

    userCheckInHour = now.hour();
    userCheckInMinute = now.minute();
  }
  else {
    Serial.println("error opening data.txt");  
  }
  
  digitalWrite(CS_SD,HIGH);
}}

void verifyCheckIn(){
  if((userCheckInHour < checkInHour)||((userCheckInHour==checkInHour) && (userCheckInMinute <= checkInMinute))){
    digitalWrite(greenLED, HIGH);
    delay(2000);
    digitalWrite(greenLED,LOW);
    Serial.println("You're welcome!");
  }
  else{
    digitalWrite(redLED, HIGH);
    delay(2000);
    digitalWrite(redLED,LOW);
    Serial.println("You are late...");
    Serial.println();
  }
}

What happens if you do not use this line of code.
rtc.adjust(DateTime(F(DATE), F(TIME)));

You do have a battery on board.

Does a second RTC board do the same?

the
rtc.adjust(DateTime(F(DATE), F(TIME)));

is for setting time...i guess

i just saw it from the codes of others

You set the time once only.

After that, you don’t need that line of code as the RTC time was set previously.

  • Set the RTC by uploading the sketch.

  • Comment that line of code, then upload the code a second time.

You should also run this code at least once, in setup, when you set the clock:

  //----------------------------------------------------
  // clear /EOSC bit to ensure that the clock runs on battery power
  Wire.beginTransmission(0x68); // address DS3231
  Wire.write(0x0E); // select register
  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
  Wire.endTransmission();

Thank You Everyone!! <3

ChrisTenone:
You should also run this code at least once, in setup, when you set the clock:

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

// clear /EOSC bit to ensure that the clock runs on battery power
  Wire.beginTransmission(0x68); // address DS3231
  Wire.write(0x0E); // select register
  Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
  Wire.endTransmission();

YES you are right this code should also add after RTC.begin() Thanks you saved my time nice job...

ChrisTenone:
You should also run this code at least once, in setup, when you set the clock:

Or you can just remove the battery and put it back, the power on default for EOSC is “clear” (oscillator enabled). In the several years I’ve been using the DS3231, the only time I ever had trouble with EOSC is when I explicitly set it (for long term module disuse) and forgot that I did, on an RTC with a soldered battery.