Accelstepper: Speed control

I'm running a NEMA 34 (6A, 800-40,000 steps/rev) with a 2HSS86H motor driver using Accelstepper.
I'm super new to Arduino and steppers, but I got it working by using a sketch from a Jeremy Fielding video. However, the speed of the motor topped out around 6 rpms. I thought this was weird, so I tried using a sketch from a Dronebot Workshop video and the motor ran much faster (around 300 rpm). I had the driver set to 800 steps/rev while running both sketches.

No matter how I change the setSpeed and setMaxSpeed values, I can't get the motor to run faster than 6 rpms using Accelstepper.

Any idea why?

Here's the sketch from the Jeremy Fielding video, using Accelstepper:

#include <AccelStepper.h>

int Stepper1Pulse = 5;
int Stepper1Direction = 6;
int speedpot = A0;

int Motor1speed = 0;
int speedmin = 0;
int speedmax = 4000;
AccelStepper step1(1, Stepper1Pulse, Stepper1Direction);

void setup() {
step1.setMaxSpeed (speedmax);
step1.setSpeed(0);
step1.setAcceleration(50);
pinMode(Stepper1Pulse, OUTPUT);
pinMode(Stepper1Direction, OUTPUT);
digitalWrite(Stepper1Pulse, LOW);
digitalWrite(Stepper1Direction, LOW);
Serial.begin(9600);
Serial.println("Running: StepperDriverTest");
}

void loop() {

Motor1speed = map((analogRead(speedpot)), 0, 1023, speedmin, speedmax);
step1.setSpeed(Motor1speed);
Serial.println(Motor1speed);
step1.runSpeed();

}

And here's the sketch from the Dronebot Workshop video, without Accelstepper:

int reverseSwitch = 4;
int driverPUL = 5;
int driverDIR = 6;
int spd = A0;

int pd = 500;
boolean setdir = LOW;

void revmotor(){

setdir = !setdir;
}

void setup() {
Serial.begin(9600);
Serial.println("Running: StepperSpeedPot");

pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
attachInterrupt(digitalPinToInterrupt(reverseSwitch), revmotor, FALLING);

}

void loop() {
pd = map((analogRead(spd)), 0, 1023, 2000, 50);
digitalWrite(driverDIR, setdir);
digitalWrite(driverPUL, HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL, LOW);
delayMicroseconds(pd);

}

Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

void loop() {
Motor1speed = map((analogRead(speedpot)), 0, 1023, speedmin, speedmax);
step1.setSpeed(Motor1speed);
Serial.println(Motor1speed);
step1.runSpeed();
}

Checking the analog input and printing every time through loop is like adding a 1 to 4 ms delay(). You really do not need to do that cause you can only move the pot so fast. Here I read the pot and print 10 times a second, but call runSpeed() every time through loop(). Note that I changed the pin numbers so I could test on my setup and changed the baud rate to a faster rate. You will need to change the code for your pin numbers.

#include <AccelStepper.h>

int Stepper1Pulse = 2;  // **** for CNC shield
int Stepper1Direction = 5;  // **** for CNC shield
const byte enablePin = 8;   // **** for CNC shield
int speedpot = A0;

int Motor1speed = 0;
int speedmin = 0;
int speedmax = 4000;
AccelStepper step1(1, Stepper1Pulse, Stepper1Direction);

void setup()
{
   step1.setMaxSpeed (speedmax);
   step1.setSpeed(0);
   step1.setAcceleration(500);
   pinMode(enablePin, OUTPUT);  // **** for CNC shield
   digitalWrite(enablePin, LOW);   // **** for CNC shield
   digitalWrite(Stepper1Direction, LOW); // CCW
   Serial.begin(115200); // ************** faster baud rate
   Serial.println("Running: StepperDriverTest");
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();

      Motor1speed = map((analogRead(speedpot)), 0, 1023, speedmin, speedmax);
      step1.setSpeed(Motor1speed);
      Serial.println(Motor1speed);
   }
      step1.runSpeed();
}
2 Likes

Thanks for the reply! The code you included explains demonstrates what you're saying perfectly!

I included the Serial.print for troubleshooting, not realizing it would slow down the pulses to the stepper. From what I understand, that extra 1-4 ms you mentioned is the main thing slowing down my motor, correct? Or was there something else also slowing down the motor in the first sketch (Jeremy Fielding)?

Thanks again!

Yes, mostly the serial prints. If you want to see the impact, use your original code unchanged except set the serial baud rate to 115200. You will see a significant increase in speed just with that.

The analogRead() function takes about 100us so that has less of an impact. But there is no reason to sample at full loop() speed so do so at 10 times a second will also increase the stepper speed.

edit: Some quick and dirty measurements with an opto sensor to measure RPM. Pot set to fastest speed.
original code at 9600 with print and analog read every loop() >>>> 12 RPM
original code at 115200 with print and analog read every loop() >>> 150 RPM (baud change only)
modified code at 115200 print every 100ms, analogRead() every loop() >>> 190 RPM
modified code at 115200 print and analog read every 100ms >>> 280 RPM