Go Down

### Topic: How to measure a digital pulse ? (Read 1 time)previous topic - next topic

#### frikosal

##### Oct 07, 2007, 11:12 am
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 changest1=millis(); // measure the changedo {} 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

#1
##### Oct 07, 2007, 12:55 pm
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

#2
##### Oct 07, 2007, 04:37 pm
Try the pulseIn() function: http://www.arduino.cc/en/Reference/PulseIn

#### frikosal

#3
##### Oct 07, 2007, 06:50 pm
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