import button
The button module wraps a normal GPIO input with active-low/active-high wiring, internal pull resistor setup, debounce handling, and common human-interface events. Use it when a GPIO represents a push button, maintained switch, limit switch, or panel input.
Quick example
import button
btn = button.begin(pin=4, pull="up", active="low")
btn.on_click(lambda: print("Clicked"))
btn.on_long_press(lambda: print("Reset"), duration_ms=3000)
if btn.pressed():
print("Button is down")
importbutton
High-level button, switch, and digital input handling.
The button module wraps a normal GPIO input with active-low/active-high wiring, internal pull resistor setup, debounce handling, and common human-interface events. Use it when a GPIO represents a push button, maintained switch, limit switch, or panel input.
button.begin(pin: int, pull: str = None, active: str = None, debounce_ms: int = None) -> button_input
button.begin(pin: int, pull: str = None, active: str = None, debounce_ms: int = None) -> button_inputCreate a debounced button input.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | GPIO pin connected to the button or switch signal. |
pull |
str |
positional or keyword | No | Optional internal pull resistor. Use "none", "up", or "down". Defaults to "none". For a button wired from GPIO to GND, use pull="up". |
active |
str |
positional or keyword | No | Optional electrical state that means pressed. Use "low" or "high". Defaults to "low". |
debounce_ms |
int |
positional or keyword | No | Optional debounce interval in milliseconds. Defaults to 30. |
button_input button_input object.
button.begin().pressed() -> bool
button.begin().pressed() -> boolReturn True when the debounced button state is pressed.
button.begin().released() -> bool
button.begin().released() -> boolReturn True when the debounced button state is released.
button.begin().on_press(callback) -> bool
button.begin().on_press(callback) -> boolRegister a no-argument callback that runs once when the button becomes pressed. Pass None to clear.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
button.begin().on_release(callback) -> bool
button.begin().on_release(callback) -> boolRegister a no-argument callback that runs once when the button becomes released. Pass None to clear.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
button.begin().on_click(callback) -> bool
button.begin().on_click(callback) -> boolRegister a no-argument callback for a short press and release. Pass None to clear.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
button.begin().on_double_click(callback) -> bool
button.begin().on_double_click(callback) -> boolRegister a no-argument callback for two quick clicks. Pass None to clear.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
button.begin().on_long_press(callback, duration_ms=1000) -> bool
button.begin().on_long_press(callback, duration_ms=1000) -> boolRegister a no-argument callback that runs once after the button is held for duration_ms.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
duration_ms |
int |
positional or keyword | No | Optional value. Defaults to 1000. |
button.begin().on_hold(callback, delay_ms=500, interval_ms=100) -> bool
button.begin().on_hold(callback, delay_ms=500, interval_ms=100) -> boolRegister a no-argument callback that repeats while the button remains held.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
callback |
callback |
positional or keyword | Yes | Required value. |
delay_ms |
int |
positional or keyword | No | Optional value. Defaults to 500. |
interval_ms |
int |
positional or keyword | No | Optional value. Defaults to 100. |
button.begin().held_ms() -> int
button.begin().held_ms() -> intReturn the current hold duration while pressed, or the most recent completed hold duration after release. This is useful inside on_release callbacks.
button.begin().last_press_ms() -> int
button.begin().last_press_ms() -> intReturn monotonic system uptime in milliseconds when the most recent press occurred.
button.begin().close() -> bool
button.begin().close() -> boolRelease the GPIO and close the native button object.