Relay2 Unit
This is the driver library of Relay2 Unit, which is used to control the relay.
Support the following products:
UiFlow2 Example
control relay
Open the relay2_core2_example.m5f2 project in UiFlow2.
This example controls the relay of the Relay2 Unit and displays it on the screen.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
control relay
This example controls the relay of the Relay2 Unit and displays it 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 unit import Relay2Unit 9 10 11title0 = None 12label2 = None 13label0 = None 14label3 = None 15label1 = None 16relay2_0 = None 17 18 19def setup(): 20 global title0, label2, label0, label3, label1, relay2_0 21 22 M5.begin() 23 Widgets.fillScreen(0x222222) 24 title0 = Widgets.Title( 25 "Relay2Unit Core2 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18 26 ) 27 label2 = Widgets.Label("Relay1", 38, 214, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 28 label0 = Widgets.Label("label0", 2, 91, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 29 label3 = Widgets.Label("Relay2", 220, 214, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 30 label1 = Widgets.Label("label1", 2, 136, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 31 32 relay2_0 = Relay2Unit((33, 32)) 33 34 35def loop(): 36 global title0, label2, label0, label3, label1, relay2_0 37 M5.update() 38 label0.setText(str((str("Relay1 State:") + str((relay2_0.get_relay_status(1)))))) 39 label1.setText(str((str("Relay2 State:") + str((relay2_0.get_relay_status(2)))))) 40 if BtnA.wasPressed(): 41 relay2_0.set_relay_cntrl(1, not (relay2_0.get_relay_status(1))) 42 elif BtnC.wasPressed(): 43 relay2_0.set_relay_cntrl(2, not (relay2_0.get_relay_status(2))) 44 45 46if __name__ == "__main__": 47 try: 48 setup() 49 while True: 50 loop() 51 except (Exception, KeyboardInterrupt) as e: 52 try: 53 from utility import print_error_msg 54 55 print_error_msg(e) 56 except ImportError: 57 print("please update to latest firmware")
Example output:
None
API
Relay2Unit
- class unit.relay2.Relay2Unit(port)
Bases:
objectCreate an Relay2Unit object.
- Parameters:
port (tuple) – The port of the relay.
UiFlow2 Code Block:

MicroPython Code Block:
from unit import Relay2Unit relay2_0 = Relay2Unit((32, 26))
- set_relay_cntrl(num=1, control=0)
Set the on/off status of a relay
- Parameters:
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
relay2_0.set_relay_cntrl(1, 1)


