Reading Pulse from Falling Edge to Falling Edge

Hello,

I’m trying to accurately measure the time between falling edge to falling edge on about 50 pulses, that range from 168 us to as small as 35 us. Below is what I’ve come up with so far. It’s simple and seems to work but isn’t as accurate as I would like. I wanted to know if anyone might have any better suggestions that will do the job better and/or more accurately. I’ve tried to simply use the pulseIn function to measure the on and off time and sum them together, but had issues with it measuring incorrectly when compared to what I’m seeing on the scope.

Any help would be greatly appreciated!

  for(i=0; i<samples; i++)
  {
    while(digitalRead(pin) == LOW) {}           //Wait for input to go high again
    while(digitalRead(pin) == HIGH) {}          //Wait for input to go low again
    
    time[i] = micros();                          //Record time to array
  }
  
  for(i=0; i<samples; i++)
  {
    nibble[i]  = time[i+1]-time[i];
  }

check attachinterrupt() and configure it for a falling edge. put the time measurement in the ISR()

be aware that the micros() has a precision of 4 uSec.

If you want to be more precise you should use a hardware timer,

check - http://www.gammon.com.au/forum/?id=11504 - for some good tech info

The attachInterrupt approach will also reduce the jitter from timer0 (which is used to keep time for millis() and delay()...)

robtillaart: check attachinterrupt() and configure it for a falling edge. put the time measurement in the ISR()

be aware that the micros() has a precision of 4 uSec.

If you want to be more precise you should use a hardware timer,

check - http://www.gammon.com.au/forum/?id=11504 - for some good tech info

Using the hardware timer worked great, thanks for the help!