Subject: Issues with Stepper Motor Control Using M5Stack and Stepmotor Driver Module
Description:
Hello,
I am reaching out to seek your assistance with an issue I am experiencing with the M5Stack and the Stepmotor Driver Module. Here is a detailed description of the hardware and steps I have taken so far:
Hardware:
- M5Stack Core2
- Stepmotor Driver Module v1.1 (HR8825)
- NEMA 17 stepper motor
Issue Description:
I successfully tested the stepper motor using MicroPython, but due to the limited functionality and hidden features in the MicroPython library, I decided to switch to PlatformIO with Arduino. Unfortunately, I have been unable to get the example code from your GitHub repository (https://github.com/m5stack/M5Module-Stepmotor) to work as expected.
Steps and Code:
-
MicroPython Code (Working):
The MicroPython code initializes the stepper motor and successfully controls its movements.
Here is a snippet of the working MicroPython code:from m5stack import * import module Z = 2 stepmotor = module.get(module.STEPMOTORDRIVER) # Use single argument stepmotor.initDevice(0x27) stepmotor.setStepPulse(500, Z) stepmotor.setStepDir(Z, 0) # Loop to toggle motor on and off while True: stepmotor.enableMotor(0) time.sleep(100) stepmotor.enableMotor(1)
-
Arduino Code (Not Working):
I tried to follow the provided example for Arduino but without success. I extended the AccelStepper class to add the methods from the "Module_Stepmotor.h" library.I use the following pins for the Z motor:
#define STEP_PIN 15 #define DIR_PIN 0 #define MOTOR_ID 2
Here is my current implementation (with lvgl GUI):
// main.cpp #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include "lvgl_port_m5stack.hpp" #include "StepperMotor.h" #define STEP_PIN 15 #define DIR_PIN 0 #define MOTOR_ID 2 StepperMotor stepperMotor(STEP_PIN, DIR_PIN, MOTOR_ID); // Initialize stepper motor extern void user_app(void); M5GFX gfx; void setup(void) { Serial.begin(115200); gfx.init(); lvgl_port_init(gfx); stepperMotor.initMotor(); Serial.println("Setup done"); user_app(); } void loop(void) { delay(10); lv_timer_handler(); // Use lv_task_handler() if lv_timer_handler() is not available delay(5); // Adjust delay as needed if (stepperMotor.distanceToGo() != 0) { // Print current and target positions Serial.print("Current Position: "); Serial.print(stepperMotor.currentPosition()); Serial.print(", Target Position: "); Serial.println(stepperMotor.targetPosition()); // Execute the motor movement stepperMotor.run(); } }
// StepperMotor.cpp #include "StepperMotor.h" #include <Wire.h> StepperMotor::StepperMotor(int step_pin, int dir_pin, uint8_t motor_id) : AccelStepper(AccelStepper::DRIVER, step_pin, dir_pin), motorID(motor_id) { setCurrentPosition(0); setMaxSpeed(500); setAcceleration(500); } void StepperMotor::initMotor() { Wire.begin(21, 22, 400000UL); // Start I2C bus driver.init(Wire, 0x27); // Initialize the driver with the I2C bus and address 0x27 driver.resetMotor(motorID, 0); // Reset the motor setMicrostepResolution(Module_Stepmotor::kMicrosteps16); delay(50); driver.enableMotor(1); } void StepperMotor::enableMotor(bool enable) { driver.enableMotor(enable ? 1 : 0); } void StepperMotor::setMicrostepResolution(Module_Stepmotor::MicrostepResolution_t resolution) { driver.setMicrostepResolution(resolution); } void StepperMotor::resetMotor() { driver.resetMotor(motorID, 0); // Reset the motor }
Observed Behavior:
When using driver.resetMotor(motorID, 1)
, the motor makes a whining noise and is locked (cannot be turned by hand).
If I use driver.enableMotor(0)
, I still need to use driver.resetMotor(motorID, 0)
to unlock the motor.
The motor does not respond to movement commands as expected.
Request:
I couldn't find detailed documentation for the library. Could you please provide it? Do you see any errors in my code, or is the library incomplete? I would appreciate your assistance.
The fact that the MicroPython code works but the Arduino code does not raises questions for me. Why are the libraries hidden? If the implementation of the library in MicroPython were open, I could have examined it further.
Best regards,
Nicolas