Hi, I'm using the RTC DS3231 to mantain the date and time on my system. The system consisnt in a ESP32 connected to many devices as temperature, illuminance and current sensors, rfid module, a display and the RTC itself. And this module receive data from others ESP01 modules as well.
Anyway, what happened is that I keept the system working for 1 month, but without the current sensors connected. After this I changed the system to another place, I connect the current sensors and the RTC start to reset to the compilation date and time sometimes.
I don't want to associate the problem with the current sensors, but it is the unique thing that comes to my mind, because everything else is the same. I already checked the battery voltage and it's properly connected.
I don't use the ESP so can't comment on that.
I have several DS3231 RTC's running without issues on Mega 2560 boards.
How are you using the DS3231? As a bought-in chip or in a module?
The connections should be straightforward to main supply and backup battery.
As I understand it, the system is fine without the sensors, but something resets the RTC either when you connect the sensors or while they are connected.
With the back up battery in place, it should be possible to disconnect the main supply without losing the set date and time.
Might something be resetting the ESP which leads to reverting to the date and time in the sketch?
Are any of the sensors or other devices connected to the I2C bus? If so, are all of the I2C devices rated to run at 400KHz, or are there some that are 100KHz maximum? Are all I2C devices running at the same voltage? Do you run anything through an I2C multiplexer?
Resetting to the compilation date/time sounds like something in your code setting the RTC date/time because it has determined that the RTC has not been set.
The BH1750, BME280, LCD 20x4 2004a and the RTC DS3231 are connected through I2C. All of the components are 3.3V except the LCD (5V), so I'm using a bidirectional logic level converter.
Are you constantly spamming the I2C bus by reading the RTC repeatedly, or are you using it to sync a system clock at periodic intervals? The ESP32 has an on-board RTC that could be synced to the external RTC at intervals to maintain accurate time, or you can use the Time library that runs a software system clocked synced to the external RTC.
@lucasportoasis - This line in @GolamMostafa code should be run ONE time (forever) to set the time (only one time). Then, edit the code to comment-out this line and upload the sketch without rtc.adjust() to continue using the sketch.
My guess is; you have left this line (one of the two in the example) and your compiled code keeps "resetting" the time.
WHY? That RTC has an alarm function, why not use that to control your sketch rather than constantly checking. You may improve battery life by sleeping between alarms.
Exactly. In fact, if the RTC was being reset, all the registers would be zero. It can only be set to the compile time by your code. So somewhere in your code are instructions to set the RTC to the compile time, and for some reason you are calling that code again. Such code should be run once, then commented out, then the sketch compiled and uploaded again.
Have you understood what has been told in post #15 and #17 with regards to the one time execution of the following intial time-setting code for the RTC
One main problem is that the OP has not posted his code leading to some speculation about it. Having said that, it would be more usual to code something like this as recommended in tutorials:
// from https://randomnerdtutorials.com/esp32-ds3231-real-time-clock-arduino/
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// 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__)));
}
Another common method is to check if say the year delivered by the RTC is plausible ( >= 2026).
If the OP then experiences resets back to the compilation date the backup battery in the RTC could be dead/missing.
Intermittent Arduino crashing is not so unusual but normally that would not force a reset of a properly installed RTC with effective backup power source.