Controlling Multiple Stepper Motors at Different Speeds

Hi,

I am trying to control three different bipolar stepper motors (here are their specs: http://www.pololu.com/product/1473) at three different speeds using one Arduino Mega 2560 and three L298N chips. When I try to give them each a different speed, for instance 10, 20 and 30 rpm, they will all only run at the lowest speed option (10). I also seem to have a dead zone on one of my motors at 50 rpm. I am fairly inexperienced with Arduino, anything electrical, or anything programming, so I would greatly appreciate assistance (though I’ve been learning a ton through this project). I really need them to run at the different speeds I specify. Thank you in advance for any help!

All of my inputs are pulled high in the setup.
If this helps my void loop code is this (I know it prob will stop some hearts with the over complication &mess that it is, sorry.):

if (digitalRead(button5Pin) == LOW) //When button in Pin 8 is pressed motor turns on
{
while (digitalRead(button6Pin) == HIGH) //Stays on until button in Pin 7 is pressed
{

while (digitalRead(lever7Pin) == HIGH) //This is my switch so that the motor doesn’t run itself off the track
{
motortravel.setSpeed(w_tm); //in rpm
int stepstm = 1;
motortravel.step(stepstm);

motorspool.setSpeed(w_sm); //in rpm
int stepssm = -1;
motorspool.step(stepssm);

motorfly.setSpeed(w_fm); //in rpm
int stepsfm = 1;
motorfly.step(stepsfm);

if(digitalRead(button6Pin) == LOW) //if the stop button is pressed, breaks the loop
{
break;

}
}
if (digitalRead(lever7Pin) == LOW) //when this lever is pressed the traveler changes direction and speed, spooler changes speed, flywheel stays same
{

n=n+1;
w_sm = ((N_bc)/N_s)((30/3.14159)/(r_bs+nT_f));
w_tm = (W_f
c/(23.14159r_s))((30/3.14159)/(r_bs+nT_f));

motortravel.setSpeed(w_tm);
int stepstm = -1;
motortravel.step(stepstm);

motorspool.setSpeed(w_sm);
int stepssm = -1;
motorspool.step(stepssm);

motorfly.setSpeed(w_fm);
int stepsfm = 1;
motorfly.step(stepsfm);

digitalWrite(lever7Pin, HIGH);

if(digitalRead(button6Pin) == LOW)
{
break;

}
}
while (digitalRead(lever8Pin) == HIGH) //This is the second switch
{

motortravel.setSpeed(w_tm);
int stepstm = -1;
motortravel.step(stepstm);

motorspool.setSpeed(w_sm);
int stepssm = -1;
motorspool.step(stepssm);

motorfly.setSpeed(w_fm);
int stepsfm = 1;
motorfly.step(stepsfm);

if(digitalRead(button6Pin) == LOW)
{
break;

}
}
if (digitalRead(lever8Pin) == LOW)
{

n=n+1;
w_sm = ((N_bc)/N_s)((30/3.14159)/(r_bs+nT_f));
w_tm = (W_f
c/(23.14159r_s))((30/3.14159)/(r_bs+nT_f));

motortravel.setSpeed(w_tm);
int stepstm = 1;
motortravel.step(stepstm);

motorspool.setSpeed(w_sm);
int stepssm = -1;
motorspool.step(stepssm);

motorfly.setSpeed(w_fm);
int stepsfm = 1;
motorfly.step(stepsfm);

digitalWrite(lever8Pin, HIGH);

if(digitalRead(button6Pin) == LOW)
{
break;

}
}
}
}

Thanks for reading :slight_smile:

Probably simplest to start using three instances of the AccelStepper library?

Or you could implement a DDA approach (bressenham's line algorithm in 3 dimensions) but that's probably a lot more complex to do.

Or there's DDS step generation using 3 phase-accumulators and a timer interrupt.

Thank you! I am looking into the AccelStepper library now. I'm having some trouble understanding how the library works. Does it need me to declare the number of steps/rev like the Stepper library does? (For instance, my motors are 200 steps/rev so: AccelStepper motorspool(200, in1Pin, in2Pin, in3Pin, in4Pin); ). Should I still be telling it how many steps to take because when I have this code in there: motorspool.step(1);, the program complains. I am searching databases and examples for assistance with these though I think my main problem is just that I don't understand the library itself (yet! I will learn!!).

I am also wondering why the Stepper library cannot support multiple motors' speeds, but the accelstepper library can (since I don't want my motors to accelerate); I only want them to change speed at given intervals. Thanks again. Sorry if these are second grade questions in programming.

The L298 is a very poor choice for driving those stepper motors. They really need to be driven with a proper stepper motor driver that can take a high voltage supply (20v to 60v) an can control the current in the motor so that the high voltage does not overload the motor.

I suspect your experience of the motors only working at low speed and having a dead zone has a lot to do with the inadequate drivers.

And the simple answer to your question is that the Accelstepper library is much better than the Stepper library. There is good documentation for the Accelstepper library here.

I assume, since you haven't mentioned it, that you have no need to precisely synchronize the movements of your motors.

...R

I actually do need to synchronize the motors' movements (if you mean that I need them to move simultaneously, not the same direction or speed though). Would the drivers really make it so that they only accepted the lowest speed? My power supply is 13V and the L298N drivers are rated for up to 46V of supply voltage. My arduino board is powered by the same source, but using a homemade 5V regulator. I also have heat sinks hooked up to the chips and a fan on them. Thank you for all the help!

Would the drivers really make it so that they only accepted the lowest speed?

Yes because it was never designed to control more than one motor so when you try and drive more than one it does not work correctly.

L298N drivers are rated for up to 46V of supply voltage

That doesn't stop it being very poor for what you want. You want a current chopping regulating driver with a step and direction interface. Something like this:- http://www.pololu.com/category/120/stepper-motor-drivers

Oh! I’m sorry, I wasn’t very clear. I have three L298N chips, each hooked up to a different motor. Now all three chips are hooked up to one arduino mega though…Thanks! I’m looking into the driver.

pixiexandria: Oh! I'm sorry, I wasn't very clear. I have three L298N chips, each hooked up to a different motor. Now all three chips are hooked up to one arduino mega though

Yes that is what I thought you had.

This is my CNC project using 3 stepping motors all capable of moving at the same time at different speeds. http://www.thebox.myzen.co.uk/Hardware/CNC_Conversion.html

The AccelStepper library is non-blocking, you just add a call to run() in loop() for each active instance.

Perhaps you should look at the MultiStepper example that comes with the AccelStepper library? It has three steppers running.

pixiexandria: My power supply is 13V and the L298N drivers are rated for up to 46V of supply voltage.

STOP a moment.

Those are 3.6v motors. You can't power them through an L298 using a 13v supply. The smoke will escape somewhere.

...R

If you're driving a leadscrew system like that resonance can be a big issue, which more or less mandates microstepping. Mechanical damping will help too.

You need to rethink - chopper drives from 24V will give you much faster rapids anyway. See how it goes with L298 but use a 6V 5A supply, 13V will burn everything up. You will need aggressive cooling on the L298's.