Go Down

Topic: How to measure a digital pulse ? (Read 874 times) previous topic - next topic

frikosal

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

Code: [Select]
estat=digitalRead(4);
do {} while(digitalRead(4)==estat); // wait until changes

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

Serial.println(t);


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?

Thanks

Cheater

Quote
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.

mellis


frikosal

Quote
Try the pulseIn() function: http://www.arduino.cc/en/Reference/PulseIn


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 ?

Go Up