Arduino clock accuracy

what is the accuracy of the clock that is used in Ardunio (i.e millis() function etc).

Since i want to build a simple clock that needs to be able to hold time for around 4hrs (after that it doesn't matter).

If it can't keep time for 4hrs, what is the easiest way to achieve this (RTC ic, simple oscillator, counter ...)?

RTC could be a real good way to go. The benefit is that you could use one that keeps time even if the Ardiuno is unpowered.

Most arduino builds use the Resonator versus a crystal as a clock source. Changing to a crystal clock source would possibly increase accuracy but the timing is dependent on what you program does... such as... if are you using interrupts.

To be honest though, even the basic resonator is probably pretty accurate for only 4 hours.

The DS1307 RTC is cheap and easy to use. There is a nice library for it too. It's not going to be atomic clock accurate but good for general purposes.

4 hours = 240 minutes = 14,400 seconds = 14,400,000 milliseconds

The millis() function is probably accurate to +/- 1 millisecond and takes an unsigned long which is a number from 0 to 4,294,967,295 which is much bigger than 14,400,000.

So you should be able to use the millis() function to time a delay of 4 hours, keeping in mind that the number returned by millis() will overflow back to 0 at some point, but you can handle this in your code.

The accuracy of the millis() function is governed by the clock in your Arduino board which is driven by either a crystal or ceramic resonator. A crystal has an accuracy of roughly +/- 50 parts per million (ppm) or so (some are more accurate some are less, but 50ppm is about right for a cheap crystal).

50 ppm = 0.005% accuracy which in your case equals about 0.72 seconds deviation from the nominal delay over 4 hours at 16MHz

A ceramic resonator is about 0.5% accurate = 72 seconds over 4 hours or about 1 minute 12 seconds at 16MHz.

Both these time errors are very much greater than the accuracy of the millis() function so it’s accuracy can effectively be ignored.

These are the worse case calculations and the actual timing error will depend on the device, temperature changes and the device’s ageing characteristics. If you are using a crystal oscillator then unless you need more than 1 seconds accuracy over 4 hours, the millis() function should suffice, although as pwillard says, other interrupts, functions etc could also effect the timing accuracy.

The same kind of clock accuracy calculations apply to a DS1307 RTC, the fundamental accuracy of the crystal determines the accuracy of the time it keeps, there’s nothing special about the DS1307 in terms of accuracy and it could in fact be worse than the Arduino’s internal clock. It all comes down to the simple question of how accurate you need the timing to be for your application and then select an appropriate oscillator.

PS: I think I’ve got the maths right - it’s late here!

thanks guys,

that gives me a good steer.

James