Reference   Language (extended) | Libraries | Comparison

unsigned long pulseIn(pin, value)

Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length. Note that this function will not return until a pulse is detected.

Parameters

pin: the number of the pin on which you want to read the pulse. (int)

value: type type of pulse to read: either HIGH or LOW. (int)

Returns

the length of the pulse (in microseconds)

Example

 

int pin = 7;
unsigned long duration;

void setup()
{
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
}

See also

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.