Run 2 Stepper Motors at once

I’ve read you cant run steppers at the same time, but you can make them seem to run at the same time.

I have the a4988 driver and can run the stepper motors back and forth separately, but really looking to run them both together. below is my code, just not sure how to do that.

#define enable 8
#define a_xDir 5
#define a_xStep 2

#define b_xDir 6
#define b_xStep 3

int steps = 20000; //Total Steps to move
int stepDelay = 35; //Delay between each pause

void step(boolean dir, byte dirPin, byte stepperPin, int steps)
{
  digitalWrite(dirPin, dir);
  for(int i = 0; i < steps; i++){
    digitalWrite(stepperPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepperPin, LOW);
    delayMicroseconds(stepDelay);
  }
}

void setup() {
  // put your setup code here, to run once:
 pinMode(a_xDir, OUTPUT);
 pinMode(a_xStep,OUTPUT);
 pinMode(b_xDir, OUTPUT);
 pinMode(b_xStep,OUTPUT);
 
 pinMode(enable,OUTPUT);
 digitalWrite(enable, LOW);
}

void loop() {


  step(false,a_xDir,a_xStep,10000); //x axis rotate clockwise
 // delay(100);
  step(true,a_xDir,a_xStep,10000);
//  delay(100);

  step(false,b_xDir,b_xStep,10000); //x axis rotate clockwise
 // delay(100);
  step(true,b_xDir,b_xStep,10000);
 // delay(100);

}

Ah figured this out, hope this helps anyone else.

void loop() {

for(int s=0; s < 10000; s++)
{
step(true,a_xDir,a_xStep,1);
step(true,b_xDir,b_xStep,1);
}

}

You can run 2 steppers off one A4988 driver. They can be both in the same direction or opposite each other depending on the way you connect the motors to the driver.

You can also change your step routine to step both motors (as you just discovered). Toggling two step pins would happen so fast they would appear to be running synchronously.

joeafox:
I’ve read you cant run steppers at the same time, but you can make them seem to run at the same time.

The person who wrote that must never have heard of CNC machining or 3D printing :slight_smile:

Your code using two delayMicroseconds()s for each step is not ideal if you want to control two motors at the same time.

  for(int i = 0; i < steps; i++){
    digitalWrite(stepperPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepperPin, LOW);
    delayMicroseconds(stepDelay);
  }

The actual step pulse to make a motor move can be very short (10 microsecs will be ample) and then speed of the motor can be governed by the interval between steps and that should NOT be part of the code for an individual step as it would mean that a step for the second motor could not begin until the time-between-steps for the first motor had concluded.

Have a look at the second example in this Simple Stepper Code

…R
Stepper Motor Basics