# [time-nuts] Clock Correction Algorithm

Miguel Gonçalves mail at miguelgoncalves.com
Thu Nov 24 09:28:38 EST 2011

```Hi!!

On 24/11/2011, at 13:48, Attila Kinali <attila at kinali.ch> wrote:

> On Thu, 24 Nov 2011 12:56:31 +0000
> Miguel Gonçalves <mail at miguelgoncalves.com> wrote:
>
>> Right! That is what I am doing.
>>
>> The problem is the drift between adjustments. Arduino's clock is slow 25 ms every minute and if temperature changes it will surely be different.
>>
>> The proposed algorithm wants to correct that.
>
> Oh.. sorry.. i misunderstood your question.
>
> Yes the algorithm would work, though i'd rather use a pll style sytem.
> Ie, measure the differnce in time between your clock and your refrence.
> Calculate from this a correction value for the clock frequency (should
> be a simple multiplication by a constant factor).
> Low pass filter that correction value.

I will investigate PLL. I don't want something overly complicated because this is a time display. I want less that 10 ms error and this goal is much better than needed for a clock.

> This should also do away with all those special cases in your algorithm,
> as a PLL style algo is self stabilizing (unless you got the loop gain
> too high*). You will get at most a constant difference between your clock
> and the reference. This difference can be minimized by using a higher
> gain in the feedback loop (at the cost of stability).

This is exactly what I wanted. I really need to read about PLL.

> Also as Javier said, 400ppm is a quite high offset for a crystal.
> Or are you using a RC oscillator based RTC? (a lot of on chip
> RTCs are build that way)

I am using the oscillator that comes with the Arduino.

> On the other hand, i once used a Atmel SAM7 RTC (or RTT in Atmel lingo)
> that is based on a RC Oscillator with +/- 30% accuracy. Using a simple
> calibration step (powering up the crystal oscillator every hour and
> using the crystal to measure the RTT frequency, setting this as new
> correction value, then going back to sleep for an hour) got me below
> 100ppm over a day in my tests (office enviroment, temperature variation
> probably below 2°C over a day, definitly less than 5°C, 30ppm crystal)

I have a RTC that I am not using for this project that has 3.5ppm between -40C and 85C but I don't intend to use it because I want to keep this simple.

Thanks a lot Atilla for your help.

Cheers,
Miguel

```