MojoScale Studio Docs
API Reference

button

High-level button, switch, and digital input handling.

Studio Docs Sensors & I/O

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.

API 12 available

button.begin(pin: int, pull: str = None, active: str = None, debounce_ms: int = None) -> button_input

Create a debounced button input.

Parameters
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.
Returns

button_input button_input object.

button.begin().pressed() -> bool

Return True when the debounced button state is pressed.

button.begin().released() -> bool

Return True when the debounced button state is released.

button.begin().on_press(callback) -> bool

Register a no-argument callback that runs once when the button becomes pressed. Pass None to clear.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Required value.

button.begin().on_release(callback) -> bool

Register a no-argument callback that runs once when the button becomes released. Pass None to clear.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Required value.

button.begin().on_click(callback) -> bool

Register a no-argument callback for a short press and release. Pass None to clear.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Required value.

button.begin().on_double_click(callback) -> bool

Register a no-argument callback for two quick clicks. Pass None to clear.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Required value.

button.begin().on_long_press(callback, duration_ms=1000) -> bool

Register a no-argument callback that runs once after the button is held for duration_ms.

Parameters
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

Register a no-argument callback that repeats while the button remains held.

Parameters
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

Return 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

Return monotonic system uptime in milliseconds when the most recent press occurred.

button.begin().close() -> bool

Release the GPIO and close the native button object.