2 arduino millis & micros inaccuracy ?

Hi,

I've just "compared" two Mega 2560 Rev3 boards and measured 189ms "drift/innaccuracy/[?]" difference between the 2 boards in 9 minutes !

Setup is as follow :

  • Same power supply
  • Trigger is set on Pin 20 of each board, in common
  • I randomly trig by putting low level on the two pins at the same time
  • See simple code below

Difference of micros() last line is 189144, so 189ms!
Do I have bad/damaged boards? (as far as I know they are genuine, I ordered them from RS Components, Belgium) or am I completely missing the point here... :slight_smile: ?

As far as I've read on the forum, with +/- 50 ppm, I should not get more than a few µs difference in a few minutes testing!?

Kind regards and thank you in advance for your help!

Here is log from Board #1:

11:43:15.528 [RX] - Booting testDetection...
11:43:18.841 [RX] - Detection @ 3312;3313124
11:43:19.877 [RX] - Detection @ 1033;1033164
11:43:28.806 [RX] - Detection @ 8929;8928532
11:44:06.843 [RX] - Detection @ 38029;38029612
11:44:09.157 [RX] - Detection @ 2313;2312280
11:46:01.712 [RX] - Detection @ 112529;112528740
11:54:59.926 [RX] - Detection @ 538098;538098940

Here is log from Board #2:

11:43:15.861 [RX] - Booting testDetection...
11:43:18.842 [RX] - Detection @ 2978;2979636
11:43:19.874 [RX] - Detection @ 1034;1032792
11:43:28.808 [RX] - Detection @ 8925;8925300
11:44:06.846 [RX] - Detection @ 38016;38015892
11:44:09.156 [RX] - Detection @ 2311;2311448
11:46:01.710 [RX] - Detection @ 112488;112488460
11:54:59.926 [RX] - Detection @ 537911;537909796

Sketch code:

volatile bool triggered = false;
volatile unsigned long lastBarrierCrossedTimeStampMillis = 0;

volatile unsigned long lastTimeStampMillis = 0;
volatile unsigned long lastTimeStampMicros = 0;
volatile unsigned long deltaMillis = 0;
volatile unsigned long deltaMicros = 0;

void setup()
{
  pinMode(20, INPUT_PULLUP); //DETECT
  attachInterrupt(3, detection, FALLING);
  
  Serial.begin(115200);
  Serial.println("Booting testDetection...");
}

void loop()
{
  while (true)
  {
    if (triggered)
    {
      String s = "Detection @ "; s = String(s + deltaMillis + ";"); s = String(s + deltaMicros);
      Serial.println(s);
      
      triggered = false;
    }
  }
}

void detection ()
{
  unsigned long nowMicros = micros();
  unsigned long nowMillis = millis();

  if (!triggered)
  {
    deltaMillis = nowMillis - lastTimeStampMillis;
    deltaMicros = nowMicros - lastTimeStampMicros;   

    triggered = true;

    lastTimeStampMillis = nowMillis;
    lastTimeStampMicros = nowMicros;
  }
}

I thing you should probe the two cristals with an oscilloscope. You will see a little difference in frequency, my guess.
Luca

olimex:
I thing you should probe the two cristals with an oscilloscope. You will see a little difference in frequency, my guess.
Luca

Done!

15.9997 MHz & 16.0002 MHz so it wouldn't come from the crystals...
Maybe this is to do with the micros and millis function that I use inside an interrupt, I'm going to look at this...
thank you anyway!

I've just tried the same principle but without interrupt (everything in main loop) and I the error/difference is so bad, too!
Am I correct when I say that the frequencies are quite the same?

const int TRIGGER_PIN = 20;

volatile bool triggered = false;
volatile unsigned long lastBarrierCrossedTimeStampMillis = 0;

volatile unsigned long lastTimeStampMillis = 0;
volatile unsigned long lastTimeStampMicros = 0;
volatile unsigned long deltaMillis = 0;
volatile unsigned long deltaMicros = 0;

void setup()
{
  pinMode(TRIGGER_PIN, INPUT_PULLUP); //DETECT

  Serial.begin(115200);
  Serial.println("Booting testDetection...");
}

void loop()
{
  while (true)
  {
    if (digitalRead(TRIGGER_PIN)==LOW)
    {
      unsigned long nowMicros = micros();
      unsigned long nowMillis = millis();

      deltaMillis = nowMillis - lastTimeStampMillis;
      deltaMicros = nowMicros - lastTimeStampMicros;

      String s = "Detection @ "; s = String(s + deltaMillis + ";"); s = String(s + deltaMicros);
      Serial.println(s);

      triggered = false;
      
      lastTimeStampMillis = nowMillis;
    lastTimeStampMicros = nowMicros;
    
    delay(1000);
    }
  }
}

I think the problem is in the difference in frequancy. If you do some math, you will find that one board is getting 500 clocks more than tho other evry second, in 9 minutes you got 540 seconds = 270000 more clock cycles! That's quite a lot