Button Unit

BUTTON is a single button Unit. The button status can be detected by the input pin by simply capturing the high/low electrical level. With the default active-low configuration, pressing the button produces a low signal and releasing it produces a high signal.

Support the following products:

ButtonUnit

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from unit import ButtonUnit
 9
10
11title0 = None
12label1 = None
13label0 = None
14button_0 = None
15
16
17def setup():
18    global title0, label1, label0, button_0
19
20    M5.begin()
21    Widgets.fillScreen(0x222222)
22    title0 = Widgets.Title(
23        "ButtonUnit Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
24    )
25    label1 = Widgets.Label(
26        "Button State:", 395, 150, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
27    )
28    label0 = Widgets.Label(
29        "Button Counter Value:", 1, 102, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
30    )
31
32    button_0 = ButtonUnit((33, 32), True, type=2)
33
34
35def loop():
36    global title0, label1, label0, button_0
37    M5.update()
38    label0.setText(str((str("Button Counter Value:") + str((button_0.count_value)))))
39
40
41if __name__ == "__main__":
42    try:
43        setup()
44        while True:
45            loop()
46    except (Exception, KeyboardInterrupt) as e:
47        try:
48            from utility import print_error_msg
49
50            print_error_msg(e)
51        except ImportError:
52            print("please update to latest firmware")

UIFLOW2 Example:

example.png

button_core2_example.m5f2

class ButtonUnit

Constructors

class ButtonUnit(port, active_low=True, type=SWITCH)

Initialize the button input. ButtonUnit is an alias of LIMITUnit.

Parameters:
  • port (tuple) – Grove GPIO tuple; the input uses the first pin, port[0].

  • active_low (bool) – Whether a low signal means pressed. Default is True.

  • type (int) – SWITCH (default) or COUNTER, imported from unit.limit.

The input enables Pin.PULL_UP internally. There is no pullup_active constructor argument.

from unit import ButtonUnit
from unit.limit import SWITCH, COUNTER

button = ButtonUnit((33, 32), active_low=True, type=SWITCH)

UIFLOW2:

init.png

Methods

ButtonUnit.count_reset()

Reset the count value to zero.

UIFLOW2:

count_reset.png

ButtonUnit.isHolding()

Check if the button is currently being held.

UIFLOW2:

isHolding.png

ButtonUnit.setCallback(type, cb)

Set a callback function for a specified button event type.

Parameters:
  • type – The event type (e.g., WAS_CLICKED, WAS_DOUBLECLICKED).

  • cb – The callback function to be executed for the event.

UIFLOW2:

setCallback.png

ButtonUnit.tick(pin)

Monitor the state transitions of a button based on its pin state and trigger appropriate handlers.

UIFLOW2:

tick.png