Pulsin Accuracy on UNO 3.0

Hi, I am relatively new to Arduino but not to microcontrollers & electronics. I am a retired EE.

With an Arduino UNO (Ver 3.0) and using PULSIN the pulsin readings are off by -6.8 percent across the board I measured from 20 us to 1 second and all are off by the same percentage. I was expecting better accuracy given the microcontroller's crystal clock source. I can certainly compensate for this in code as shown below but it makes the code less efficient. It seems to me that Pulsin should be more accurate than this.

It would be nice to know the reason for such a huge error ?? Anyone know? Certainly the system clock cannot be off by 6.8 percent ?

For reference, below is my sample code that corrects the error to < 1%. (Might help someone else.)

/*
Pulsin Test
*/

int pin = 7;
unsigned long duration;
unsigned long corrected_val;
unsigned long timeout = 2000000;

void setup() {
Serial.begin(9600);
pinMode(pin, INPUT);
}

void loop() {

duration = pulseIn(pin, HIGH,timeout);
corrected_val = duration + (duration / 147 + 1);
Serial.println(corrected_val);
delay(500);
}

1/147th is 0.68%, not 6.8%.

For values up to 146 there will be no correction except adding 1. I don't see how that helps with measurements down to 20 us.

The UNO uses a ceramic resonator instead of a crystal. Still, I would expect it to be better than 0.68% accurate.

The resolution of pulseIn() is 21 clock cycles or about 1.3125 microseconds.

Thanks John,

You are correct. It is .68% and it is indeed a .5% ceramic resonator. I saw the crystal for the USB chip and wrongly assumed it was for the micro. My first day with the Arduino. Note to self: Check accuracy on each UNO board.

As far as adding the 1 ... It was not intended to help above 50us. Simply stated, it corrects the returned pulsin error so that a measured 20.08us pulse becomes 20 instead of the 19 that is returned by pulsin. The added 1 microsecond is insignificant for longer pulses.

William