DS3231 RTC not display current time

so i've been working with RTC module DS3231 several times, i stop the program and unconnect it with the power, when I connect it again it displays current time same as the time on my phone or laptop, but tonight i connect the power again and the module doesn't display the right time. i search the solution in several posts, i think it's because the RTC module lost power. so I give it a try if it's true that my RTC lost power. but it turns out that serial monitor doesn't print line 19 at all so maybe lost power are not the problem, so what's wrong and how to set the module again? i tried to re-upload the program but it still displays the wrong time.

when it must be 21:mm:ss, it displays 00:mm:ss instead, the minutes and second are not different. i'm waiting till 22 (10 pm), and it display minutes and second true, but the hour still 00, also it gives me wrong dates in 2021 even i didn't adjust the date time cause I truly need the current time

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

RTC_DS3231 rtc;

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

void setup () {
  Serial.begin(115200);

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!"); //this is line 19
    // When time needs to be set on a new device, or after a power loss, the
    // 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));
  }

  // When time needs to be re-set on a previously configured device, the
  // 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();

    delay(3000);
}

Why did you start this topic in the Uploading category of the forum ?

It has been moved to the Programming category

alright, i'm sorry, i'm still new with this forum, i'm sure to post on the right category next time, thank u for reminding me

If the battery is dead, you will lose correct time. Since you show a rtc. lostpower function, you seem to be aware of what to do. There are a lot of ways to get the time, the most accurate being using an NTP server, but if WiFi is not available, then a human input will be needed. You can use Serial for that.

but i just bought this new module

i re-uploaded the code (i was work on another program but i want to try it again for the last time before ended my work tonight) it turns out the RTC display the right current time, does anybody know how this can happen? is it the effect of using it for several times? if it happens again and i should re-upload the program again and again whenever it displays the wrong time then it must be unsuitable for a long terms use, right?

Sorry, I don't understand what your problem is. An RTC once set with a new battery will keep the correct time for close to 8 years. A 3231 is very accurate, but a monthly, weekly or daily adjustment of a few seconds is not uncommon. This is normally done by consulting an NTP server OR using a GPS module, but that is far less common.
BTW, what board is the RTC connecting to.
The DS3231 library by Andrew Wickert has an example called DS3231_set that you can use as a model, it uses the Serial input which I sometimes use in order to set a non-current date/time for testing future dates and/or times.
If you prefer an NTP approach then the following code snippet will do that, you need to fill in the libraries needed and fit it into your sketch

RTC_DS3231 rtc;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 0, 60000);  // NTP server, UTC, update interval 60s

void setup() {
  Serial.begin(115200);
  Wire.begin();

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

  // Connect to Wi-Fi
  WiFi.begin(SSID, PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting time...");

    // Start NTP client
    timeClient.begin();
    timeClient.update();

    // Set RTC time from NTP
    time_t rawTime = timeClient.getEpochTime();
    DateTime now = DateTime(rawTime);
    rtc.adjust(now);
    Serial.println("RTC updated from NTP");
  }

Is this the DS3231 module you're using?

If so, and if you have a CR2032 installed, you have removed at least one of R5 and D2 as shown in the schematic below? That's part of a charging circuit that's intended for a rechargeable coin cell. The CR2032 isn't. And if memory serves, the charging circuit only really works at 3.3V anyhow, but I could be remembering that incorrectly.

yea, it's exactly same with the picture with battery CR2032, i didn't removed any part of it, I try again today, and it display right time, maybe there somethin' that i still not realized makes my RTC display wrong time yesterday

thank u so much for ur suggestion, maybe something wrong that i don't realize make my rtc display the wrong time, it works properly today, and i hope it stays so for the later days.

@van_der_decken That ZS-042 module has a lot wrong with it. Get a real DS3231. I use Chronodot. The V2 is probably what you want, and the V3 adds an EEPROM if you also need an EEPROM. I even buy the raw DS3231 chips. The important thing is both the raw chips and the Chronodot work EXACTLY the way the code suggests. the ZS-042, not so much.
I HATE the ZS-042 and hate trying to help people who use it and say it's a DS3231. It is NOT.

I can understand sir, as a newbie, it's very confusing to me that some modules or development boards are better known by the name of the chip used rather than the name of the module itself, in this case it should be ZS-402 rather than DS3231, and most people misinterpret that ZS-402 i2c module as DS3231 just because it has DS3231 on it, even on the component online store they never mention ZS-402 name, so hearing ZS-402 is really new to me when actually THAT'S the module that I used :slightly_smiling_face:

Yes, that board, in particular, gets confused a lot. It also won't work anywhere near as good as a genuine chip/board. To get a quick handle on how to get the best, do the following search, grab a coffee and start reading. The search is contained in []
here it is [@ShermanP ds3231] and Sort by latest post. There are more than 50 posts, so after you read a dozen or so you can likely start to be selective. The first step is to get, read, and understand the datasheet. It contains a LOT of information including how to set alarms, and the settings for the control register. Here is a link DS 3231 Datasheet Good luck.

roger sir, thank u so much for ur advice