Multiple stepper motors

Hey guys!

I have a question regarding programming stepper motors. I have a mechanical engineering background and am EXTREMELY new to this so I’m wondering if it would be possible for me to run 6 stepper motors simultaneously while adjusting their angles separately - they will all start running at the same time but some of the motors will step more times and go further than others to get to different positions. I have figured out how to make them move to different angular positions, but currently they do not run simultaneously. Please take a look at my code below. But please keep in mind that I have never dealt with programming before so you may have to really dumb down your explanation :slight_smile: Any help is appreciated! -Zey

#define BAUD (57600)
#define MAX_BUF (64)
#define MIN_STEP_DELAY (100)

struct Arm {
int motor_step_pin;
int motor_dir_pin;
int motor_enable_pin;
int limit_switch_pin;
};

//------------------------------------------------------------------------------
// GLOBALS
//------------------------------------------------------------------------------
Arm arms[6];

//------------------------------------------------------------------------------
// METHODS
//------------------------------------------------------------------------------

/**

  • First thing this machine does on startup. Runs only once.
    */
    void setup() {
    Serial.begin(BAUD); // open coms

arms[0].motor_step_pin=17;
arms[0].motor_dir_pin=16;
arms[0].motor_enable_pin=48;
arms[0].limit_switch_pin=37;

arms[1].motor_step_pin=54;
arms[1].motor_dir_pin=47;
arms[1].motor_enable_pin=55;
arms[1].limit_switch_pin=36;

arms[2].motor_step_pin=57;
arms[2].motor_dir_pin=56;
arms[2].motor_enable_pin=62;
arms[2].limit_switch_pin=35;

arms[3].motor_step_pin=23;
arms[3].motor_dir_pin=22;
arms[3].motor_enable_pin=27;
arms[3].limit_switch_pin=34;

arms[4].motor_step_pin=26;
arms[4].motor_dir_pin=25;
arms[4].motor_enable_pin=24;
arms[4].limit_switch_pin=33;

arms[5].motor_step_pin=29;
arms[5].motor_dir_pin=28;
arms[5].motor_enable_pin=39;
arms[5].limit_switch_pin=32;

int i;
for(i=0;i<6;++i) {
pinMode(arms*.motor_step_pin,OUTPUT);*
pinMode(arms*.motor_dir_pin,OUTPUT);
pinMode(arms.motor_enable_pin,OUTPUT);
pinMode(arms.limit_switch_pin,INPUT);
digitalWrite(arms.motor_enable_pin,LOW);
digitalWrite(arms.limit_switch_pin,HIGH);
_ }*_

_ Serial.println("** Ready ");
}
/

* After setup() this machine will repeat loop() forever.
/
void loop() {
int i=0;
for(i=0;i<6;++i) {_

move_motor(i, 90);
_ }
}
void move_motor(int i, int deg) {
int j;
int steps = map(deg, 0, 360, 0, 6400);*_

* Serial.print("Testing forward ");*
* Serial.println(i);*
digitalWrite(arms*.motor_dir_pin,HIGH);
_ for(j=0;j<16400;++j) {
digitalWrite(arms*.motor_step_pin,HIGH);
digitalWrite(arms.motor_step_pin,LOW);
delayMicroseconds(MIN_STEP_DELAY);*

* }
Serial.print("Testing backward ");
Serial.println(i);_

digitalWrite(arms.motor_dir_pin,LOW);
_ for(j=0;j<16400;++j) {
digitalWrite(arms*.motor_step_pin,HIGH);
digitalWrite(arms.motor_step_pin,LOW);
delayMicroseconds(MIN_STEP_DELAY);*

* }
}*_

If you're going to do something like that (e.g. - GrBL), you'd be FAR better off starting with some code that already does the motion control. Motion control is HARD, so why re-invent the wheel? Start with an existing motion control app, then you just have to write the code to tell it what to do and when. Depending on your application, that may be nothing more than writing some simple G-code, and feeding that to something like GrBL.

Regards, Ray L.

but currently they do not run simultaneously.

That is because your code moves one motor at a time, blocking until the motor gets to where you told it to go.

The AccelStepper library is able to make steppers move at the same time, because it never tries to make a motor step more than once, before giving the other motors time to move.

You'd need to reinvent that wheel. Or use that wheel...

zeyneptuzun: I have a question regarding programming stepper motors. I have a mechanical engineering background and am EXTREMELY new to this so I'm wondering if it would be possible for me to run 6 stepper motors simultaneously while adjusting their angles separately - they will all start running at the same time but some of the motors will step more times and go further than others to get to different positions. I have figured out how to make them move to different angular positions, but currently they do not run simultaneously

To make the motors move simultaneously you have to interleave the steps.

But before going any further I have a question. Do you need to move them in a closely synchronized way? For example motor A moves 173 steps in the same time that motor B move 874 steps? Or is it OK for them all to run at the same step interval so that motor A finishes long before motor B.

I have not used it myself but the MultiStepper version of the AccelStepper library is designed for synchronized movement. if you don't need synchronized movement then the AccelStepper library will be sufficient.

And because you are using stepper drivers that take step and direction signals it would be relatively easy to implement either system without using any library. If you are interested have a look at the second example in this Simple Stepper Code. It could easily be extended to deal with multiple motors. If you want to make the steppers move in a synchronized way you must figure out the appropriate interval between steps for each motor for any particular movement.

...R Stepper Motor Basics

Robin2: But before going any further I have a question. Do you need to move them in a closely synchronized way? For example motor A moves 173 steps in the same time that motor B move 874 steps? Or is it OK for them all to run at the same step interval so that motor A finishes long before motor B.

Thank you so much for the help! To answer your question, I need to move them in a synchronized way. To make things easier, I think I will have to worry about the number of steps later down the road. Like I said, I am very very new to this, so first, I think I should begin by moving the steppers the same amount of steps. As long as I can get them to run simultaneously, I think I will be able adjust the number of steps later.

PaulS: That is because your code moves one motor at a time, blocking until the motor gets to where you told it to go.

The AccelStepper library is able to make steppers move at the same time, because it never tries to make a motor step more than once, before giving the other motors time to move.

Yes, I realize that my code moves one motor at a time, that's why I started this thread lol I looked into the AccelStepper library, but it specifically says that it's adequate for simple, single motor applications. Would I be able to use it with multiple steppers?

I looked into the AccelStepper library, but it specifically says that it's adequate for simple, single motor applications.

I don't know where you read that, but, as Robin2 pointed out, it can control multiple steppers. What it can not do is make them move in sync. That is, it does not ensure that each stepper steps 1/nth of its commanded number of steps while taking turns.

The MultiStepper library corrects that oversight.

But one MAJOR disadvantage to Mulitstepper:

Caution: only constant speed stepper motion is supported: acceleration and deceleration is not supported All the steppers managed by MultiStepper will step at a constant speed to their target

In the real world, this means you will never get anywhere near the maximum speed of the motor, even under no-load conditions. Moving a real-world load, it will be MUCH worse.

Regards, Ray L.

RayLivingston: But one MAJOR disadvantage to Mulitstepper:

In the real world, this means you will never get anywhere near the maximum speed of the motor,

Indeed. But maybe they will move as fast as the OP requires?

I have a PC program that calculates the movements for 3 synchronized motors without acceleration. I can see how it would be complex to include acceleration.

...R