2,400 ppm drift between two clocks??? (What am I measuring?)

Hey there,

I recently compared two Arduino Nanos with a very simple code, a timer interrupt every 1000 ms (using Timer1). The Nanos then should send one char via serial to Atmel Studio, where I added timestamps to the two incoming messages. I compared these two sets of timestamps and they drift apart from each other 2.4 ms per second! How can this be? What's my mistake (if I was to blame)?

Thanks everyone!

hankyban:
What's my mistake (if I was to blame)?

Failed to post your code.

Please use
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags.

Hi, well, my code can't be more basic...

#include <TimerOne.h>

void setup() {
  Serial.begin(9600);
  
  Timer1.initialize(1000000);
  Timer1.attachInterrupt(callback);
}

void callback(){
Serial.write("T");
}

void loop() {
}

Out of interest, what do you see if you print micros() instead of "T" ?

@UKHeliBob, it produces the same results. Of course, the micros are displayed correctly (every 1000 ms), but that's not the point. The Atmel Studio timestamp says simply that the messages do not arrive every 1000 ms (that would be ok when the error is equally destributed, but it's not.).

Please take a look at my attachment. (I know, it's done quick and dirty...) The table shows the timestamps for the incoming message from nano 1 and 2 in [ms]. The third column shows the difference between two timestamps. This difference may vary of course, but it should not "drift away" that fast... The diagram shows the drift over incoming messages since the trendline is not horizontal. I added a trend line with the corresponding equation. Here it's 2.6 ms / s drift.

I must admit, I'm using "china" Nanos, but still... ~2400 ppm and more is a LOT.

drift_2.png

Is drift the correct description for what you are seeing ? If there is a consistent difference between consecutive figures for each Nano then there is no drift. How accurate do you expect each Nano to be ?

How are the Nanos connected to Atmel Studio ?

I'm not familiar with the TimerOne library, but perhaps test the timing without a Hardware Serial event (scope toggling an output pin) and eliminate the effects of managing the interrupts on Serial.

hankyban:
Hey there,

I recently compared two Arduino Nanos with a very simple code, a timer interrupt every 1000 ms (using Timer1). The Nanos then should send one char via serial to Atmel Studio, where I added timestamps to the two incoming messages. I compared these two sets of timestamps and they drift apart from each other 2.4 ms per second! How can this be? What's my mistake (if I was to blame)?

Thanks everyone!

These Nanos have ceramic resonators? That would explain it.

UKHeliBob:
If there is a consistent difference between consecutive figures for each Nano then there is no drift.

Well, that's the point. It's not consistent.

UKHeliBob:
How accurate do you expect each Nano to be ?

As accurate as the crystal oscillator, and it's usually in the area around 20 - 50 ppm.

UKHeliBob:
How are the Nanos connected to Atmel Studio ?

USB / serial connection, 9600 Baud

BulldogLowell:
I'm not familiar with the TimerOne library, but perhaps test the timing without a Hardware Serial event (scope toggling an output pin) and eliminate the effects of managing the interrupts on Serial.

Thanks, but a few days ago, I did it without using an interrupt, simply checking if (millis()%1000) and then sending a short message which lead to the same results. The thing is: I use the exact same source code on exact same kinds of nanos and get different results, every time. Someone ever tried to write the OSCCAL register for "tuning" the crystal freq?

hankyban:
Hi, well, my code can't be more basic...

It can. But we should start with what you have.

...
  Timer1.attachInterrupt(callback);
...
Serial.write("T");
...

I wonder if calling any Serial methods in an interrupt service routine could interfere with the correct operation of Serial or Timer1.

Before concluding there is something terribly wrong with the two clocks you need to confirm your code works as expected. (Or use code known to work correctly.)

2.4 ms per second is 0.24%. A difference of 0.24% between units is within spec for (most - including the ones used on official boards, and presumably the cheap clones) ceramic resonators.

DrAzzy:
2.4 ms per second is 0.24%. A difference of 0.24% between units is within spec for (most - including the ones used on official boards, and presumably the cheap clones) ceramic resonators.

Not only that, but their resonant frequency is horribly, horribly temperature sensitive.

Ok, thanks for your answers... I thought the Nano was using a crystal osc... That would explain everything. Concerning the code: even if the code was not super efficient, there shouldn't be such a difference (always the same difference) on two identical systems (except for the resonator's behaviour) with the identical code.

I must admit, putting the serial function inside the callback is not the brightest idea but should not be the reason for the constant difference.

Thanks guys! You helped me.

hankyban:
Hey there,

I recently compared two Arduino Nanos with a very simple code, a timer interrupt every 1000 ms (using Timer1). The Nanos then should send one char via serial to Atmel Studio, where I added timestamps to the two incoming messages. I compared these two sets of timestamps and they drift apart from each other 2.4 ms per second! How can this be? What's my mistake (if I was to blame)?

Thanks everyone!

Ceramic resonators are a bad source of clocking frequency.

Crystal oscillators are much more precise.

While the drift with crystal oscillators is only about 0.01%, it might be 0.4% or up to 0.8% time drift with ceramic resonators.

Unfortunately, nowadays most Arduino board designs just use rather inaccurate ceramic resonators for microcontroller clocking and crystal oscillators are only used for the USB clocking.

Look up the board schematics!

If it uses a crystal ocillator for USB clocking ony, but ceramic resonator for controller clocking, then you might get up to 0.8% time drift (worst case). That's 8 milliseconds within a single second!

Ceramic resonators have 50 to 100 times more time drift than crystal oscillators provide.

jurs, thank you as well. It's indeed a ceramic osc. So, no wonder I get about 2400 - 2600 ppm.