Stepper Motor losing steps when using enable pin

I wrote a program that makes one revolution on my stepper motor. It uses the Accelstepper library and a Hybrid Servo Driver (HSS57, if it's useful). I have the following code:

#include <AccelStepper.h>
int STEP = 4; // Pin pulse
int DIR  = 5; // Pin direction
int ENA = 6; // Pin enable
int RPM = 200; 
int SPR = 400; // Steps per revolution
int SPS; // Steps per second
AccelStepper stepper(1, STEP, DIR);

void setup()
{ 
   stepper.setEnablePin(ENA);
   stepper.setPinsInverted(0, 0, 1);
   stepper.enableOutputs();
   SPS = (RPM * SPR) / 60;
   stepper.setSpeed(SPS);
   stepper.setCurrentPosition(0);
   stepper.moveTo(400); 
   stepper.setMaxSpeed(1000);	
   stepper.setAcceleration(1000);
}

void loop()
{  
  stepper.run();
}

However, this losses steps, as it makes just under one revolution. But when I delete the "stepper.setEnablePin(ENA);" line it works just fine.

Why is that? Did I make a mistake in my code?

The calculation RPM * SPR likely overflows. 400 x 200 == 80 000 > 65535 max of an int.

Use serial.println showing SPS on serial monitor.

1 Like

Which Arduino?

1 Like

How many steps is it missing?

Try inserting a

delay(100); 

right after the

stepper.enableOutputs();

and see if that fixes it.

1 Like

The enable pin (when turned off) does not stop the stepper, in contrast it allows it to spin freely.

1 Like

I changed the data type from int to long, the issue persists.

I'm using an Arduino Nano.

It unfortunately did not stop the issue :c. I don't know how many steps it's missing, but it does roughly a 300° rotation.

I know that, when the pin is set to LOW it spins. When it's set to HIGH it blocks. So at least it's working properly in that sense.

Of what?

Changing the data type of just SPS isn’t enough to change the type of this calculation:

At least one of the constituents in the multiplication also needs to be bigger than an int.

You are right, I had only changed SPS. I changed the data type of SPR and RPM now too but it didn't change anything. Do I need to do something else?

Please post your changed code so we can follow along.

#include <AccelStepper.h>
int STEP = 4; // Pin pulse
int DIR  = 5; // Pin direction
int ENA = 6; // Pin enable
long RPM = 200; 
long SPR = 400; // Steps per revolution
long SPS; // Steps per second
AccelStepper stepper(1, STEP, DIR);

void setup()
{ 
   stepper.setEnablePin(ENA);
   stepper.setPinsInverted(0, 0, 1);
   stepper.enableOutputs();
   delay(100);
   SPS = (RPM * SPR) / 60;
   stepper.setSpeed(SPS);
   stepper.setCurrentPosition(0);
   stepper.moveTo(400); 
   stepper.setMaxSpeed(1000);	
   stepper.setAcceleration(1000);
}

void loop()
{  
  stepper.run();
}

Thanks
Is the motor under load?

If you run at slower speeds (1:2? 1/10?, 1/100?) does it do the stepping correctly?

Are you using an encoder for your closed loop driver?

If you run at slower speeds (1:2? 1/10?, 1/100?) does it do the stepping correctly?

That fixed it! When I run it at 1:1000 it works, maybe I calculated the SPS wrong or something, I'll check that later.
Thank you so much.