BLDCDriver Unit

This library is the driver for Unit BLDCDriver.

Support the following products:

Unit BLDCDriver

UiFlow2 Example

Motor speed control

Open the cores3_bldc_driver_example.m5f2 project in UiFlow2.

The example program gradually increases the motor speed and then stops the motor.

UiFlow2 Code Block:

cores3_bldc_driver_example.png

Example output:

None

MicroPython Example

Motor speed control

The example program gradually increases the motor speed and then stops the motor.

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 BLDCDriverUnit
11import time
12
13
14title0 = None
15label0 = None
16label_speed = None
17i2c0 = None
18bldcdriver_0 = None
19speed = None
20
21
22def setup():
23    global title0, label0, label_speed, i2c0, bldcdriver_0, speed
24    M5.begin()
25    Widgets.fillScreen(0x222222)
26    title0 = Widgets.Title("BLDCDriver Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24)
27    label0 = Widgets.Label("Speed: ", 35, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
28    label_speed = Widgets.Label("0", 115, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
30    bldcdriver_0 = BLDCDriverUnit(i2c0, 0x65)
31    bldcdriver_0.set_mode(0)
32    bldcdriver_0.set_open_loop_pwm(500)
33    bldcdriver_0.set_rpm_int(0)
34    speed = 0
35
36
37def loop():
38    global title0, label0, label_speed, i2c0, bldcdriver_0, speed
39    M5.update()
40    if speed < 300:
41        speed = speed + 5
42        label_speed.setText(str(speed))
43        bldcdriver_0.set_rpm_int(speed)
44        time.sleep_ms(100)
45    else:
46        bldcdriver_0.set_mode(1)
47        label_speed.setText(str("0"))
48        time.sleep_ms(500)
49
50
51if __name__ == "__main__":
52    try:
53        setup()
54        while True:
55            loop()
56    except (Exception, KeyboardInterrupt) as e:
57        try:
58            from utility import print_error_msg
59
60            print_error_msg(e)
61        except ImportError:
62            print("please update to latest firmware")

Example output:

None

API

BLDCDriverUnit

class unit.bldc_driver.BLDCDriverUnit(i2c, address=101)

Bases: object

Create an BLDCDriverUnit object.

Parameters:

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from unit import BLDCDriverUnit

unit_bldcdriver_0 = BLDCDriverUnit(i2c0, 0x65)
property get_current_mode: int

Get the current mode setting.

Returns:

current mode.

Return type:

int

UiFlow2 Code Block:

get_current_mode.png

MicroPython Code Block:

unit_bldcdriver_0.get_current_mode()
get_device_spec(mode)

Get device firmware version and I2C address.

This method retrieves either the firmware version or the I2C address of the device based on the provided mode.

Parameters:

mode (int) – The mode to determine what information to fetch. - 0xFE: Retrieve firmware version. - 0xFF: Retrieve I2C address.

Return type:

None

UiFlow2 Code Block:

get_device_spec.png

MicroPython Code Block:

unit_device.get_device_spec(mode)
property get_motor_current_direction: int

Get the current direction setting.

Returns:

current direction.

Return type:

int

UiFlow2 Code Block:

get_motor_current_direction.png

MicroPython Code Block:

unit_bldcdriver_0.get_motor_current_direction()
property get_motor_current_model: int

Get the motor current model setting.

Returns:

motor current model.

Return type:

int

UiFlow2 Code Block:

get_motor_current_model.png

MicroPython Code Block:

unit_bldcdriver_0.get_motor_current_model()
property get_motor_pole_pairs: int

Get the pole pairs setting.

Returns:

motor pole pairs.

Return type:

int

UiFlow2 Code Block:

get_motor_pole_pairs.png

MicroPython Code Block:

unit_bldcdriver_0.get_motor_pole_pairs()
property get_motor_status: int

Get motor status.

Returns:

motor status.

Return type:

int

UiFlow2 Code Block:

get_motor_status.png

MicroPython Code Block:

unit_bldcdriver_0.get_motor_status()
property get_open_loop_pwm: int

Get the open loop pwm.

Returns:

open loop pwm.

Return type:

int

UiFlow2 Code Block:

get_open_loop_pwm.png

MicroPython Code Block:

unit_bldcdriver_0.get_open_loop_pwm()
property get_pid_value: tuple

Get the PID value.

This method retrieves the PID values from the specified register and returns them as a tuple.

Returns:

A tuple containing the PID values (proportional, integral, derivative).

Return type:

tuple[int, int, int]

UiFlow2 Code Block:

get_pid_value.png

MicroPython Code Block:

unit_bldcdriver_0.get_pid_value()
property get_read_back_freq_float: float

Get the read back frequency in float.

Returns:

read back frequency.

Return type:

float

UiFlow2 Code Block:

get_read_back_freq_float.png

MicroPython Code Block:

unit_bldcdriver_0.get_read_back_freq_float()
property get_read_back_freq_int: int

Get the read back frequency in int.

Returns:

read back frequency.

Return type:

int

UiFlow2 Code Block:

get_read_back_freq_int.png

MicroPython Code Block:

unit_bldcdriver_0.get_read_back_freq_int()
property get_read_back_freq_str: str

Get the read back frequency in str.

Returns:

read back frequency.

Return type:

str

UiFlow2 Code Block:

get_read_back_freq_str.png

MicroPython Code Block:

unit_bldcdriver_0.get_read_back_freq_str()
property get_read_back_rpm_float: float

Get the read back rpm in float.

Returns:

read back rpm.

Return type:

float

UiFlow2 Code Block:

get_read_back_rpm_float.png

MicroPython Code Block:

unit_bldcdriver_0.get_read_back_rpm_float()
property get_read_back_rpm_int: int

Get the read back rpm in int.

Returns:

read back rpm.

Return type:

int

UiFlow2 Code Block:

get_read_back_rpm_int.png

MicroPython Code Block:

unit_bldcdriver_0.get_read_back_rpm_int()
property get_read_back_rpm_str: str

Get the read back rpm in str.

Returns:

read back rpm.

Return type:

str

UiFlow2 Code Block:

get_read_back_rpm_str.png

MicroPython Code Block:

unit_bldcdriver_0.get_read_back_rpm_str()
property get_rpm_float: float

Get the rpm in float.

Returns:

rpm.

Return type:

float

UiFlow2 Code Block:

get_rpm_float.png

MicroPython Code Block:

unit_bldcdriver_0.get_rpm_float()
property get_rpm_int: int

Get the rpm in int.

Returns:

Revolutions per minute.

Return type:

int

UiFlow2 Code Block:

get_rpm_int.png

MicroPython Code Block:

unit_bldcdriver_0.get_rpm_int()
save_data_in_flash()

Save motor data to flash.

UiFlow2 Code Block:

save_data_in_flash.png

MicroPython Code Block:

unit_bldcdriver_0.save_data_in_flash()
Return type:

None

set_direction(direction)

Set the direction.

Parameters:

model (int) – 0 forward, 1 backward.

Return type:

None

UiFlow2 Code Block:

set_direction.png

MicroPython Code Block:

unit_bldcdriver_0.set_direction()
set_i2c_address(addr)

Set the I2C address.

Parameters:

addr (int) – The new I2C address, range: 1~127.

Return type:

None

UiFlow2 Code Block:

set_i2c_address.png

MicroPython Code Block:

unit_device.set_i2c_address(addr)
set_mode(mode)

Set the mode setting.

Param:

int mode: 0 mean open loop, 1 mean close loop.

Parameters:

mode (int)

Return type:

None

UiFlow2 Code Block:

set_mode.png

MicroPython Code Block:

unit_bldcdriver_0.set_mode(mode)
set_motor_model(model)

Set the motor model setting.

Parameters:

model (int) – 0 mean low speed, 1 mean high speed.

Return type:

int

UiFlow2 Code Block:

set_motor_model.png

MicroPython Code Block:

unit_bldcdriver_0.set_motor_model(model)
set_open_loop_pwm(pwm)

Set the open loop pwm.

Parameters:

pwm (int) – open loop pwm., range: 0~2047.

Return type:

int

UiFlow2 Code Block:

set_open_loop_pwm.png

MicroPython Code Block:

unit_bldcdriver_0.set_open_loop_pwm(pwm)
set_pid_value(p, i, d)

! Set the PID values (Proportional, Integral, Derivative).

This method sets the PID values to the specified register, which will control the motor’s PID behavior.

Parameters:
  • p (int) – The proportional value.

  • i (int) – The integral value.

  • d (int) – The derivative value.

Return type:

None

UiFlow2 Code Block:

set_pid_value.png

MicroPython Code Block:

unit_bldcdriver_0.set_pid_value(p, i, d)
set_pole_pairs(pole)

Set pole pairs.

Parameters:

pole (int) – pole pairs, range: 0~255.

Return type:

None

UiFlow2 Code Block:

set_pole_pairs.png

MicroPython Code Block:

unit_bldcdriver_0.set_pole_pairs(pole)
set_rpm_float(rpm)

Set the rpm in float.

Parameters:

rpm (float) – Revolutions per minute.

Return type:

None

UiFlow2 Code Block:

set_rpm_float.png

MicroPython Code Block:

unit_bldcdriver_0.set_rpm_float(rpm)
set_rpm_int(pwm)

Set the rpm in int.

Parameters:

rpm (int) – Revolutions per minute.

Return type:

None

UiFlow2 Code Block:

set_rpm_int.png

MicroPython Code Block:

unit_bldcdriver_0.set_rpm_int(rpm)