AccelStepper runs the motor non stop

I have wired up a stepper motor using a DRV8255 driver and the AccelStepper library. I can’t get it to function as I believe it should, the problem being that the motor never stops.

Direction and Step are on Pins 2 and 3 respectively, Not enable and not sleep are both tied high. It doesn’t matter if I use move or moveTo the motor still runs permanently.

Finally, a negative integer in the setSpeed results in it spinning the other way, a negative integer in the Move or MoveTo makes no difference to the direction of rotation and I understand that it should.

Any ideas what I am missing or not understanding?

// Include the AccelStepper library:
#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
  // Set the maximum speed in steps per second:
  stepper1.setMaxSpeed(1000);
  stepper1.setAcceleration(150);
}

void loop() {
  // Set the speed in steps per second:

  stepper1.setSpeed(600);
  stepper1.move(100);
  stepper1.run();
}

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose “copy for forum”
  3. paste clipboard into write-window of a posting

regarrding your code
loop does what its name says loop is looping infinitely

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Hi Stefan and thanks for the reply.

I have updated the code pasting as you requested.

I appreciate move will indeed loop forever but moveTo is an absolute and should stop when it reaches the desired position so I am somewhat confused why it does not. I will read the items you suggested.

Thanks again.

the code you have posted uses function move not function moveTo
best regards Stefan

Remove the setSpeed from loop(). That puts it in constant speed mode. Use the setMaxSpeed to control speed. You can change that (and acceleration) at any time.

#include <AccelStepper.h>
// changed dir and step pins to suit my setup
#define dirPin 5
#define stepPin 2
#define motorInterfaceType 1
// added enable pin for CNC shield
const byte en = 8;

// Create a new instance of the AccelStepper class:
AccelStepper stepper1 = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup()
{
   // Set the maximum speed in steps per second:
   pinMode(en, OUTPUT);
   digitalWrite(en, LOW);   
   stepper1.setAcceleration(150);
}

void loop()
{   
   stepper1.setMaxSpeed(600);
   stepper1.moveTo(100);
   stepper1.run();
}

Changes to code to accommodate my CNC shield setup with Uno to test code.

1 Like

Thank you groundFungus it was the setSpeed I didn’t understand. :+1: