# Reading the width of the pulse with Resolution of 1 Microsecond

I want to measure the pulse width with resolution of 1 microsecond. will PulseIn Do that? or Does this Code.

``````ISR(TIMER1_CAPT_vect){
if(bit_is_set(TCCR1B,ICES1)){
// if rising Edge is Detected
TCNT1 = 0; // make the timer count 0
TCCR1B ^= _BV(ICES1);
}
else {
captureValue = ICR1;
TCCR1B ^= _BV(ICES1);
}
}
``````

Which is the Accurate One

Here's how the 1-pin cap sense does close, sub usec timing.

http://playground.arduino.cc/Code/CapacitiveSensor

the critical part, the whole is at the link,

``````  // Prevent the timer IRQ from disturbing our measurement
noInterrupts();
// Make the pin an input with the internal pull-up on

// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (*pin & bitmask) { cycles =  0;}
else if (*pin & bitmask) { cycles =  1;}
else if (*pin & bitmask) { cycles =  2;}
else if (*pin & bitmask) { cycles =  3;}
else if (*pin & bitmask) { cycles =  4;}
else if (*pin & bitmask) { cycles =  5;}
else if (*pin & bitmask) { cycles =  6;}
else if (*pin & bitmask) { cycles =  7;}
else if (*pin & bitmask) { cycles =  8;}
else if (*pin & bitmask) { cycles =  9;}
else if (*pin & bitmask) { cycles = 10;}
else if (*pin & bitmask) { cycles = 11;}
else if (*pin & bitmask) { cycles = 12;}
else if (*pin & bitmask) { cycles = 13;}
else if (*pin & bitmask) { cycles = 14;}
else if (*pin & bitmask) { cycles = 15;}
else if (*pin & bitmask) { cycles = 16;}

// End of timing-critical section
interrupts();
``````

Note that micros() returns a value with 4 usec granularity. Interrupts take time to process.

From the looks of it pulseIn will do just that, looks to be quite time consuming tho. I tried to read the pulse width from a RC receiver, to determine what to do in my code based on the length of the pulse. In the end my solution was to use an interrupt to detect a rising edge and another to detect the falling edge. I would then save micros() to a variable on each interrupt then deduct one from the other to give pulse width. This was then checked at the start of each run of my loop.