Arduino Time Clock Accuracy
The limitations of accuracy of the library depend on accuracy of the crystal. When they make, or cook, the crystal they can only make it to a certain degree accurate, also the environment of the crystal (temperature, humidity, etc.) play a role in the accuracy of it. Let's say you have a crystal that is off by .5 second every hour, great for short term, but if you expand that over a year it is over 1 hour off by that time. If you want something to keep an accurate time over a long period I suggest a real time clock (they still have inaccuracies), a GPS module, or an internet connect to sync with.
For further information look at the wikipedia article on quartz crystals
The use of a 84MHz crystal versus a 16MHz crystal will not necessarily improve the accuracy of the Arduino clock since the frequency of the crystal is more an indicator of processor speed than accuracy. The accuracy of the Arduino clock is primarily dependent on the accuracy of the crystal oscillator.
EDIT: I am no expert on crystal oscillators so if you see anything wrong here please let me know
Re-visiting an old question... as I found a very informative blog post that sheds new light into it. But let me first provide some context before giving the link.
When assessing the quality of a time base, be it a crystal, a ceramic resonator or a lab-grade frequency standard, there are two notions that should be distinguished:
- accuracy: how close is the frequency of the time base to its nominal value
- stability: how much does that frequency drift over time
Accuracy is important if you want your clock to give correct time “out of the box”. However, if you are willing to spend some time calibrating your clock, then you do not really care because you are going to calibrate out any inaccuracy you measure. jfpoilpret's answer provides an example of a “manual” calibration protocol, which is by necessity quite lengthy. If you can borrow a GPS module with a 1PPS output, the calibration could be done in a few seconds.
Stability is a more serious issue. If the frequency of the time base drifts randomly, this will defeat your calibration efforts. Essentially, the calibration will tell you how fast or slow your clock is running right now, but it will not allow you to predict how fast or slow it will run in the future.
Here is the promised link: Arduino clock frequency accuracy, by Joris van Rantwijk.
What Joris did is measure the accuracy and stability of an Arduino Pro Mini (clocked off a ceramic resonator) and an old Duemilianove (quartz crystal). From my perspective, the main takeaways are:
- both clocks are grossly inaccurate, thus both would need user calibration in order to be used as timepieces
- the quartz crystal of the Duemilianove has decent stability, better than 1.5e-8 at 6 h averaging time
- the stability of the Pro Mini’s ceramic resonator is pathetic, more than two orders of magnitude worse than the crystal, which makes it essentially useless as a time piece
Here is his Allan deviation plot, which measures clock instability as a function of observation time:
(source: jorisvr.nl)
Although this study has some limitations (only two boards were tested, and the observation time is too short), it is well thought and very informative. I encourage you to read it in whole.
The best way to know the accuracy of the resonator of your board is to measure it yourself.
To do so, you can use the Arduino millis()
function of your board and write a small sketch that will:
- enable you to set the beginning time for measuring time drift (eg with a simple push button); you will trigger the button based on an accurate time base.
- then repeatedly call millis() until at least 120h ("arduino hours", that would be around 5 days) have elapsed
- display a signal when those 120h have elapsed (your sketch should probably "warn" you before the exact time has been reached so you get ready for measure)
- when the 120h have elapsed, check your reference time based (used in step 1.) and check how much time have elapsed (should be 120h +/- epsilon)
- once you know the drift of your clock, and provided your board will run in the same environental conditions (temperature mainly) of your measure, you can use it in your sketches to adjust the
millis()
value every hour or so.
Of course, this approach is far from perfect as it requires human intervention and thus will create additional time drifts during measurements, that's why you need to measure your clock time drifts over a long period.
An improved approach would be to connect a high accuracy RTC clock (the accuracy must be chosen based on the accuracy you need for your application) to your board and adapt the sketch so that it automatically calculates the drift. Once you got the time drift you can do the same as step 5 above in your sketches, and disconnect the RTC clock from your board.
Important points:
- measure the time drift on the board that will need clock adjustment later on (if you have several boards, you must measure one drift per board)
- ensure the stability of the environment in which your board will be used
Finally, if you really need high accuracy, then definitely connect an external clock source (e.g. RTC clock, GPS, NTP) to your board and use it as a SyncProvider for the PJRC library.