# Trying to understand AccelStepper constant speed

I'm starting to learn the AccelStepper library for controlling stepper motors for a project I'm working on. I'm able to get the stepper motors running with the basic examples that use acceleration.

I know for parts or all of project I'll want to use constant speed, so I popped up the constant speed demo: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library/blob/master/examples/Accel_ConstantSpeed/Accel_ConstantSpeed.ino

``````#include <Wire.h>
#include <AccelStepper.h>

void forwardstep1() {
myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, SINGLE);
}

AccelStepper Astepper1(forwardstep1, backwardstep1);

void setup()
{
Serial.begin(9600);
Serial.println("Stepper test!");

AFMS.begin();  // create with the default frequency 1.6KHz

Astepper1.setSpeed(50);
}

void loop()
{
Astepper1.runSpeed();
}
``````

(I'm using the adafruit stepper motor wing)

I uploaded it to my arduino and it's working, the shaft is rotating, but it's doing it really slow. I figured this made sense because it's set at a speed of 50, so I tried turning up the speed:

``````Astepper1.setSpeed(200);
``````

But the speed still seems to be the same speed. Considering this is a 200 step motor and `setSpeed` is steps per second you'd think this would whip the motor around fairly quick.

I'm positive that this is due to my misunderstanding of how the library works, but I'm having a hard time trying to figure out how to properly use the constant speed methods.

• please put a space before the `https://` in the URL ... without it, the link is not clickable Jul 13, 2020 at 23:27
• try the other example sketches Jul 13, 2020 at 23:33
• Hey @jsotola, I corrected the link. thanks for that catch. Also, I have tried the other demo sketches. The acceleration based demos work and even the StackingTest.ino demo works which doesn't use constant speed directly, but does individual stepping without acceleration and that works, but I still can't seem to figure out why the constant speed methods themselves do not work as expected. Jul 14, 2020 at 15:06
• I just realized that the Stacking test doesn't use the accelstepper library, so yeah, still at a loss :( Jul 14, 2020 at 15:08
• @jsotola, figured it out. See my answer below. Jul 14, 2020 at 17:25

So I ended up doing some source code diving for the AccelStepper library to figure out what the problem was and it turns out there was a missing method call in their example code, specifically a call to `setMaxSpeed` in the setup function:

``````void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");

AFMS.begin(); // create with the default frequency 1.6KHz

// ! Note that you need to set the max speed, otherwise the stepper motor will
// ! only step once per interval regardless of what `setSpeed` value you give
Astepper1.setMaxSpeed(1000);

Astepper1.moveTo(targetDistance);
Astepper1.setSpeed(targetSpeed);
}
``````

If you look at the `setSpeed` method in the library you'll notice a constraint of the speed value:

The `constrain` method takes in a speed value and then a low and high, which the accelstepper library sets to the private `_maxSpeed` variable. If the speed is outside of that range it will set the value to the low or high value passed in.

This means, if you don't call `setMaxSpeed` the library will use the default `_maxSpeed` value assigned during construction which issss `1.0`:

So, to correctly use the constant speed methods, you need to be sure to include a call to `setMaxSpeed` in your code before calling `setSpeed`, likely in the `setup` function.

I also shot a video explanation to share with my co-workers. You can see it here: https://vimeo.com/438272826