how to avoid time drift (lagging) due to serial.print() when using RTC chip

i have problems wit serious time drifts when using an arduino uno with an rtc like ds1302, ds1307, or even ds3231. this has to do with this problem:

When I use have serial.print() statements to read out the time of my RTC (ds3231), after a couple of hours there is a time drift of several seconds. Moreover this drift accumulates over time!

I read somewhere (see note) that this is due to serial.print:

" NOTE: Users should be reminded that the time to process code between printout statements can accumulate over time. This is especially noticed when printing out the data at different points within the main program. When code is executed this way it appears that the time is drifting, be assured that it is most likely not drifting, just the time between calls adding up over time. (source: Virtuabotix is under construction)"

I tested my circuit without serial.print statements (using an lcd dislay for output) and there the time was correct! The problem is that I need to monitor my application, but that I cannot use an lcd display.

  1. How can I solve this time drift problem that is caused by serial.println()?
    I already tried to limit serial.print() to just 1 occurence, but that did not help.
  2. Why does this time drift by serial.print() accumulate (I just print the current time of the RTC) ?

Thanks!!

Serial.print has absolutely nothing whatsoever to do with how well a hardware RTC keeps time. Therefore, the problem is in your code...

Regards,
Ray L.

Thanks for your answer, but I think you are wrong.

The problem is definitely not with the code, because when I just replace serial.print() by lcd.print(), there is no drift at all. All the code remains the same, and it is the same circuit. I run that configuration for months now and I have less then 2 seconds drift per MONTH.

When I use serial.print() I have several seconds drift per HOUR.

The problem is not that the RTC drifts. The RTC time is correct. This is proven by the fact when I remove all serial.print() statements and use an lcd.print() with an LCD display to show the time.
It is just that when I want to check the time with serial.print() that the time given by serial.print() seems to drift (and this drift accumulates over time)

PS: This problem is a known problem, as I indicated in the 'NOTE' above. Serial.print() is notorious for its lagging problems.

Any ideas on solving this ?
Thanks!!

Unless you can explain WHY removing the Serial.print solves the problem, you have no way of knowing you are seeing cause and effect. The fact is, there is NO logical connection between the RTC and Serial.print, so there is not a reason in the world to believe what you believe. That is a fact. But, what do I know, I've only been doing this for 35 years....

Since you're absolutely confident your code, which you're not showing us, is correct, I fail to see how anyone here is going to be able to help you.

Good luck, you're going to need it....

Regards,
Ray L.

Hi Guys,

My apologies for being blunt. :slight_smile:
WHY removing the Serial.print solves the problem is exactly what I don't understand, logically there is indeed no connection. I came to that conclusion, because it was hinted by the note at the virtuobotix website (see link in first post), and indeed by changing all serial.print(rtc.getTimeStr()) by lcd.print(rtc.getTimeStr()) I got accurate time.

But I am no expert, and I am glad you guys are willing to help.
I don't have access to the code now, but I will post asap.

Thanks again! :slight_smile:

Users should be reminded that the time to process code between printout statements can accumulate over time. This is especially noticed when printing out the data at different points within the main program. When code is executed this way it appears that the time is drifting, be assured that it is most likely not drifting, just the time between calls adding up over time.

The above is incorrect (in fact it's crap) unless you are outputting more data on serial than it can cope with causes no noticeable delay. Serial outputs its data asynchronusly. Even then the error will not be cumulative!

Any software running on the Arduino has no effect on how well an RTC keeps.

Mark

WHY removing the Serial.print solves the problem is exactly what I don't understand,

You should be looking at why adding Serial.print() causes the problem. Are you perhaps using pins 0 and 1 for something else as well as hardware Serial ? If you posted your code we could see and provide advice.

When I use have serial.print() statements to read out the time of my RTC (ds3231),

How are you using Serial.print( ) to read the time from the RTC ?

https://www.virtuabotix.com/virtuabotix-ds1302-real-time-clock-module-pin-out-coding-guide/#respond

So, you had this problem 5 months ago, and you got the correct answer, and you completely misconstrued the answer you got then, and you are trolling us with the exact same nonsense now ?

Slow serial printing might conceivable interfere with the cumulative millis() count on the arduino, over a long period of time.

There is no way that serial printing can interfere with the RTC time, which has it's own clock and it's own crystal and is designed with the specific aim of timekeeping over fairly long period of time.

Not only does the theory not support it... I have recently run code that includes serial print, and compares RTC time and millis() time to a microsecond accurate GPS pulse per second source. Neither of those showed any effect from the serial print activities.