16340 模块

The following products are supported:

Module 16340

Module16340 是一个电源监测电池模块。它通过 Module I2C 总线访问板载 INA226 传感器,用于读取总线电压、分流电压、电流和功率。

UiFlow2 应用示例

读取电源监测数值

该示例创建 Module16340 对象,并从板载 INA226 传感器读取电压、电流和功率值。

在 UiFlow2 中打开 base16340_cores3_example.m5f2 项目。

UiFlow2 代码块:

init.png

示例输出:

None

MicroPython 应用示例

读取电源监测数值

该示例打印总线电压、分流电压、电流和功率值。

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 module import Module16340
 9import time
10
11
12title0 = None
13label0 = None
14label1 = None
15label2 = None
16label3 = None
17module16340_0 = None
18
19
20def setup():
21    global title0, label0, label1, label2, label3, module16340_0
22
23    M5.begin()
24    Widgets.setRotation(1)
25    Widgets.fillScreen(0x222222)
26    title0 = Widgets.Title(
27        "Base16340 CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.Montserrat18
28    )
29    label0 = Widgets.Label("label0", 2, 50, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.Montserrat18)
30    label1 = Widgets.Label("label1", 2, 94, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.Montserrat18)
31    label2 = Widgets.Label("label2", 2, 138, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.Montserrat18)
32    label3 = Widgets.Label("label3", 2, 176, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.Montserrat18)
33
34    module16340_0 = Module16340(address=0x45)
35
36
37def loop():
38    global title0, label0, label1, label2, label3, module16340_0
39    M5.update()
40    time.sleep(1)
41    label0.setText(str((str("Shunt Voltage:") + str((module16340_0.read_shunt_voltage())))))
42    label1.setText(str((str("Bus Voltage:") + str((module16340_0.read_bus_voltage())))))
43    label2.setText(str((str("Current:") + str((module16340_0.read_current())))))
44    label3.setText(str((str("Power:") + str((module16340_0.read_power())))))
45
46
47if __name__ == "__main__":
48    try:
49        setup()
50        while True:
51            loop()
52    except (Exception, KeyboardInterrupt) as e:
53        try:
54            from utility import print_error_msg
55
56            print_error_msg(e)
57        except ImportError:
58            print("please update to latest firmware")

示例输出:

None

API参考

Module16340

class module.module16340.Module16340(address=69)

基类:INA226

创建 Module16340 对象。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from module import Module16340

module16340 = Module16340()
calibrate(max_expected_current=0, cal_value=None)

校准 INA226。

参数:
  • max_expected_current (float) – 预期最大电流,单位为安培。

  • cal_value (int) – 要使用的校准值。如果为 None,将根据预期最大电流和分流电阻值计算。

MicroPython 代码块:

ina226_0.calibrate(10)

ina226_0.calibrate(cal_value=0xC80)
read_bus_voltage()

读取总线电压,单位为伏特。

返回:

总线电压,单位为伏特。

返回类型:

float

UiFlow2 代码块:

read_bus_voltage.png

MicroPython 代码块:

ina226_0.read_bus_voltage()
read_current()

读取电流,单位为安培。

返回:

电流,单位为安培。

返回类型:

float

UiFlow2 代码块:

read_current.png

MicroPython 代码块:

ina226_0.read_current()
read_power()

读取功率,单位为瓦特。

返回:

功率,单位为瓦特。

返回类型:

float

UiFlow2 代码块:

read_power.png

MicroPython 代码块:

ina226_0.read_power()
read_shunt_voltage()

读取分流电压,单位为伏特。

返回:

分流电压,单位为伏特。

返回类型:

float

UiFlow2 代码块:

read_shunt_voltage.png

MicroPython 代码块:

ina226_0.read_shunt_voltage()