A driver library and expansion board for controlling a 5-wire 4-phase stepper motor via the I2C interface. #17083
leezisheng
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The Bus Unipolar Stepper Motor Expansion Board is a module designed to control multiple unipolar stepper motors (e.g., 5-wire 4-phase stepper motors like 28BYJ48, 35BYJ46, 35BY412L) via the I2C serial communication interface. Key features include:
This expansion board combines a compact footprint (directly attachable to XIAO mainboards or standalone use) with ULN2003 drivers (500mA/channel, 2.5A total) for 5-wire 4-phase steppers (5–24V input). Dual solid capacitors ensure stable power, while reverse-polarity protection and visual status LEDs enhance reliability in stacked configurations.
We provide MicroPython sample code with robust error handling for multi-motor independent control.
Bus Stepper Motor Driver
Main Features
micropython.native
decorator to improve execution efficiency of critical functions.File Description
Core Software Design Concept
By integrating the PCA9685 PWM control chip with the RZ7899 motor driver chip, we can control DC motors. The program communicates with the PCA9685 via I²C to generate PWM signals, controlling motor speed, direction. The classes
PCA9685
andBusDCMotor
are designed:PCA9685
class is responsible for controlling the generation of waveform frequency and duty cycle for different PWM channels.BusDCMotor
class encapsulates the control of DC motors, adjusting speed and direction using PWM duty cycles.pca9685.py
This file implements control over the PCA9685 chip, allowing for the setting of PWM frequency, controlling the duty cycle of PWM waveforms, and driving servos or other devices using PWM signals.
Main Class
PCA9685:
This class provides control over the PCA9685 chip, enabling the setting and retrieval of PWM frequencies and controlling the duty cycle of PWM waveforms.
Methods:
reset()
: Resets the PCA9685 module, restoring default settings.freq(freq: float)
: Sets or retrieves the PWM frequency of the PCA9685.pwm(index: int, on: int, off: int)
: Sets or retrieves the PWM signal for the specified channel.duty(index: int, value: int, invert: bool = False)
: Sets the duty cycle for the specified channel and provides an option to invert the signal.bus_step_motor.py
This file controls DC motors. The PWM signals generated by the PCA9685 chip allow for control of the speed and direction of up to 4 DC motors.
Main Class
BusStepMotor:
Controls 5-wire, 4-phase stepper motors (such as 28BYJ-48), supporting three drive modes and two motion modes.
Methods:
__init__(pca9685: PCA9685, motor_count: int = 2)
: Initializes the stepper motor control class._next_step(motor_id: int)
: Computes and updates the stepper motor state for the specified motor._start_timer(motor_id: int, speed: int)
: Starts a timer and sets a callback function to control continuous motor motion._stop_timer(motor_id: int)
: Stops the timer for the specified motor, stopping its motion.start_continuous_motion(motor_id: int, direction: int, driver_mode: int, speed: int)
: Starts continuous motion for a motor.stop_continuous_motion(motor_id: int)
: Stops the continuous motion of a motor.start_step_motion(motor_id: int, direction: int, driver_mode: int, speed: int, steps: int)
: Starts step motion, rotating a motor for a specified number of steps.Attributes:
pca9685 (PCA9685)
: Instance of the PCA9685 chip that controls PWM signal output.motor_count (int)
: Number of stepper motors controlled, with a maximum of 4.timers (list)
: List storing the timers for each motor.steps (list)
: List storing the target steps for each motor.step_counters (list)
: List storing the current step counters for each motor.directions (list)
: List storing the movement direction of each motor (forward or backward).driver_modes (list)
: List storing the drive mode of each motor (single-phase, dual-phase, or half-step drive).speeds (list)
: List storing the speed of each motor (controlling the pulse frequency of the driver output).Class Variables:
DRIVER_MODE_SINGLE (int)
: Constant value for the single-phase drive mode.DRIVER_MODE_DOUBLE (int)
: Constant value for the dual-phase drive mode.DRIVER_MODE_HALF_STEP (int)
: Constant value for the half-step drive mode.FORWARD (int)
: Constant value for the forward direction of the stepper motor.BACKWARD (int)
: Constant value for the backward direction of the stepper motor.PHASES (dict)
: A dictionary containing phase sequences for different drive modes.The channel drive sequence under different drive modes is shown in the table below:
[1, 0, 0, 0]
[1, 1, 0, 0]
[1, 0, 0, 0]
Dual:★★★
Half:★☆☆
[0, 1, 0, 0]
[0, 1, 1, 0]
[1, 0, 1, 0]
Dual:★★☆
Half:★★★
[0, 0, 1, 0]
[0, 0, 1, 1]
[0, 0, 1, 0]
Dual:★★★
Half:★☆☆
[0, 0, 0, 1]
[1, 0, 0, 1]
[0, 0, 1, 1]
Dual:★★☆
Half:★★★
[0, 0, 0, 1]
[0, 1, 0, 1]
[0, 1, 0, 0]
[1, 1, 0, 0]
Phase Notation:
1
: Active0
: InactiveTorque Levels:
Key Features
How to Use
Install Dependencies
Before running the example program, ensure that your environment has the necessary modules like
machine
andtime
. You can install dependencies via the MicroPython package manager.Save the two files to your main folder and import them as follows:
Usage Example
Notes
Conclusion
This example program provides a basic framework for driving unipolar stepper motors using the I2C bus. You can adjust motor control parameters as needed to develop more complex control logic.
Contact the Developer
License
This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License (CC BY-NC 4.0).
This project includes code originally licensed under the MIT License (for pca9685.py) and modified code licensed under the Creative Commons Attribution-NonCommercial 4.0 International License (CC BY-NC 4.0) (for bus_dc_motor.py).
MIT License:
CC BY-NC 4.0 License:
Beta Was this translation helpful? Give feedback.
All reactions