Arduino pulseIn(): Simple Guide for Beginners

Arduino pulseIn(): Simple Guide for Beginners

The pulseIn() function is used to read the pulse length of a signal of a given level on a given pin. If the level is set to HIGH, the function will wait for that level to appear on the pin, then start the timer and stop it when the pin is again at logic zero (LOW).

Suppose the expected logic level does not appear on the pin within a certain timeout, which the function’s input parameter can set. In that case, the function will return 0, otherwise the duration of the read pulse in microseconds.

It has been established that errors are possible when using the function to measure wide pulses. The function works with pulses of 10 microseconds to 3 minutes.

It is not recommended to use for reading long pulses, although it is theoretically possible to read a pulse up to 3 minutes long.

Syntax

pulseIn(pin, value)
pulseIn(pin, value, timeout)

Parameters

pin: the number of the input/output port on which the signal will be expected (int).
value: type of pending signal – HIGH or LOW.
timeout (optional): signal waiting time (timeout) in microseconds; one second by default (unsigned long).

Returned Value

Signal length in microseconds or 0 if no signal is received before timeout expires (unsigned long).

Notes

If the input is in a high-impedance state, random values will occur on it, which can be used as a “seed” for the pseudo-random number generator.

Example of Usage

int pin = 7; //output port number
unsigned long duration; //signal timeout

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

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

FAQs

How many arguments does the pulseIn() function accept?

It accepts two arguments: the pin number and whether it’s high or low.

How do you count pulses in Arduino?

If pin 12 goes high, then the Arduino will count that as a pulse. The number of pulses detected is shown on the serial monitor. When pin 12 is held high for one second, the Arduino will count it as one pulse.

What is pulse function?

A pulse in the signal analysis is a rapid, transient change in an amplitude of a signal from its baseline value to a higher value, followed by a return to the baseline value, followed by another rapid transient change in amplitude.

How fast can Arduino count pulses?

At 50kHz, the Arduino only detected approximately 24,000 pulses during a half-second detection cycle.

Related Video: What is a Pulse and How to Use the pulseIn() Function (With Example)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top