adafruit_debouncer

Debounces an arbitrary predicate function (typically created as a lambda) of 0 arguments. Since a very common use is debouncing a digital input pin, the initializer accepts a DigitalInOut object instead of a lambda.

  • Author(s): Dave Astels

Implementation Notes

Software and Dependencies:

class adafruit_debouncer.Button(pin: ROValueIO | Callable[[], bool], short_duration_ms: int = 200, long_duration_ms: int = 500, value_when_pressed: bool = False, **kwargs)

Debouncer for buttons. Reports pressed and released for the button state. Counts multiple short presses, allowing to detect double clicks, triple clicks, etc. Reports long presses separately. A long press can immediately follow multiple clicks, in which case the long click will be reported in the same update as the short clicks.

Parameters:
  • pin (DigitalInOut/function) – the DigitalIO or function to debounce.

  • short_duration_ms (int) – the maximum length of a short press in milliseconds.

  • long_duration_ms (int) – the minimum length of a long press in milliseconds.

  • value_when_pressed (bool) – the value of the predicate when the button is pressed. Defaults to False (for pull up buttons).

property long_press: bool

Return whether a long press has occured at the last update.

property pressed: bool

Return whether the button was pressed or not at the last update.

property released: bool

Return whether the button was release or not at the last update.

property short_count: int

Return the number of short press if a series of short presses has ended at the last update.

update(new_state: int | None = None) None

Update the debouncer state. MUST be called frequently

class adafruit_debouncer.Debouncer(io_or_predicate: ROValueIO | Callable[[], bool], interval: float = 0.01)

Debounce an input pin or an arbitrary predicate

property current_duration: float

Return the number of seconds since the most recent transition.

property fell: bool

Return whether the debounced value went from high to low at the most recent update.

property interval: float

The debounce delay, in seconds

property last_duration: float

Return the number of seconds the state was stable prior to the most recent transition.

property rose: bool

Return whether the debounced value went from low to high at the most recent update.

update(new_state: int | None = None) None

Update the debouncer state. MUST be called frequently

property value: bool

Return the current debounced value.