Digital Input

Digital Input is used to read the digital input of host devices.

UiFlow2 Example

Get the digital input status

Open the stamplc_digital_input_example.m5f2 project in UiFlow2.

This example demonstrates how to get the status of a digital input and display the status on the screen.

UiFlow2 Code Block:

stamplc_digital_input_example.png

Example output:

None

MicroPython Example

Get the digital input status

This example demonstrates how to get the status of a digital input and display the status on the screen.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import DigitalInput
 9
10
11label0 = None
12digitalinput_0 = None
13
14
15def digitalinput_0_falling_event(args):
16    global label0, digitalinput_0
17    label0.setText(str(digitalinput_0.value()))
18
19
20def digitalinput_0_rising_event(args):
21    global label0, digitalinput_0
22    label0.setText(str(digitalinput_0.value()))
23
24
25def setup():
26    global label0, digitalinput_0
27
28    M5.begin()
29    label0 = Widgets.Label("label0", 112, 57, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
30
31    digitalinput_0 = DigitalInput(1)
32    digitalinput_0.irq(digitalinput_0_falling_event, digitalinput_0.IRQ_FALLING)
33    digitalinput_0.irq(digitalinput_0_rising_event, digitalinput_0.IRQ_RISING)
34    label0.setText(str(digitalinput_0.value()))
35
36
37def loop():
38    global label0, digitalinput_0
39    M5.update()
40
41
42if __name__ == "__main__":
43    try:
44        setup()
45        while True:
46            loop()
47    except (Exception, KeyboardInterrupt) as e:
48        try:
49            from utility import print_error_msg
50
51            print_error_msg(e)
52        except ImportError:
53            print("please update to latest firmware")

Example output:

None

API

DigitalInput

class DigitalInput(id: int)

Initialize a digital input object.

Parameters:

id (int) – The ID of the digital input. The range of ID is 1-8.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hadrware import DigitalInput

in1 = DigitalInput(1)
get_status() bool

Get the status of the digital input.

Returns:

The status of the digital input.

Return type:

bool

UiFlow2 Code Block:

get_status.png

MicroPython Code Block:

in1.get_status()
value() int

Get the value of the digital input.

Returns:

The value of the digital input.

Return type:

int

UiFlow2 Code Block:

value.png

MicroPython Code Block:

in1.value()
irq(handler=None, trigger=IRQ_FALLING | IRQ_RISING) None

Enable interrupt for the pin.

Parameters:
  • handler (function) – The interrupt handler function.

  • trigger (int) – The interrupt trigger mode, DigitalInput.IRQ_FALLING or DigitalInput.IRQ_RISING.

UiFlow2 Code Block:

irq.png

MicroPython Code Block:

def handler(pin):
    print('interrupt triggered')

in1.irq(handler, DigitalInput.IRQ_FALLING)