Hbridge Unit
This library is the driver for Unit HBridge. Only version v1.1 supports current measurement.
Support the following products:
UiFlow2 Example
Motor speed and rotate direction control
Open the cores3_hbridge_motor_control.m5f2 project in UiFlow2.
This example demonstrates how to control the motor’s speed and switch its rotation direction.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
Motor speed and rotate direction control
This example demonstrates how to control the motor’s speed and switch its rotation direction.
MicroPython Code Block:
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")
Example output:
None
API
HbridgeUnit
- class unit.hbridge.HbridgeUnit(i2c, address=32)
Bases:
object
Create an HbridgeUnit object.
- Parameters:
i2c (machine.I2C | PAHUBUnit) – I2C port.
UiFlow2 Code Block:
MicroPython Code Block:
from unit import HbridgeUnit unit_hbridge_0 = HbridgeUnit(i2c0, 0x20)
- get_adc_value(raw=0, res=8)
Get ADC value.
This method retrieves the ADC value based on the specified resolution. It supports both 8-bit and 16-bit ADC resolutions. If raw is set to 1, the raw ADC value is returned. Otherwise, the corresponding voltage is calculated and returned.
- Parameters:
- Returns:
The raw ADC value or the calculated voltage, depending on raw.
- Return type:
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.get_adc_value(raw, res)
- get_device_status(mode)
Get device status.
get firmware version and i2c address.
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.get_device_status(mode)
- get_driver_config(reg=0)
Get driver config.
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.get_driver_config(reg)
- get_vin_current()
Get the input voltage current (unit: A).
- Returns:
The input voltage current value.
- Return type:
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.get_vin_current()
- set_16bit_pwm(duty=0)
Set 16-bit pwm duty cycle
- Parameters:
duty (int) – pwm duty, range: 0~65535
- Return type:
None
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.set_16bit_pwm(duty)
- set_8bit_pwm(duty=0)
Set 8-bit pwm duty cycle
- Parameters:
duty (int) – PWM duty, range: 0~255
- Return type:
None
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.set_8bit_pwm(duty)
- set_direction(dir=0)
Set direction
This method controls the motor’s movement direction or stops it.
- Parameters:
dir (int) – Direction control parameter: - 0: Stop - 1: Forward - 2: Reverse
- Return type:
None
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.set_direction(dir)
- set_percentage_pwm(duty=0, res=8)
Set the PWM output based on percentage.
- Parameters:
- Return type:
None
UiFlow2 Code Block:
MicroPython Code Block:
unit_hbridge_0.set_percentage_pwm(duty, reg)