ElliotGarbus wrote: ↑Tue Sep 20, 2022 1:32 am
How often should I be refreshing the RTC from the network to keep time? Originally I was going to update the RTC once a week, and I saw over the course of a day, I was losing minutes. I have moved to updating the RTC every 6 hours. Is this too often or not often enough?
Hmm.. minutes per day is pretty high. Are you using an external RTC or the thing called an "RTC" in the microcontroller?
'Real Time Clock' is a badly abused term. Marketing departments use it to mean anything that provides output in an days:hours:minutes:seconds format, whether the output has any relation to the actual time or not. The ones described in microcontroller datasheets are usually driven by the chip's CPU clock, which typically has about a 3% tolerance on absolute accuracy and a 5% tolerance on drift.
Standalone devices that call themselves RTCs do better, but there are still grades of performance. The DS1307 and PCF8523 keep reasonably good time for intervals of a few hours, but have an expected drift of about 30ppm (+/-3 seconds per day). They use a quartz crystal oscillator, but don't have any hardware to limit or compensate for the crystal's drift relative to temperature:
https://www.adafruit.com/product/3296
https://www.adafruit.com/?q=PCF8523&sort=BestMatch
The next step up are devices like the DS3231. It has a quartz crystal built into the package and calibrated at the factory, plus what's known as a 'thermal compensation table'.. bascially the manufacturer has measured a large set of crystals at different temperatures, and worked out how much drift to expect at any temperature in the chip's operating range. That information is written into the chip as a lookup table, and the chip has a built-in thermometer to measure the crystal's temperature at any moment.
The oscillator still has the same 30ppm drift as a DS1307 or PCF8523, but the compensation table is good enough to keep the reported time stable to within a few seconds per year:
https://www.adafruit.com/?q=DS3231&sort=BestMatch
The next step up from there are thermally controlled quartz oscillators (TCXOs), whose accuracy and stability approach the low end of the atomic clock system (1 part in 1e19.. about 1 nanosecond every 300 years).
We don't carry TCXOs, and they wouldn't be appropriate to your project anyway.. they're power-hungry and require careful handling.
Somewhere in the middle are GPS modules. Their internal clocks are about as good as a DS1307 or PCF8523, but when they have a fix, they adjust the clock at the beginning of each GPS-second. The GPS satellites are synced to the international atomic clock system, and if you let GPS modules hold a fix for a few hours, they'll synchronize their clocks to within 10ns of the GPS clock system. In effect, they have no drift over time as long as they continue to have a fix:
https://www.adafruit.com/?q=GPS&sort=BestMatch
If you're using the ESP32's we-call-it-an-RTC, try stepping up to a PCF8523. That will keep your drift within about 3 seconds if you sync the clock once a day, or within about 30 seconds worst-case if you sync once a week.