PulseIn – Read a series of pulse durations

PulseIn is used to measure a series of active and idle pulses. This is commonly used in infrared receivers and low cost temperature sensors (DHT). The pulsed signal consists of timed active and idle periods. Unlike PWM, there is no set duration for active and idle pairs.

class pulseio.PulseIn(pin, maxlen=2, *, idle_state=False)

Create a PulseIn object associated with the given pin. The object acts as a read-only sequence of pulse lengths with a given max length. When it is active, new pulse lengths are added to the end of the list. When there is no more room (len() == maxlen) the oldest pulse length is removed to make room.

  • pin (Pin) – Pin to read pulses from.
  • maxlen (int) – Maximum number of pulse durations to store at once
  • idle_state (bool) – Idle state of the pin. At start and after resume the first recorded pulse will the opposite state from idle.

Read a short series of pulses:

import pulseio
import board

pulses = pulseio.PulseIn(board.D7)

# Wait for an active pulse
while len(pulses) == 0:
# Pause while we do something with the pulses

# Print the pulses. pulses[0] is an active pulse unless the length
# reached max length and idle pulses are recorded.

# Clear the rest

# Resume with an 80 microsecond active pulse

Deinitialises the PulseIn and releases any hardware resources for reuse.


No-op used by Context Managers.


Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.


Pause pulse capture


Resumes pulse capture after an optional trigger pulse.


Using trigger pulse with a device that drives both high and low signals risks a short. Make sure your device is open drain (only drives low) when using a trigger pulse. You most likely added a “pull-up” resistor to your circuit to do this.

Parameters:trigger_duration (int) – trigger pulse duration in microseconds

Clears all captured pulses


Removes and returns the oldest read pulse.


The maximum length of the PulseIn. When len() is equal to maxlen, it is unclear which pulses are active and which are idle.


True when pulse capture is paused as a result of pause() or an error during capture such as a signal that is too fast.


Returns the current pulse length

This allows you to:

pulses = pulseio.PulseIn(pin)

Returns the value at the given index or values in slice.

This allows you to:

pulses = pulseio.PulseIn(pin)