Relay2 Module

Support the following products:

Relay2Module

Micropython Example:

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4import os, sys, io
 5import M5
 6from M5 import *
 7from module import Relay2Module
 8from hardware import *
 9
10
11title0 = None
12label0 = None
13label1 = None
14label2 = None
15label3 = None
16relay2_0 = None
17
18
19def setup():
20    global title0, label0, label1, label2, label3, relay2_0
21
22    M5.begin()
23    Widgets.fillScreen(0x222222)
24    title0 = Widgets.Title("2Relay Module Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
25    label0 = Widgets.Label("Relay 1:", 2, 53, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
26    label1 = Widgets.Label("Relay 2:", 2, 119, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27    label2 = Widgets.Label("I2C Addr:", 1, 177, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label3 = Widgets.Label(
29        "FW Version:", 181, 177, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18
30    )
31
32    relay2_0 = Relay2Module(address=0x25)
33    label2.setText(str((str("I2C Addr:") + str((relay2_0.get_i2c_address())))))
34    label3.setText(str((str("FW Version:") + str((relay2_0.get_firmware_version())))))
35
36
37def loop():
38    global title0, label0, label1, label2, label3, relay2_0
39    M5.update()
40    label0.setText(str((str("Relay 1:") + str((relay2_0.get_relay_status(1))))))
41    label1.setText(str((str("Relay 2:") + str((relay2_0.get_relay_status(2))))))
42    if BtnA.isPressed():
43        relay2_0.set_relay_state(1, 1)
44    else:
45        relay2_0.set_relay_state(1, 0)
46    if BtnB.isPressed():
47        relay2_0.set_relay_state(2, 1)
48    else:
49        relay2_0.set_relay_state(2, 0)
50
51
52if __name__ == "__main__":
53    try:
54        setup()
55        while True:
56            loop()
57    except (Exception, KeyboardInterrupt) as e:
58        try:
59            from utility import print_error_msg
60
61            print_error_msg(e)
62        except ImportError:
63            print("please update to latest firmware")

UIFLOW2 Example:

example.png

class Relay2Module

Constructors

class Relay2Module(address)

Initialize the 2Relay Module with the specified I2C address.

Parameters:

address (int|list|tuple) – I2C address of the Relay2Module.

UIFLOW2:

init.png

Methods

Relay2Module.set_relay_state(num, state) None

Set the state of a specific relay.

Parameters:
  • num (int) – The relay number (1 or 2).

  • state (bool) – True to turn on, False to turn off.

UIFLOW2:

set_relay_state.png

Relay2Module.get_relay_status(num) bool

Get the status of a specific relay.

Parameters:

num (int) – The relay number (1 or 2).

UIFLOW2:

get_relay_status.png

Relay2Module.set_all_relay_state(state) None

Set the state of both relays simultaneously.

Parameters:

state (bool) – True to turn on both relays, False to turn off both relays.

UIFLOW2:

set_all_relay_state.png

Relay2Module.get_firmware_version() int

Get the firmware version of the Relay2 Module.

UIFLOW2:

get_firmware_version.png

Relay2Module.set_i2c_address(addr) None

Set a new I2C address(0x08~0x77) for the Relay2 Module.

Parameters:

addr (int) – The new I2C address to set.

UIFLOW2:

set_i2c_address.png

Relay2Module.get_i2c_address() int

Get the current I2C address of the Relay2 Module.

UIFLOW2:

get_i2c_address.png