StamPLC IO

IOStamPLC 通过 I2C 控制 StamPLC IO 扩展板。

UiFlow2 应用示例

电压和电流监测

在 UiFlow2 中打开 stamplc_io_example.m5f2 项目。

该示例将两个输出通道设置为 PWM 模式,然后显示通道 0 和通道 1 的电压与电流。

UiFlow2 代码块:

stamplc_io_example.png

示例输出:

MicroPython 应用示例

电压和电流监测

该示例将两个输出通道设置为 PWM 模式,然后显示通道 0 和通道 1 的电压与电流。

MicroPython 代码块:

 1# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from stamplc import IOStamPLC
 9from stamplc import StamPLC
10
11
12title0 = None
13label0 = None
14label1 = None
15stamplc_0 = None
16stamplc_io_0 = None
17
18
19def setup():
20    global title0, label0, label1, stamplc_0, stamplc_io_0
21
22    M5.begin()
23    Widgets.fillScreen(0x000000)
24    title0 = Widgets.Title("StamPLC IO Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.Montserrat18)
25    label0 = Widgets.Label("label0", 1, 42, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.Montserrat18)
26    label1 = Widgets.Label("label1", 2, 71, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.Montserrat18)
27
28    stamplc_0 = StamPLC()
29    stamplc_io_0 = IOStamPLC(address=0x20)
30    stamplc_io_0.set_output_mode(IOStamPLC.PWM_MODE)
31    stamplc_io_0.set_pwm_config(0, 1, 100)
32    stamplc_io_0.set_pwm_config(1, 1, 100)
33
34
35def loop():
36    global title0, label0, label1, stamplc_0, stamplc_io_0
37    M5.update()
38    label0.setText(
39        str(
40            (
41                str("ch0:")
42                + str(
43                    (
44                        str((stamplc_io_0.get_voltage(0)))
45                        + str(
46                            (
47                                str("mV")
48                                + str(
49                                    (
50                                        str(", ")
51                                        + str((str((stamplc_io_0.get_current(0))) + str("uA")))
52                                    )
53                                )
54                            )
55                        )
56                    )
57                )
58            )
59        )
60    )
61    label1.setText(
62        str(
63            (
64                str("ch1:")
65                + str(
66                    (
67                        str((stamplc_io_0.get_voltage(1)))
68                        + str(
69                            (
70                                str("mV")
71                                + str(
72                                    (
73                                        str(", ")
74                                        + str((str((stamplc_io_0.get_current(1))) + str("uA")))
75                                    )
76                                )
77                            )
78                        )
79                    )
80                )
81            )
82        )
83    )
84
85
86if __name__ == "__main__":
87    try:
88        setup()
89        while True:
90            loop()
91    except (Exception, KeyboardInterrupt) as e:
92        try:
93            from utility import print_error_msg
94
95            print_error_msg(e)
96        except ImportError:
97            print("please update to latest firmware")

示例输出:

API参考

IOStamPLC

class IOStamPLC(i2c=None, address=0x20)

创建一个 StamPLC IO 扩展对象。

参数:
  • i2c – I2C 接口。省略时使用共享的 StamPLC I2C 总线。

  • address (int) – StamPLC IO 扩展的 I2C 地址。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from stamplc import IOStamPLC

io = IOStamPLC(address=0x20)
get_voltage(channel)

获取单个通道的电压。

参数:

channel (int) – 通道索引,01

返回:

电压,单位为 mV。

返回类型:

int

UiFlow2 代码块:

get_voltage.png

MicroPython 代码块:

voltage = io.get_voltage(0)
get_current(channel)

获取单个通道的电流。

参数:

channel (int) – 通道索引,01

返回:

电流,单位为 uA。

返回类型:

int

UiFlow2 代码块:

get_current.png

MicroPython 代码块:

current = io.get_current(0)
get_io_control()

获取 IO 控制寄存器的值。

返回:

IO 控制寄存器值。

返回类型:

int

set_io_control(value)

设置 IO 控制寄存器的值。

参数:

value (int) – IO 控制寄存器值。

set_solid_relay(channel, state)

设置单个通道的固态继电器输出。

参数:
  • channel (int) – 通道索引,01

  • state (bool) – True 打开输出,False 关闭输出。

UiFlow2 代码块:

set_solid_relay.png

MicroPython 代码块:

io.set_solid_relay(0, True)
get_solid_relay(channel)

获取单个通道的固态继电器输出状态。

参数:

channel (int) – 通道索引,01

返回:

输出状态。

返回类型:

bool

UiFlow2 代码块:

get_solid_relay.png

set_ina226_pullup(channel, enable)

启用或禁用单个通道的 INA226 上拉。

参数:
  • channel (int) – 通道索引,01

  • enable (bool) – True 启用上拉,False 禁用上拉。

UiFlow2 代码块:

set_ina226_pullup.png

get_ina226_pullup(channel)

获取单个通道的 INA226 上拉状态。

参数:

channel (int) – 通道索引,01

返回:

上拉状态。

返回类型:

bool

UiFlow2 代码块:

get_ina226_pullup.png

set_relay(state)

设置板载继电器输出。

参数:

state (bool) – True 打开继电器,False 关闭继电器。

UiFlow2 代码块:

set_relay.png

get_relay()

获取板载继电器输出状态。

返回:

继电器状态。

返回类型:

bool

UiFlow2 代码块:

get_relay.png

set_output_mode(mode)

设置输出模式。

参数:

mode (int) – IOStamPLC.OUTPUT_IO_MODEIOStamPLC.PWM_MODE

UiFlow2 代码块:

set_output_mode.png

MicroPython 代码块:

io.set_output_mode(IOStamPLC.PWM_MODE)
get_output_mode()

获取输出模式。

返回:

IOStamPLC.OUTPUT_IO_MODEIOStamPLC.PWM_MODE

返回类型:

int

UiFlow2 代码块:

get_output_mode.png

set_ina226_config(channel, value)

设置单个通道的 INA226 配置寄存器。

参数:
  • channel (int) – 通道索引,01

  • value (int) – 寄存器值。

get_ina226_config(channel)

获取单个通道的 INA226 配置寄存器。

参数:

channel (int) – 通道索引,01

返回:

寄存器值。

返回类型:

int

set_pwm_config(channel, freq, duty)

设置单个通道的 PWM 频率和占空比。

参数:
  • channel (int) – 通道索引,01

  • freq (int) – PWM 频率,1100

  • duty (int) – PWM 占空比,01000

UiFlow2 代码块:

set_pwm_config.png

MicroPython 代码块:

io.set_pwm_config(0, 1, 100)
get_pwm_config(channel)

获取单个通道的 PWM 频率和占空比。

参数:

channel (int) – 通道索引,01

返回:

(freq, duty)

返回类型:

tuple

get_firmware_version()

获取固件版本。

返回:

固件版本。

返回类型:

int

UiFlow2 代码块:

get_firmware_version.png

MicroPython 代码块:

version = io.get_firmware_version()
get_i2c_address()

获取已配置的 I2C 地址。

返回:

I2C 地址。

返回类型:

int

UiFlow2 代码块:

get_i2c_address.png

MicroPython 代码块:

address = io.get_i2c_address()
refresh_i2c_address()

从设备刷新当前使用的 I2C 地址。