# Pulsein()

**URL:** <https://forum.arduino.cc/t/pulsein/309777>\
**Category:** Programming\
**Created:** [May 6, 2015, 5:20pm UTC](https://forum.arduino.cc/t/pulsein/309777 "2015-05-06T17:20:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grosleon91](https://avatars.discourse-cdn.com/v4/letter/g/bbce88/32.png) [@grosleon91](https://forum.arduino.cc/u/grosleon91)\
**Post date:** [May 6, 2015, 5:20pm UTC](https://forum.arduino.cc/t/pulsein/309777/1 "2015-05-06T17:20:43Z")

</div>

The code:  
unsigned long pulseIn(uint8\_t pin, uint8\_t state, unsigned long timeout)  
{  
// cache the port and bit of the pin in order to speed up the  
// pulse width measuring loop and achieve finer resolution. calling  
// digitalRead() instead yields much coarser resolution.  
uint8\_t bit = digitalPinToBitMask(pin);  
uint8\_t port = digitalPinToPort(pin);  
uint8\_t stateMask = (state ? bit : 0);  
unsigned long width = 0; // keep initialization out of time critical area

// convert the timeout from microseconds to a number of times through  
// the initial loop; it takes 16 clock cycles per iteration.  
unsigned long numloops = 0;  
unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;

// wait for any previous pulse to end  
while ((\*portInputRegister(port) & bit) == stateMask)  
if (numloops++ == maxloops)  
return 0;

// wait for the pulse to start  
while ((\*portInputRegister(port) & bit) != stateMask)  
if (numloops++ == maxloops)  
return 0;

// wait for the pulse to stop  
while ((\*portInputRegister(port) & bit) == stateMask) {  
if (numloops++ == maxloops)  
return 0;  
width++;  
}

// convert the reading to microseconds. There will be some error introduced by  
// the interrupt handlers.

// Conversion constants are compiler-dependent, different compiler versions  
// have different levels of optimization.  
#if **GNUC** ==4 && **GNUC\_MINOR** ==3 && **GNUC\_PATCHLEVEL** ==2  
// avr-gcc 4.3.2  
return clockCyclesToMicroseconds(width \* 21 + 16);  
#elif **GNUC** ==4 && **GNUC\_MINOR** ==8 && **GNUC\_PATCHLEVEL** ==1  
// avr-gcc 4.8.1  
return clockCyclesToMicroseconds(width \* 24 + 16);  
#elif **GNUC** \<=4 && **GNUC\_MINOR** \<=3  
// avr-gcc \<=4.3.x  
#warning "pulseIn() results may not be accurate"  
return clockCyclesToMicroseconds(width \* 21 + 16);  
#else  
// avr-gcc \>4.3.x  
#warning "pulseIn() results may not be accurate"  
return clockCyclesToMicroseconds(width \* 24 + 16);  
#endif

}

---

<div class="post-metadata">

**Author:** ![evanmars](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/evanmars/32/217678_2.png) [@evanmars](https://forum.arduino.cc/u/evanmars)\
**Post date:** [May 6, 2015, 7:04pm UTC](https://forum.arduino.cc/t/pulsein/309777/2 "2015-05-06T19:04:54Z")

</div>

Here is some information on what is with the registers:

> **[Arduino Reference - Arduino Reference](https://www.arduino.cc/reference/en/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [May 6, 2015, 10:02pm UTC](https://forum.arduino.cc/t/pulsein/309777/3 "2015-05-06T22:02:47Z")

</div>

> Can someone explain me the code behind pulsein function.

Yes, if you can say what part you don't understand.

> May someone tell me how it measure time or signals and then convert it into ms.

It counts the number of times it runs through a loop. It multiplies that by the number of clock cycle each time through the loop takes (21 or 24 depending on the compiler version). It adds the amount of fixed overhead cycles (16) to get total clock cycles during the pulse. It then divides that by the number of clock cycles per microsecond (16 on a 16 MHz Arduino) to get the pulse length in microseconds (not milliseconds).

> What is with registers

It is faster to read a full 8-bit I/O register and use the AND operator (&) to isolate one pin from that register than it is to use digitalRead() to read an individual pin.

> and how the function know that the state of signal is "1" and begin with measurment and when is "0" to stop.

The 'stateMask' is set to the bit mask when you are looking for a HIGH pulse and 0 when you are looking for a LOW pulse. The loops do logical operations on the input register contents and the 'stateMask' to determine if the input pin is IN a pulse or BETWEEN pulses.

The first loop waits for the input to be BETWEEN pulses so we don't measure a pulse that is already in progress.

The second loop waits for the input to change back to being IN a pulse. This marks the start of a pulse.

The third loop counts until the input changes back to BETWEEN pulses. It is this third loop that measures the pulse width.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:16pm UTC](https://forum.arduino.cc/t/pulsein/309777/4 "2021-05-05T21:16:43Z")

</div>

Closed on May 5, 2021, 9:16pm UTC


