Running 4 Stepper Motors At The Same TIme But Different Speed

Hi, I am currently working on a 6 DOF (Degrees Of Freedom) Robotic Arm project. I am using stepper motors at the joints. I want to move my motors in a coordinated fashion but I can't seem to find a way to do that. I am using DRV8825 stepper motor drivers and an Arduino Mega 2560. I googled up for coordinated movement of stepper motors, found nothing but a video that uses Timer Interrupts to move the motors. I also looked in AccelStepper Library but that uses AF motor shield and also utilizes 4 pins so that's, somehow, not feasible for me. Can anybody in here help me with this so I can control all my stepper in coordinated manner using DRV8825 Stepper Driver? I'll be really grateful!

I also looked in AccelStepper Library but that uses AF motor shield and also utilizes 4 pins so that's, somehow, not feasible for me.

No, the AccelStepper library is completely hardware agnostic and is the place to start. Use the DRIVER mode for step+direction stepper drivers such as the DRV8825

The AccelStepper library can directly control the DRV8825 drivers however it is NOT intended for co-ordinated control of motors if that means (for example) that motorA should move 237 steps in the same time that motorB moves 332 steps.

The MultiStepper library is intended for coordinated movements but it does not use acceleration.

To be honest with a DRV8825 driver it is not difficult to control a motor without any library and it is not much more difficult to have co-ordinated movements with acceleration.

Where the code might get a little more complex is if you want a very large number of steps per second - and the AccelStepper library is not good at that either.

...R Stepper Motor Basics Simple Stepper Code

Hi, I have written the following sketch to control 2 stepper motors that start and stop at the same time and move same number of steps. i.e. 200.

#define X_DIR_PIN          55
#define X_STEP_PIN         54
#define X_ENABLE_PIN       38

#define Y_DIR_PIN          61
#define Y_STEP_PIN         60
#define Y_ENABLE_PIN       56

int steps = 200;

void setup() {
  pinMode(X_DIR_PIN,   OUTPUT);
  pinMode(X_STEP_PIN,    OUTPUT);
  pinMode(X_ENABLE_PIN, OUTPUT);
  pinMode(Y_DIR_PIN,   OUTPUT);
  pinMode(Y_STEP_PIN,    OUTPUT);
  pinMode(Y_ENABLE_PIN, OUTPUT);
}

void simpleMove(int steps) {
  int interval = 2;
  for (int i = 0; i < steps; i++){
      digitalWrite(X_STEP_PIN, HIGH);
      digitalWrite(X_STEP_PIN, LOW);
      digitalWrite(Y_STEP_PIN, HIGH);
      digitalWrite(Y_STEP_PIN, LOW);
      delay(interval);
}
}
void loop() {
  
  digitalWrite(X_DIR_PIN, LOW);
  digitalWrite(Y_DIR_PIN, LOW);
  simpleMove(steps);
  delay(1000);
  digitalWrite(X_DIR_PIN, HIGH);
  digitalWrite(Y_DIR_PIN, HIGH);
  simpleMove(steps);
  delay(1000);

}

I am looking for a solution in which motor X moves 400 steps and motor Y moves 200 steps but they still start and stop at the same time. Any kind of help will be immensely appreciated. Thanks Arduino Community!

Robin2:
The MultiStepper library is intended for coordinated movements but it does not use acceleration.

Robin2, Acceleration is not a problem for me. Can you please tell me how do I use my direction and step pins (Which I am going to use to because I am using DRV8825) after including MultiStepper library.

sarmadgulzar:

define Y_DIR_PIN 61

define Y_STEP_PIN 60

define Y_ENABLE_PIN 56

Hello there!

What board are you using such that these pins are accessible?

Time to upgrade to the AccelStepper library.

Why have you started a new Thread about the same problem.

I am suggesting to the Moderator to merge them.

…R

I will wait to see if your new Thread is merged with this one.

...R

sarmadgulzar: Hi, I have written the following sketch to control 2 stepper motors that start and stop at the same time and move same number of steps. i.e. 200.

You are pretty close to your goal.

Instead of using your function simpleMove() controlling both motors create a separate function for each motor that moves it a single step and counts down the number of steps for that motor - something like this

void stepMotorX() {
   if (motorXsteps > 0) {
      digitalWrite(X_STEP_PIN, HIGH);
      digitalWrite(X_STEP_PIN, LOW);
      motorXsteps --;
   }
}

and in loop() have something like

void loop() {
   stepMotorX();
   stepMotorY();
   stepMotorZ();
   // etc
}

...R

bos1714: What board are you using such that these pins are accessible?

I am using RAMPS Board v1.4

Robin2: You are pretty close to your goal.

Instead of using your function simpleMove() controlling both motors create a separate function for each motor that moves it a single step and counts down the number of steps for that motor - something like this

void stepMotorX() {
   if (motorXsteps > 0) {
      digitalWrite(X_STEP_PIN, HIGH);
      digitalWrite(X_STEP_PIN, LOW);
      motorXsteps --;
   }
}

and in loop() have something like

void loop() {
   stepMotorX();
   stepMotorY();
   stepMotorZ();
   // etc
}

...R

Hi Robin, I implemented this as:

void stepMotorX(){
  if (motorXsteps > 0){
    digitalWrite(X_STEP_PIN, HIGH);
    digitalWrite(X_STEP_PIN, LOW);
    delay(interval);
    motorXsteps--;
  }
}

void stepMotorY(){
  if (motorYsteps > 0){
    digitalWrite(Y_STEP_PIN, HIGH);
    digitalWrite(Y_STEP_PIN, LOW);
    delay(interval);
    motorYsteps--;
  }
}

void loop() {
  
  digitalWrite(X_DIR_PIN, LOW);
  digitalWrite(Y_DIR_PIN, LOW);
  stepMotorX();
  stepMotorY();
}

but have a look at this image:

they move together but do not stop at the same time which is my requirement.

MorganS: Time to upgrade to the AccelStepper library.

Hi Morgan, thanks. But could you please tell me how do I use my DRV8825 with AccelStepper Library.

sarmadgulzar: they move together but do not stop at the same time which is my requirement.

There is another current Thread involving multiple steppers that does not require them to stop at the same time so my brain is a bit fuddled - sorry.

I have implemented a system for my own use in which the steppers move the way you want - start and stop at the same time but move different numbers of steps.

It does that by figuring out the total-time needed for the motor that needs to move the greatest number of steps. Then for each of the other motors it calculates a step interval that is the total-time / number-of-steps. You should probably measure the time using micros() so that you can get fractions of a millisec.

For my own project these calculations are done on my PC rather than on my Arduino. The PC sends to the Arduino 4 numbers - the total-time in microsecs and the step-interval for each of the 3 motors.

...R

Robin2: There is another current Thread involving multiple steppers that does not require them to stop at the same time so my brain is a bit fuddled - sorry.

I have implemented a system for my own use in which the steppers move the way you want - start and stop at the same time but move different numbers of steps.

It does that by figuring out the total-time needed for the motor that needs to move the greatest number of steps. Then for each of the other motors it calculates a step interval that is the total-time / number-of-steps. You should probably measure the time using micros() so that you can get fractions of a millisec.

For my own project these calculations are done on my PC rather than on my Arduino. The PC sends to the Arduino 4 numbers - the total-time in microsecs and the step-interval for each of the 3 motors.

...R

Can you share your code?

Also do you think ESP32 is powerful enough to run calculation fast enough? taking account of CPU power and multithreading?

Thanks!

danielt514: Can you share your code?

Sorry, no. The calculations are done in a Python program and I wrote it a long time ago.

Also do you think ESP32 is powerful enough to run calculation fast enough? taking account of CPU power and multithreading?

As many 3D printers function very effectively with a 16MHz Arduino the answer is probably "yes" - but I have no idea whether the ESP32's WiFi stuff would get in the way - perhaps it can be disabled if not needed.

...R