DCMotor Module
This library is the driver for Module DCMotor, and the module communicates via I2C.
Support the following products:
UiFlow2 Example
Speed Control
Open the cores3_dc_motor_module_speed_control.m5f2 project in UiFlow2.
This example demonstrates the use of the DCMotor Module to control the speed of a DC motor and display the motor’s encoder value in real-time. The program automatically adjusts the motor speed, gradually increasing or decreasing the speed until it reaches the maximum or minimum value, then reverses the direction.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
Speed Control
This example demonstrates the use of the DCMotor Module to control the speed of a DC motor and display the motor’s encoder value in real-time. The program automatically adjusts the motor speed, gradually increasing or decreasing the speed until it reaches the maximum or minimum value, then reverses the 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 module import DCMotorModule 9import time 10 11 12title0 = None 13label_speed = None 14label_speed_val = None 15label_encoder = None 16label_encoder_value = None 17module_dcmotor_0 = None 18last_time = None 19direction = None 20speed = None 21encoder_value = None 22 23 24def setup(): 25 global \ 26 title0, \ 27 label_speed, \ 28 label_speed_val, \ 29 label_encoder, \ 30 label_encoder_value, \ 31 module_dcmotor_0, \ 32 last_time, \ 33 direction, \ 34 speed, \ 35 encoder_value 36 37 M5.begin() 38 Widgets.fillScreen(0x222222) 39 title0 = Widgets.Title("DCMotor Control", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 40 label_speed = Widgets.Label("Speed:", 5, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 41 label_speed_val = Widgets.Label("0", 105, 60, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24) 42 label_encoder = Widgets.Label( 43 "Encoder Value:", 5, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 44 ) 45 label_encoder_value = Widgets.Label( 46 "0", 203, 100, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 47 ) 48 module_dcmotor_0 = DCMotorModule() 49 module_dcmotor_0.clear_encoder(1) 50 direction = True 51 speed = 0 52 53 54def loop(): 55 global \ 56 title0, \ 57 label_speed, \ 58 label_speed_val, \ 59 label_encoder, \ 60 label_encoder_value, \ 61 module_dcmotor_0, \ 62 last_time, \ 63 direction, \ 64 speed, \ 65 encoder_value 66 M5.update() 67 if (time.ticks_diff((time.ticks_ms()), last_time)) > 100: 68 last_time = time.ticks_ms() 69 if direction: 70 speed = speed + 5 71 if speed >= 255: 72 direction = False 73 else: 74 speed = speed - 5 75 if speed <= -255: 76 direction = True 77 module_dcmotor_0.set_motor_speed(1, speed) 78 label_speed_val.setText(str(speed)) 79 encoder_value = module_dcmotor_0.get_encoder(1) 80 label_encoder_value.setText(str(encoder_value)) 81 module_dcmotor_0.clear_encoder(1) 82 83 84if __name__ == "__main__": 85 try: 86 setup() 87 while True: 88 loop() 89 except (Exception, KeyboardInterrupt) as e: 90 try: 91 from utility import print_error_msg 92 93 print_error_msg(e) 94 except ImportError: 95 print("please update to latest firmware")
Example output:
None
API
DCMotorModule
- class module.dc_motor.DCMotorModule
Bases:
objectCreate an DCMotorModule object.
UiFlow2 Code Block:

MicroPython Code Block:
from module import DCMotorModule dcmotor_module = DCMotorModule()
- set_motor_speed(id, speed)
Set speed of motor.
- Parameters:
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
dcmotor_module.set_motor_speed(id, speed)
- set_motor_speed_percent(id, percent)
Set motor speed as a percentage.
- Parameters:
- Return type:
None
UiFlow2 Code Block:

MicroPython Code Block:
dcmotor_module.set_motor_speed_percent(id, percent)
- get_encoder(id)
Get encoder count.
UiFlow2 Code Block:

MicroPython Code Block:
dcmotor_module.get_encoder()


