RTC DS_3231 shows inaccurate results in a big program context

Hello there. By this moment I have been able to create a digital clock using RTC DS_3231 module. The problem is that when I test it with just a display alone (I can't set other time intervals afterwards) everything is fine and it works properly for more than 12 hours with only about 1 second decline. But when I use it in a big program which uses classes and other components it fails periodically (once an hour or so). I have logged all time intervals in HH:MM format with each second output to the *.txt file and was able to define those inaccuracies. For example, program was reaching only 19-th or 52-nd second twice within two hours and then was reset to zero second again working accordingly to the time etalon (that is I was able to see those 19 and 52 seconds inconsistencies on the real clock I was using as an etalon). The program I have written is very big, but if needed I can add its code here, on the forum. In general, I just want to know why it always happens to my program and I can't see the correct results. Hours and minutes depend on those "broken" seconds, so my clock is always some seconds or even minutes ahead or behind real time, though after each "break" (such breaks happen rarely) it counts precisely as 60 seconds for each minute.

Mostly likely, incorrect code. Post the code, using code tags, and explain what you have done to debug it.

Are there other devices on the I2C bus besides the DS3231?

Are you reading the time from the DS3231 directly, using it to sync the clock in the Time library, using the square-wave output to advance a clock by an interrupt, or some other method?

jremington, groundFungus, david_2018, thank for your replies. The point is that it works properly when I test it as a model in special program, but fails for reality. It's rather a hardware problem, not the code itself. Now I have been translating comments for my program into English to let you undertand it better.

groundFungus, I2C is connected to Arduino Nano and multiplied on the mini breadboard for both LCD 1602 display and RTC DS_3231, so yes, I2C is on more than one device.

Please post the minimal code that demonstrates this problem. If this really is a hardware problem, one or more of the DS3231 library examples would almost certainly show it.

Keep in mind that breadboards are unreliable, as are poor solder joints to modules, so connections are always the first places to inspect and verify.

Finally, there are plenty of counterfeit DS3231 modules on Amazon, Ebay, Alibaba, etc. Unless you bought yours from a reliable/authorized distributor, all bets are off.

jremington, thank you. I will take each of your advices into account. Have a nice day.

P.S. Minimal code hardly shows anything. It's all connected in my program. If you still need it, I will post it. Please, let me know. It has 1086 lines of code with comments in English.

It seems unlikely that the time keeping would fail unless the clock registers are being written to. But it's possible for that to happen if you are switching back and forth between Vcc power and coin cell power for the DS3231 and the /EOSC bit is left high. Then the oscillator would stop when you switch to the coin cell. But even then, I don't think it resets seconds to zero. It just freezes the current value until Vcc power is restored. So I think if seconds is resetting to zero at 52 or 19 seconds, something is writing that zero into the seconds register.

Perhaps another possibility is that the RTC is correct, but the time values you read are being corrupted by the microcontroller because the stack is filling up, or something like that. Perhaps after it has messed up, you could run a simple example sketch that just reads and displays the current time in the RTC. That would tell you if the RTC time really has been corrupted.

I did a test with RTC and only display alone and it worked properly after one night. Also it worked ok when I just initialized RTC in setup(). The problem occurs when I try to set the clock with remote control. So, when I confirm the data and call rtc.adjust(...) function again (after setup) this issue begins after some time, though it always ticks properly after each break. Seconds just go zero and time is broken. It's strange as the same function outputs the data in all mentioned cases.

Can you provide minimal example code which demonstrates the issue. Something more than the code which functions correctly but less than the 1086 line monster.

cattledog, thank you for your reply. The point is that Wokwi model works for my project properly. I tend to think it's been a hardware conflict, but I am not sure. I can give you a Wokwi link from where you can observe the scheme itself and the code which actually works. Please, let me know if you still need a minimum code which will take some time for me to prepare.

I am a novice with RTC and couldn't get your question correctly. Would you, please, explain it in more detail or just rephrase it? Actually, I use RTClib.h and Wire.h libraries to run RTC for which I invoke their native methods.

Would you, please, let me know where I can get an original one?

I will try other libraries as well, though I use Adafruit which I heard is a good supplier of hardware libraries.

Or around 12 minutes in a year. That's probably normal for DS3231. I don't think you have a poor quality clone.

The DS3231 does not know or care how large or complex your code running one the Arduino is. So long as the Arduino is only reading the time from the RTC, the accuracy won't be affected. But if the code is writing the time to the RTC or changing other registers, that could explain the problem.

You posted on the forum hoping for expert advice from those with more experience than yourself? You don't need me to tell you that everyone here thinks it is a software problem!

Thank you for your reply. I will do more thorough tests on this issue (seconds go zero from time to time) and post the results here.

And excuse me for incorrect section to choose when I created this topic. It should has been a hardware problem from the very start, of course. I wish I could switch the section now to address it there.

It should be quite a bit better. I don't know how much trouble one has to go to to tune this chip, but out of the box it shoukd only be a few minutes i a year.

I do agree, in any case, that there is (now) no evidence for the idea of a bad module.

@fixer_84 please do post a link to your simulation. You can use the Share button there and paste it in your next post on this thread.

TIA

a7

There is the DS3231M version that uses a slightly less accurate clock source instead of the crystal.

Never heard of it! THX.

From the data sheet

Timekeeping Accuracy ±5ppm (±0.432 Second/Day)

and from the "real" DS3231 data sheet

 Accuracy ±2ppm from 0°C to +40°C

Both claim better than being out three minutes over a year.

The good old DS1307 is by comparison wildly inaccurate, but does have a nice 56 byte user available SRAM for the odd things you might want to enjoy placing in custody of its battery backup scheme.

And you may have purchased a few too many DS1307 some time ago, never mind how long precisely.

a7