INA226 Unit

这是 INA226 Unit 的驱动库,用于从 INA226 传感器获取电流和功率数据。

支持以下产品:

INA226

UiFlow2 应用示例

获取值

在 UiFlow2 中打开 ina226_core2_example.m5f2 项目。

该示例从 INA226 Unit 获取电流、电压和功率值,并将其显示在屏幕上。

UiFlow2 代码块:

example.png

示例输出:

None

MicroPython 应用示例

获取值

该示例从 INA226 Unit 获取电流、电压和功率值,并将其显示在屏幕上。

MicroPython 代码块:

  1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4
  5import os, sys, io
  6import M5
  7from M5 import *
  8import m5ui
  9import lvgl as lv
 10from hardware import I2C
 11from hardware import Pin
 12import time
 13from unit import INA226Unit
 14
 15
 16page0 = None
 17label0 = None
 18label1 = None
 19label2 = None
 20label3 = None
 21i2c0 = None
 22ina226_0 = None
 23
 24
 25def setup():
 26    global page0, label0, label1, label2, label3, i2c0, ina226_0
 27
 28    M5.begin()
 29    Widgets.setRotation(1)
 30    m5ui.init()
 31    page0 = m5ui.M5Page(bg_c=0xFFFFFF)
 32    label0 = m5ui.M5Label(
 33        "Bus Voltage:",
 34        x=0,
 35        y=56,
 36        text_c=0x000000,
 37        bg_c=0xFFFFFF,
 38        bg_opa=0,
 39        font=lv.font_montserrat_16,
 40        parent=page0,
 41    )
 42    label1 = m5ui.M5Label(
 43        "Current:",
 44        x=0,
 45        y=88,
 46        text_c=0x000000,
 47        bg_c=0xFFFFFF,
 48        bg_opa=0,
 49        font=lv.font_montserrat_16,
 50        parent=page0,
 51    )
 52    label2 = m5ui.M5Label(
 53        "Power:",
 54        x=0,
 55        y=124,
 56        text_c=0x000000,
 57        bg_c=0xFFFFFF,
 58        bg_opa=0,
 59        font=lv.font_montserrat_16,
 60        parent=page0,
 61    )
 62    label3 = m5ui.M5Label(
 63        "Shunt Voltage:",
 64        x=0,
 65        y=161,
 66        text_c=0x000000,
 67        bg_c=0xFFFFFF,
 68        bg_opa=0,
 69        font=lv.font_montserrat_16,
 70        parent=page0,
 71    )
 72
 73    i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
 74    ina226_0 = INA226Unit(i2c0, 0x41, type="1A")
 75    page0.screen_load()
 76
 77
 78def loop():
 79    global page0, label0, label1, label2, label3, i2c0, ina226_0
 80    M5.update()
 81    time.sleep_ms(500)
 82    label0.set_text(
 83        str((str("Bus Voltage:") + str((str((ina226_0.read_bus_voltage())) + str("V")))))
 84    )
 85    label1.set_text(str((str("Current:") + str((str((ina226_0.read_current())) + str("A"))))))
 86    label2.set_text(str((str("Power:") + str((str((ina226_0.read_power())) + str("W"))))))
 87    label3.set_text(
 88        str((str("Shunt Voltage:") + str((str((ina226_0.read_shunt_voltage())) + str("V")))))
 89    )
 90
 91
 92if __name__ == "__main__":
 93    try:
 94        setup()
 95        while True:
 96            loop()
 97    except (Exception, KeyboardInterrupt) as e:
 98        try:
 99            m5ui.deinit()
100            from utility import print_error_msg
101
102            print_error_msg(e)
103        except ImportError:
104            print("please update to latest firmware")

示例输出:

None

API参考

INA226Unit

class unit.ina226.INA226Unit(i2c, address=65, type='10A')

基类:INA226

创建一个 INA226Unit 对象。

参数:
  • i2c (I2C) – Accel Unit 连接的 I2C 总线。

  • address (int) – 设备的 I2C 地址。默认为 0x41。

  • type (str) – INA226 的类型。默认值为 “10A”。可选值为 “1A” 和 “10A”。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import I2C
from unit import INA226Unit

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
ina226_0 = INA226Unit(i2c0, address=0x41, type="10A")

INA226

class driver.ina226.INA226(i2c, addr=64, shunt_resistor=0.02)

基类:object

创建一个 INA226 对象。

参数:
  • i2c (I2C) – INA226 连接到的 I2C 总线。

  • address (int) – 设备的 I2C 地址。

  • shunt_resistor (float) – 分流电阻的阻值(单位:Ω)。默认值为 0.2。

UiFlow2 代码块:

init.png

MicroPython 代码块:

from hardware import I2C
from driver import INA226

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
ina226 = INA226(i2c0, 0x40, shunt_resistor=0.02)
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_shunt_voltage()

读取分流电压(单位:V)。

返回:

分流电阻电压(单位:V)。

返回类型:

float

UiFlow2 代码块:

read_shunt_voltage.png

MicroPython 代码块:

ina226_0.read_shunt_voltage()
read_bus_voltage()

读取总线电压,单位为 V。

返回:

总线电压,单位为 V。

返回类型:

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()

读取以瓦特 (W) 为单位的功率。

返回:

功率,单位为 Watt。

返回类型:

float

UiFlow2 代码块:

read_power.png

MicroPython 代码块:

ina226_0.read_power()