Arduino Programming Language: PulseIn

Name

pulseIn()

Description

This function reads a pulse on a pin. It can read a pulse either HIGH or LOW on a pin. 
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. if no pulse starts within a specified time out it stops and returns 0.

Syntax

pulseIn(PIN, value);
pulseIn(PIN, value, timeout);

Parameters

  • pin: the number of the pin on which you want to read the pulse.
  • value (integer): type of pulse to read: either HIGH or LOW.
  • timeout (unsigned long, optional): number of microseconds to wait for the pulse to start. Default value is one second

Returns

unsigned long 

Usage

Application

Examples

int pulsepin = 8;
int value = 0;

void setup() {
pinMode(pulsepin, INPUT);
Serial.begin(9600);
}

void loop() {
value = pulseIn(pulsepin, HIGH);
Serial.println(value);
delay(100);
}

Related Functions


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc