Relay

Relay is used to control the relay of host devices.

UiFlow2 Example

Relay control

Open the stamplc_relay_example.m5f2 project in UiFlow2.

This example demonstrates how to use a button to control the state of a relay and display the relay’s state value on the screen.

UiFlow2 Code Block:

stamplc_relay_example.png

Example output:

None

MicroPython Example

Relay control

This example demonstrates how to use a button to control the state of a relay and display the relay’s state value 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 Relay
 9
10
11label0 = None
12relay_0 = None
13
14
15def btnA_wasClicked_event(state):  #  noqa: N802
16    global label0, relay_0
17    relay_0.on()
18
19
20def btnB_wasClicked_event(state):  #  noqa: N802
21    global label0, relay_0
22    relay_0.off()
23
24
25def btnC_wasClicked_event(state):  #  noqa: N802
26    global label0, relay_0
27    relay_0.set_status(not (relay_0.get_status()))
28
29
30def setup():
31    global label0, relay_0
32
33    M5.begin()
34    label0 = Widgets.Label("label0", 75, 28, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
35
36    BtnA.setCallback(type=BtnA.CB_TYPE.WAS_CLICKED, cb=btnA_wasClicked_event)
37    BtnB.setCallback(type=BtnB.CB_TYPE.WAS_CLICKED, cb=btnB_wasClicked_event)
38    BtnC.setCallback(type=BtnC.CB_TYPE.WAS_CLICKED, cb=btnC_wasClicked_event)
39
40    relay_0 = Relay(1)
41
42
43def loop():
44    global label0, relay_0
45    M5.update()
46    label0.setText(str(relay_0.value()))
47
48
49if __name__ == "__main__":
50    try:
51        setup()
52        while True:
53            loop()
54    except (Exception, KeyboardInterrupt) as e:
55        try:
56            from utility import print_error_msg
57
58            print_error_msg(e)
59        except ImportError:
60            print("please update to latest firmware")

Example output:

None

API

Relay

class Relay(id: int)

Initialize a relay object.

Parameters:

id (int) – The ID of the relay. The range of ID is 1-4.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hadrware import Relay

relay = Relay(1)
on() None

Turn on the relay.

UiFlow2 Code Block:

on.png

MicroPython Code Block:

relay.on()
off() None

Turn off the relay.

UiFlow2 Code Block:

off.png

MicroPython Code Block:

relay.off()
value() int

Get the value of the relay.

Returns:

The value of the relay.

Return type:

int

UiFlow2 Code Block:

set_value.png

get_value.png

MicroPython Code Block:

relay.value(1)
relay.value()
get_status() bool

Get the status of the relay.

Returns:

The status of the relay.

Return type:

bool

UiFlow2 Code Block:

get_status.png

MicroPython Code Block:

relay.get_status()
set_status(status: bool) None

Set the status of the relay.

Parameters:

status (bool) – The status of the relay.

UiFlow2 Code Block:

set_status.png

MicroPython Code Block:

relay.set_status(True)