8

I'm currently trying to create an Arduino time clock by using the PJRC Time library (http://www.pjrc.com/teensy/td_libs_Time.html). I know that since most of the Arduino boards are running with a 16MHz clock and a single resonator, the time can become "out-of-sync" after a certain period of time.

However, I was wondering if anyone has an idea about the accuracy of the Time library when used on the Arduino DUE with a 84MHz clock. I've been testing it out and so far, the clock has been kept in sync for a few hours. Thanks!

  • The limitations of accuracy of the library depend on accuracy of the crystal. 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. – Jesse Laning Feb 16 '14 at 0:12
  • @jamolnng Thanks for your answer. Do you know the accuracy of the crystal on the Arduino DUE or where I could go about finding it? – KK6FSL Feb 16 '14 at 0:16
  • The best accuracy you are going to get is 84MHz, 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. – Jesse Laning Feb 16 '14 at 0:25
  • @jamolnng So can I expect a higher accuracy with the 84MHz crystal rather than the 16MHz crystal on most other Arduinos? – KK6FSL Feb 16 '14 at 0:32
  • 5
    "The best accuracy you are going to get is 84MHz" - that doesn't make much sense to me! – Cybergibbons Feb 16 '14 at 6:38
8

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

4

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:

Allan deviation of Arduino Clock Frequency
(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.

  • 1
    Quartz crystals’ frequency changes slightly with temperature. If you can control the temperature it helps keep the frequency stable. – Duncan C Mar 13 at 0:36
3

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:

  1. 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.
  2. then repeatedly call millis() until at least 120h ("arduino hours", that would be around 5 days) have elapsed
  3. 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)
  4. 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)
  5. 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.

2

Your average system clock crystal will be off by several tens ppm (parts per million. They are great for stable and accurate timing of signals, but dramatic for keeping accurate time. Without special provisions the system crystal may be off by several seconds per day.

The solution is to use a proper Real Time Clock, driven by what is commonly known as a 32768Hz watch crystal. These crystals are easily a factor 10 better in accuracy. You can either set up your own oscillator that interrupts the main processor and keep count in your Arduino sketch or you find a RTC breakout board.

Two random examples that pop up in Google with search terms "RTC breakout":

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.