PaulStoffregen / FreqMeasure

Measures the elapsed time during each cycle of an input frequency.
http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
69 stars 31 forks source link

mega 2560 and UNO shows inaccurate results #19

Open 0i0i0i opened 3 years ago

0i0i0i commented 3 years ago

Description

When use arduino 2560 and UNO to test how accurate the measument of the frequency could be, I found the different borads have different results. I used a standard 1kHz squarewave source and measured its frequency with an osilloscope, in which it shows exact 1000Hz.

Howerever, when I loaded the example code to all borads I have, I got 1001.86, 1003.45 and so on. One arduino mega 2560 could have different result to another. From your demo picture, I noticed the result is pretty accurate. My guess is that some variation of the cristals is resposible for this. If that is the case, I would multiple the result with a factor. However, there might be some systematic issue envolved, like some latency of reading. If the later is true, I would add some constants to some part of the period measurement. Could we add some parameters for conpensate?

Steps To Reproduce Problem

load the following code and the result will show it.

Hardware & Software

Board: arduino mega 2560 rev.3 Shields / modules used non Arduino IDE version 1.8.5 Teensyduino version (if using Teensy) Version info & package name (from Tools > Boards > Board Manager) Operating system & version windows7-64bit Any other software or hardware?

Arduino Sketch

// Change the code below by your sketch (please try to give the smallest code which demonstrates the problem)

#include <FreqMeasure.h>

void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
  if (FreqMeasure.available()) {
    // average several reading together
    sum = sum + FreqMeasure.read();
    count = count + 1;
    if (count > 30) {
      float frequency = FreqMeasure.countToFrequency(sum / count);
      Serial.println(frequency);
      sum = 0;
      count = 0;
    }
  }
}

Errors or Incorrect Output

Inaccurate results.

PaulStoffregen commented 3 years ago

It's probably hardware.

According to the schematics on Arduino's website, Arduino Uno and Mega uses a ceramic resonator, part number CSTCE16M0V53-R0. The datasheet says the "5" digit means 0.5% accuracy. You should expect up to 0.5% error on these boards.

Of course cheap clones might use anything!

On Teensy we use only high quality quartz crystals which are rated for 0.003% accuracy (30 ppm).

0i0i0i commented 3 years ago

Thanks Paul. I think I can try to replace these 0.5% ones with higher quality ones.