RTC DS3231, the battery not work

Sorry, the english isn`t may mother tongue, I should ask to Google for aid.

I am work in a clock project. I want to show the local and UTC time of the winter and summer periods in my country.

I am not any problem with the code, its work OK.

I have used some units of each three boards: one with a memory, other prepared for the Raspberri and the third a Adafruit module, all whit the DS3231SN and DS3231S. I have alwais verified that the pin 14 of DS3231 have a voltage upper of 3V from the auxiliar battery. None of the board used has keep the time after disconnecting the main power. Always resume with the time of the last compilation.

The display is a 1602 with I2C port.

This is a mystery to my. I am read some post about this problem but never an explanation that help me to solve the error that I am making both in software and/or hardware.

I would greatly appreciate some kind of help, it would even be enough for me if someone assures me that he has made an assembly with any of these plates and the auxiliary battery has worked for him.

Thanks in advance, Arsenio

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>

const int LAST_DAY_BEFORE_LAST_MONDAY = 24;
const int IS_MARCH = 3;
const int IS_OCTOBER = 10;
const int IS_SUNDAY = 0;
const int HOUR_OF_CHANGE = 2;
const int WINTER_OFFSET = 1;
const int SUMMER_OFFSET = 2;

int initialDay;
unsigned long timeForAction = 0;
enum Period {IS_WINTER, IS_SUMMER};


LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;

String nameOfTheDayOfTheWeek (int d) {
  switch (d) {
    case 0: return "DOMINGO";
    case 1: return "LUNES";
    case 2: return "MARTES";
    case 3: return "MIERCOLES";
    case 4: return "JUEVES";
    case 5: return "VIERNES";
    case 6: return "SABADO";
  }
}

String nameOfMonth (int m) {
  switch (m) {
    case 1: return "ENE";
    case 2: return "FEB";
    case 3: return "MAR";
    case 4: return "ABR";
    case 5: return "MAY";
    case 6: return "JUN";
    case 7: return "JUL";
    case 8: return "AGO";
    case 9: return "SEP";
    case 10: return "OCT";
    case 11: return "NOV";
    case 12: return "DIC";
  }
}

bool isTimeSummer(DateTime dt) {
  if (dt.month() > IS_MARCH && dt.month() < IS_OCTOBER) {   
    return IS_SUMMER; 
  }
  
  if (dt.month() == IS_MARCH) {
    if (dt.day() > LAST_DAY_BEFORE_LAST_MONDAY) {
      if (dt.dayOfTheWeek() == IS_SUNDAY) {
        if (dt.hour() > HOUR_OF_CHANGE) {         
          return IS_SUMMER;   
        } else {          
          return IS_WINTER;
        }
      }
      
      if ((LAST_DAY_BEFORE_LAST_MONDAY - dt.day() + dt.dayOfTheWeek()) < 0) {        
        return IS_SUMMER; 
      }
    }    
  }

  if (dt.month() == IS_OCTOBER) {    
    if (dt.day() > LAST_DAY_BEFORE_LAST_MONDAY) {
      if (dt.dayOfTheWeek() == IS_SUNDAY) {
        if (dt.hour() < HOUR_OF_CHANGE) {          
          return IS_SUMMER;                
        } else {          
          return IS_WINTER; 
        }
      }

      if ((LAST_DAY_BEFORE_LAST_MONDAY - dt.day() + dt.dayOfTheWeek()) >= 0) {       
        return IS_SUMMER; //The result >= 0 ere before the last sunday
      }
    }
  }
  return IS_WINTER; //Any condition for summer
}

int calcUtcOffset (DateTime t) { 
  bool period = isTimeSummer (t);
  
  if (period == IS_WINTER) {
    return WINTER_OFFSET;
  } else {
    return SUMMER_OFFSET;
  }
}

void showDate(String d) {
  int colPosition = int((16 - d.length()) / 2);
  lcd.setCursor(0, 0);
  lcd.print("                ");
  lcd.setCursor(colPosition, 0);
  lcd.print(d);
}

void showLocalTime(String b) {
  lcd.setCursor(0, 1);
  lcd.print(b);
}

void showUtcTime(String b) {
  lcd.setCursor(9, 1);
  lcd.print(b);
}

String processDate (DateTime t) {
  String t_date = String(t.day());
  String nameDOW = nameOfTheDayOfTheWeek(t.dayOfTheWeek());
  String nameMonth = nameOfMonth(t.month());
  
  t_date.concat(" ");
  t_date.concat(nameMonth);
  t_date.concat(" " );
  t_date.concat(nameDOW);

  return t_date;
}

String processLocalTime (DateTime t) {
  int utcOffset = calcUtcOffset(t);
  DateTime dt (t.unixtime() + (utcOffset * 3600));
   
  char buf_time_local[9];
  snprintf(buf_time_local, sizeof(buf_time_local), "%02d:%02d:%02d",
    dt.hour(), dt.minute(), dt.second());
    
  return buf_time_local;  
}

String processUtcTime(DateTime t) {  
  char buf_time_utc[6];
  snprintf(buf_time_utc, sizeof(buf_time_utc), "%02d:%02d",
    t.hour(), t.minute()); 

  return buf_time_utc;
}

void setTimeClock () {
  int initYear = 2020;
  int initMonth = 3;
  int initDay = 30;
  int initHour = 19;
  int initMinute = 24;
  rtc.adjust(DateTime(initYear, initMonth, initDay, initHour, initMinute, 0));
}
 
void setup() {
  Serial.begin(9600);
  rtc.begin();

  lcd.init();
  lcd.backlight();
  lcd.clear();
 
  DateTime nowTime = rtc.now();
  initialDay = nowTime.day();

  String s_date = processDate (nowTime);
  showDate(s_date);

  String s_hour_local = processLocalTime (nowTime); //Sets the String of the local time
  showLocalTime (s_hour_local);

  String s_hour_utc = processUtcTime (nowTime);     //Sets the String of the UTC time
  showUtcTime (s_hour_utc);
 
  setTimeClock();
}

void loop () {
  int delaySeconds = 1; //Time of delay in seconds
  if (millis() > timeForAction) {
    timeForAction = millis() + delaySeconds * 1000;
    
    DateTime nowTime = rtc.now();             //Read the current time on RTC
    if (nowTime.day() != initialDay) {
      String s_date = processDate (nowTime);  //Sets the String variable ofg the date
      showDate (s_date); 
    }
    
    String s_hour_local = processLocalTime (nowTime); //Sets the String of the local time
    showLocalTime (s_hour_local);

    String s_hour_utc = processUtcTime (nowTime);     //Sets the String of the UTC time
    showUtcTime (s_hour_utc);
  }
}

Always resume with the time of the last compilation.

Sure, you actually set it in the setup() routine which is called once in every startup:

  setTimeClock();

So the battery might have kept the time but you overwrite it.

This is a mystery to my. I am read some post about this problem but never an explanation that help me to solve the error that I am making both in software and/or hardware.

We cannot help you with your hardware as you failed to provide any useful information about it. Post schematics/wiring diagrams of your hardware!