🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Issues with Stepmotor Driver Module v1.1 (HR8825) Using M5Stack

    Modules
    4
    5
    936
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      NicoDess
      last edited by

      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:

      1. 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)
        
      2. 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

      1 Reply Last reply Reply Quote 0
      • P
        pntnut
        last edited by

        I have exactly the same problem. Were you able to figure it out?

        1 Reply Last reply Reply Quote 0
        • P
          pntnut
          last edited by

          Another thing I just realized: The Arduino-example code is for the old module (M039, not M039-V11).

          S 1 Reply Last reply Reply Quote 0
          • S
            Sean xiang @pntnut
            last edited by

            @pntnut Hi, the M039 and M039-V11 registers are compatible(all most), so you can still use the M039 lib to drive the StepMotor Driver 1.1 module

            https://github.com/m5stack/M5Module-Stepmotor

            1 Reply Last reply Reply Quote 0
            • V
              vuhn88
              last edited by vuhn88

              Kind of an old thread but I wanted to respond since it may help others who are trying to get this module to work, not much out there on the Arduino coding side.

              I believe the issue might be the pin assignment for step and dir, it's actually printed on the PCB. For the Core2 X, Y, Z step = 13, 27, 2 and dir = 14, 19, 0 respectively. So I'm not sure why STEP_PIN is set to 15, maybe this is the legacy Core?

              I used the same referenced Stepmotor code (https://github.com/m5stack/M5Module-Stepmotor) and did get it working eventually. Here is some very basic example code that may help:

              #include <M5Unified.h>
              #include "Module_Stepmotor.h"
              
              // change to CORE2 if using Core2
              #define CORE_S3
              
              #ifdef CORE_S3
                  static const int X_STEP_PIN = 18;
                  static const int X_DIR_PIN = 17;
                  static const int Y_STEP_PIN = 6;
                  static const int Y_DIR_PIN = 7;
                  static const int Z_STEP_PIN = 13;
                  static const int Z_DIR_PIN = 0;
                  
                  static const int SDA_PIN = 12;
                  static const int SCL_PIN = 11;
              #elif defined CORE2
                  static const int X_STEP_PIN = 13;
                  static const int X_DIR_PIN = 14;
                  static const int Y_STEP_PIN = 27;
                  static const int Y_DIR_PIN = 19;
                  static const int Z_STEP_PIN = 2; // Not sure about this one
                  static const int Z_DIR_PIN = 0;
              
                  static const int SDA_PIN = 21;
                  static const int SCL_PIN = 22;
              #endif
              
              static Module_Stepmotor driver;
              
              void setup() {
                  Wire.begin(SDA_PIN, SCL_PIN, 400000UL);
              
                  driver.init(Wire, 0x27);
                  driver.resetMotor(0, 0);
                  driver.resetMotor(1, 0);
                  driver.resetMotor(2, 0);
                  driver.enableMotor(1);
              
                  // PWM, sets the speed of the stepper motor, adjust accordingly
                  ledcSetup(0, 500, 8);
              
                  // XYZ step
                  ledcAttachPin(X_STEP_PIN, 0);
                  ledcAttachPin(Y_STEP_PIN, 0);
                  ledcAttachPin(Z_STEP_PIN, 0);
              
                  ledcWrite(0, 127);
              
                  // XYZ dir
                  pinMode(X_DIR_PIN, OUTPUT);
                  digitalWrite(X_DIR_PIN, 1);
                  pinMode(Y_DIR_PIN, OUTPUT);
                  digitalWrite(Y_DIR_PIN, 1);
                  pinMode(Z_DIR_PIN, OUTPUT);
                  digitalWrite(Z_DIR_PIN, 1);
              }
              
              void loop() {
                  digitalWrite(X_DIR_PIN, 0);
                  delay(2000);
                  digitalWrite(X_DIR_PIN, 1);
                  delay(2000);
              }
              
              1 Reply Last reply Reply Quote 0
              • First post
                Last post