What to do if your RTC is not accurate enough?
If your RTC drifts too much over time, here are some solutions you need to consider - but keep in mind that not all variants
Did you select the correct clock source?
Many RTCs and controllers have multiple clock sources - typically, one internal RC oscillator and one external crystal oscillator. The RC oscillator is typically much more inaccurate, hence you should always select. You should absolutely double-check if you have set the clock source correctly - for example by checking with an oscilloscope if the crystal is oscillating. Note that this is not 100% reliable since your oscilloscope probe is loading the crystal with its additional capacitance and might change the frequency significantly - and in some cases the extra capacitance will cause the crystal to stop oscillating. In practice, it works most of the time though, so it’s always worth a try - just don’t take the measured frequency too seriously.
Did you select the correct load capacitance?
In our post on How to compute crystal load capacitors using Python we showed an easy method of how to compute the correct load capacitor value. If in doubt, redo the calculation - often, people make the mistake of looking for the load capacitance in the datasheet and assuming that this is the value of the capacitors to attach to the crystal.
Additionally, since the load capacitance calculation involves some estimation of the board capacitance, you should tune the correct capacitance once you have a prototype board, i.e. selecting a slighly different load capacitor value to get the desired accuracy. See our post on How to tune your crystal oscillator to get the best possible frequency accuracy for further instructions.
Can you use a better crystal?
Often you can just spend a few cents more to get a crystal that has an higher accuracy and/or a lower temperature coefficient. Note that having a more accurate crystal is typica
Can you digitally tune the RTC?
Many modern RTCs feature a way of digitally tuning the exact frequency using register settings. Our post on How to tune your crystal oscillator to get the best possible frequency accuracy has a sections containing more information on how you can approach this. Note that digital tuning can not by itself compensate for temperature changes leading to your crystal frequency changing.
Can you automatically compensate for RTC drift using your microcontroller?
If your product is powered on at least a couple of times a year (i.e. it is not powered by a CR2032 or similar battery), you can use that timespan to recalibrate your RTC using digital tuning, or to manually tune the RTC by computing the number of hours since the last power on event and subtracting e.g. a few seconds for each hour to adjust for the RTC drift. This approach is most accurate if you have any way of comparing the current RTC frequency to another clock source (see below). Note that automatic compensation can only adjust for temperature changes leading to your crystal frequency changing if the MCU is running, the second clock source is running and if the second clock source
Can you synchronize to a second clock source?
Using your microcontroller, you can build a frequency counter in firmware that will compute that actual frequency of your RTC crystal. In order for this to work, you need to have a second clock source with a higher accuracy.
These are the most common alternate clock sources you can use for your board:
- The high speed oscillator clocking your microcontroller. If power usage is not a major concern, I recommend using an oscillator, not a crystal, because you don’t have to tune load capacitors when using an oscillator, leading to higher effective accuracy in many real-world usecases - especially, if you don’t want to tune at all.
- The power grid has a frequency of 50 Hz or 60 Hz depending on where you are in the world. It has an excellent long-term stability, but its short term stability is not that great. The people running the power grid are continously compensating the short term instabilities so that the. Note that if you want to use the power grid as a clock source, you have to take care of proper isolation and spacing since grid power can be extremely dangerous.
- GPS is rather complex to implement but provides an extremelyaccurate clock source if you have GPS reception. Note that it typically won’t work indoors and it’s a very expensive yet very accurate way of clocking your device. Since it’s also rather power-intensive, it is often not a good option for battery powered products.
- In Germany, the DCF77 RF transmitter sends a time signal every minute. I recommend to buy ready to use DCF77 modules like this one if at all possible
- Some microcontrollers (for example, some STM32F0) can use a computer’s USB as clock source once they have established a USB connection. This does not work when using an external Serial-to-USB converter
If you can use neither of those clock sources, you could do some significant development to recover clock sources from sources like:
- Ethernet
- USB
- CAN
- UART
- Some RF signal like Wifi, NFC
but as far as I know there are no ready-to-use modules except those listed above. Note that there are many circumstances under which those clock source just won’t work reliably, and implementing it will be rather difficult, so I don’t recommend to go down this path of tears.
Can you synchronize to the internet?
If your device has internet connectivity or connectivity to a network like Sigfox or LoRa, consider if you can just synchronize the time with the internet or just, for example, send a message to your device every day at 00:00
so that it can compensate for the drift. You could also continously adjust the RTC drift by digital calibration or automatic compensation in firmware.