I am having an issue that I was wondering if anyone has seen before. I am using a DS1307 with an Arduino Due as a RTC. Its is connected via I2C, which I also have an EEPROM chip (M24M02-DR) on. They have different addresses. I am using the RTClib (JeeLabs) library to set the time using rtc.adjust(). I then read the values back from the DS1307 using now() a few time a second and store them as year, month, day, hour, minute, second and unixTime. I use the values as they change for delays etc. in my code.
All of this works perfectly but every once in a while the the values returned from the DS1307 are incorrect, This is extremely rare but when it occurs it seems to freeze the DS1307 as all values returned by now() from then on are the same. Polling the power to the Arduino fixes the issue and now() immediately starts to return the correct values.
An example of this occurring would go as follows,
- I set the the time and date on the DS1307 to 05/03/2021 at 12:50:00
- I read this back from the clock and display it on the LCD as 05/03/2021 12:50:00
- A few time a second I check if the value has changed and if so the display is updated
- After a few weeks or months of working perfectly the date/time returns from the DS1307 as 21/21/2021 21:21:21
- The code continues to run correctly reading and display I/O etc but the values returned from the now() function call is always 21/21/2021 21:21:21
I would really like to figure out what could be causing the DS1307 to return the incorrect values, as some of the code I run relies on the time for delays. My issue is that I cannot recreate the problem and it occurs randomly so it is very hard to come up with a fix for it that I could test. I have multiple copies of my hardware setup and running and it will eventually occur on most of them even if it takes months to happen so I can confirm that its not a faulty piece of equipment. Any thoughts or suggestions on this would be greatly appreciated.
Update: Turns out the info wasn't fully correct, the clock being used is actually a DS3231. I thought it was a DS1307 as that is how it is declared in the software (I didn't set the hardware up). So it turns out the library I am using (attached as RTClib1) was created for the DS1307 and I am using it on a DS3231. I have compared it to a branch of itself (attached as RTClib2) which was created by adafruit to work with other RTC's including the DS3231. There are some small differences but I don't think they would cause the issue mentioned as they are addressed (0x68) the same. I have also included the RTC circuit which is connected to the SDA and SCL pins of the due micro controller. Again any thoughts or suggestions on this would be greatly appreciated
RTClib1.cpp (9.2 KB)
RTClib.h (2.93 KB)
RTClib2.cpp (63.7 KB)
RTClib2.h (18.4 KB)