Arduino Nano Every ATmega4809 oscillator précision

Hi,

I had problems of timings using a DHTNEW library ( for reading a humidity temp. sensor ).

Was working fine with a arduino MEGA and not working on arduino every.

By using this code i saw differences in timing :

110µs for the every, I tried 2 different every and get the same result.
103µs for arduino mega

I made measurement with a oscilloscope PC Hantek 6022BL

Did somebody experience some problems too with Arduino Every ?
Is it possible to calibrate ?

Thanks

On promotionnal of ATMEGA they say internal oscillator is factory calibrated.

uint8_t pin = 8;
uint32_t start = 0;

void setup()
{
  pinMode(pin, OUTPUT);
  
  while (1)
  {
    start = micros();
    digitalWrite(pin, HIGH);
    while (micros() - start < 100);

    start = micros();
    digitalWrite(pin, LOW);
    while (micros() - start < 100);
  }
}

void loop()
{}

micros() is not exact, play with the value 100

Sadly, the micros function has a resolution of 4µs in all the AVR based Arduino boards. It is possible to do better, but it means making fairly extensive modifications to the library functions. The frequency error of the internal oscillator is somewhat difficult to determine from the spec sheet, but seems to be about ±2% at nominal temperatures. You can get a bit better with calibration (step size is 0.75%). Because your Mega board has a crystal oscillator, it will be more accurate.

To generate a pulse exactly (within the 2%) you can program the microcontroller's Timer/Counter directly. Using TCB0 or TCB1 for this in "Single-Shot Mode" you can generate a pulse with width configurable to any multiple <65535 of 0.0625µs.

Karma++ for typing "4µs" instead of "4us". :wink: