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);
}
