0

I'm relatively new to Arduino thus need your help to figure out why my stepper motor doesn't rotate clockwise and counterclockwise. All I could do was to make it rotate only in one direction.

I am using AccelStepper Library

Here is my code:

#include <AccelStepper.h>
//Define stepper motor connections
#define dirPin 4
#define stepPin 14
//Create stepper object
AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.
void setup()
{
  stepper.setMaxSpeed(1200); //maximum steps per second
}
void loop()
{
  stepper.setSpeed(1200); //steps per second
  stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
}

I have tried to add delay and counterclockwise code but its not working.

#include <AccelStepper.h>
//Define stepper motor connections
#define dirPin 4
#define stepPin 14
//Create stepper object
AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.
void setup()
{
  stepper.setMaxSpeed(1200); //maximum steps per second
}
void loop()
{
  Serial.println("clockwise");
  stepper.setSpeed(1200); //steps per second
  stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
  delay(1000);
  Serial.println("counterclockwise");
  stepper.setSpeed(-1200); //steps per second
  stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
  delay(1000);
}
  • Try to use lower values for spinning, let's say 150. Some drivers are easy overheating when using higher speeds. – smajli May 30 at 11:03
  • it rotate only in one direction ... which direction is that? – jsotola May 30 at 17:33
  • No its not about heating or higher speed If i put 1200 it move perfectly clockwise if i put value to -1200 it move perfectly counter-clockwise But if i try to use both it creates problem – Hardy May 30 at 18:09
  • What hardware do you use? I mean, what is the stepper motor driver? There are some drivers which overheat easily and (due to built-in overheat protection) stop operation. – smajli May 31 at 8:18
  • @smajli its not about heating i am missing something with code if i rotate in 1 direction only with default code it works fine. Motor - NEMA17 5.5Kgcm Stepper Motor – Hardy May 31 at 14:38
2

Hi here it the reference to the library functions that are available in accelstepper library.

Go through the link, it will provide you the issues with your code and example for your application like @smajli said steps more than 1000 is unreliabe, here you can see setSpeed(float) function where setting negative or positive sign selects the direction and value set the speed of rotation.

  • than you for your input but i am still struggling to understand functions :( i have changed speed to 1000 and behavior is still same – Hardy May 30 at 15:15
  • did you tried -500 and 500 ? – Vaibhav May 31 at 16:13
  • yes buddy seems i can't use delay function in loop but i am not getting how to use other function of this library or use millis function – Hardy May 31 at 18:55
0

From the AccelStepper website: link

Speeds of more than 1000 steps per second are unreliable

Try to change the speed to lower value, and let us know what is the outcome.

  • thank you for pointing out but still same behavior – Hardy May 30 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.