Hbridge Unit
该库是 Unit HBridge 的驱动程序。仅 v1.1 版本支持电流测量。
支持以下产品:
UiFlow2 应用示例
电机速度与旋转方向控制
在 UiFlow2 中打开 cores3_hbridge_motor_control.m5f2 项目。
该示例演示如何控制电机的速度并切换其旋转方向。
UiFlow2 代码块:
示例输出:
None
MicroPython 应用示例
电机速度与旋转方向控制
该示例演示如何控制电机的速度并切换其旋转方向。
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 * 8from hardware import I2C 9from hardware import Pin 10from unit import HbridgeUnit 11import time 12 13 14title0 = None 15label0 = None 16label_speed = None 17i2c0 = None 18hbridge_0 = None 19speed = None 20dir2 = None 21 22 23def setup(): 24 global title0, label0, label_speed, i2c0, hbridge_0, speed, dir2 25 M5.begin() 26 Widgets.fillScreen(0x222222) 27 title0 = Widgets.Title("HBridge Motor Control", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 28 label0 = Widgets.Label("Speed:", 35, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 29 label_speed = Widgets.Label("0", 110, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 30 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000) 31 hbridge_0 = HbridgeUnit(i2c0, 0x20) 32 hbridge_0.set_pwm_freq(1000) 33 speed = 0 34 hbridge_0.set_direction(0) 35 dir2 = True 36 37 38def loop(): 39 global title0, label0, label_speed, i2c0, hbridge_0, speed, dir2 40 M5.update() 41 speed = speed + 1 42 label0.setText(str(speed)) 43 if speed > 99: 44 speed = 0 45 dir2 = not dir2 46 if dir2: 47 hbridge_0.set_direction(1) 48 else: 49 hbridge_0.set_direction(2) 50 time.sleep_ms(1000) 51 hbridge_0.set_percentage_pwm(speed, 8) 52 time.sleep_ms(50) 53 54 55if __name__ == "__main__": 56 try: 57 setup() 58 while True: 59 loop() 60 except (Exception, KeyboardInterrupt) as e: 61 try: 62 from utility import print_error_msg 63 64 print_error_msg(e) 65 except ImportError: 66 print("please update to latest firmware")
示例输出:
None
API参考
HbridgeUnit
- class unit.hbridge.HbridgeUnit(i2c, address=32)
基类:
object创建一个 HbridgeUnit 对象。
- 参数:
i2c (machine.I2C | PAHUBUnit) – I2C 端口。
UiFlow2 代码块:

MicroPython 代码块:
from unit import HbridgeUnit unit_hbridge_0 = HbridgeUnit(i2c0, 0x20)
- get_driver_config(reg=0)
获取驱动配置。
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.get_driver_config(reg)
- set_direction(dir=0)
设置方向
该方法用于控制电机的运动方向或使其停止。
- 参数:
dir (int) – 方向控制参数:- 0:停止 - 1:前进 - 2:反向
- 返回类型:
None
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.set_direction(dir)
- set_8bit_pwm(duty=0)
设置 8 位 PWM 占空比
- 参数:
duty (int) – PWM duty,占空比范围:0~255
- 返回类型:
None
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.set_8bit_pwm(duty)
- set_16bit_pwm(duty=0)
设置 16 位 PWM 占空比
- 参数:
duty (int) – PWM 占空比,范围:0~65535
- 返回类型:
None
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.set_16bit_pwm(duty)
- set_percentage_pwm(duty=0, res=8)
根据百分比设置 PWM 输出。
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.set_percentage_pwm(duty, reg)
- set_pwm_freq(freq=0)
设置 PWM 频率。
- 参数:
freq (int) – PWM 频率。
- 返回类型:
None
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.set_pwm_freq(freq)
- get_adc_value(raw=0, res=8)
获取 ADC 值。
该方法会根据指定的分辨率读取 ADC 值,支持 8-bit 和 16-bit ADC 分辨率。如果将 raw 设置为 1,则返回原始 ADC 值;否则会计算并返回对应的电压值。
- 参数:
- 返回:
原始 ADC 值或计算得到的电压,取决于 raw。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.get_adc_value(raw, res)
- get_vin_current()
获取输入电压电流(单位:A)。
- 返回:
输入电压电流值。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
unit_hbridge_0.get_vin_current()



