pulseIn

Hello. I have a temperature sensor that outputs a wave with either high or low. The temperature is a function of the period of the low and high section of the wave.

I am looking to use the PulseIn() method, but am curious about one thing. When I call Pulsein with argument say HIGH, and the wave is currently HIGH, then will PulseIn start measuring immediately or wait until it goes low and then changes to HIGH?

Thanks fillalph

Hi Fillalph,

PulseIn() will wait for a LOW state, then count while in HIGH state.

Lionel.

Start measuring immediately. But maybe we should change that (i.e. only start counting from a LOW to HIGH transition on the pin). What do you think?

Actually you can see it for yourself, simply open lib/targets/arduino/wiring.c in your arduino folder at line 489 (Note that it's commented out and the actual implementation is right below).

Hope this helps, Lionel.

Lionel Lemarie: I don't have that much experience in C coding and that was a bit over my head (line 489) mellis: I think if I call PulseIn() with argument HIGH (so I want to measure the period of high) and the signal is currently high, then it shouldn't start measuring yet. Rather, it should start counting when the signal goes from LOW to HIGH and measure the length of that HIGH.

fillalph

@Lionel Lemarie: Woah. I just went to see if I could understand line 489 today, it looked different. I must have been looking at a different file last time :D.

EDIT:

unsigned long pulseIn(int pin, int state)
{
      unsigned long width = 0;

      while (digitalRead(pin) == !state)
            ;
            
      while (digitalRead(pin) != !state)
            width++;
            
      return width * 17 / 2; // convert to microseconds
}

I think I agree with mellis on how the function works, but I would just like to be sure. If I call Pulsin(High) and my function is currently high, but halfway through the signal, then it would jump into and execute the 2nd while loop correct?... and take a measurement but return an incorrect period measurement?

Can someone help me out. Thanks fillalph

Thanks