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:
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:

MicroPython Code Block:
from hadrware import Relay relay = Relay(1)
- value() int
Get the value of the relay.
- Returns:
The value of the relay.
- Return type:
UiFlow2 Code Block:


MicroPython Code Block:
relay.value(1) relay.value()




