How to measure a digital pulse ?

Hi, I want to measure the time one of the digital input pins is high (or low). I'm doing the following:

do {} while(digitalRead(4)==estat); // wait until changes

t1=millis(); // measure the change
do {} while(digitalRead(4)==!estat); 


My questions are: 1-How accurate you think that is? The time for calling millis() and the digital read eat time, so.. 2-How can I do it for intervals below 1ms?


2-How can I do it for intervals below 1ms?

You need to do something like counter++; every loop. That creates your own arbitrary unit which is the amount of time one loop takes to complete.

Try the pulseIn() function:

Try the pulseIn() function:

Thanks!! :)

What if the pulse already began when you call the function? ie, the pin is already high and you want to know how long does it take to go low. It seems this is not supported, where can I find the library code to change it ?