Hai!!! Can you help me in controlling the direction of rotation of my stepper motor
The components used are as follows:
- Arduino Nano
2.Stepper Motor Driver - A4988 (Vref 0.77v)
3.1A 125AC limit Switches (2)
4.NEMA 17HS3401 Stepper Motor
5.12V 5A SMPS (Power Supply)
6.47uf decoupling capacitor
I made the connections as per the above pasted wiring diagram,
#include <AccelStepper.h> //accelstepper library
const byte limitSwitch_1 = 2; //pin for the microswitch using attachInterrupt()
const byte limitSwitch_2 = 3; //pin for the microswitch using attachInterrupt()
bool switchFlipped = false; //stores the status for flipping
bool previousFlip = true; //stores the previous state for flipping - needed for the direction change
// direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 4, 5);
void setup()
{
//Limit Switches
pinMode(limitSwitch_1, INPUT_PULLUP); // internal pullup resistor (debouncing)
pinMode(limitSwitch_2, INPUT_PULLUP); // internal pullup resistor (debouncing)
attachInterrupt(digitalPinToInterrupt(2), FlipDirection, FALLING); //do not change it to 'CHANGE'
attachInterrupt(digitalPinToInterrupt(3), FlipDirection, FALLING);
//---------------------------------------------------------------------------
//Serial Communication
Serial.begin(9600); //defining some baud rate
Serial.println("Testing Accelstepper"); //print a message
//---------------------------------------------------------------------------
//Stepper parameters
//setting up some default values for maximum speed and maximum acceleration
stepper.setMaxSpeed(5000); //SPEED = Steps / second
stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
stepper.setSpeed(1500);
delay(500);
//---------------------------------------------------------------------------
}
void loop()
{
stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
flipCheck(); //checking the flip in each loop
}
void flipCheck()
{
if(switchFlipped == true)
{
//Serial.println(previousFlip); //This was just a control flag for debugging
if(previousFlip == true) //If the previous flip is 1, we have positive direction
{
stepper.setSpeed(1500);
}
if(previousFlip == false) //If the previous flip is 0, we have negative direction
{
stepper.setSpeed(-1500);
}
switchFlipped = false;
//We have to reset this, so in the next iteration of the loop, the code will not enter this part, only when there was a click again
}
}
void FlipDirection()
{
switchFlipped = true; //we change the status to true, so the code will enter the flipCheck() function
previousFlip = !previousFlip; //change the state to different from the previous - this controls the direction
}
This the code what i used. So the mechanism is initially the stepper should spin in Clockwise direction, and when limit switch 1 is pressed it should change the direction to Counterclockwise, and when limit switch 2 is pressed it should again change the direction to Clockwise.
I have grounded the Common (C) terminals of the limit switches and connected the Normally Open (NO) terminals to D2 and D3 respectively, so that when the limit switch is pressed the FALLING in voltage will cause an interrupt.
The problem is that the stepper motor is spinning fine initially before pressing the limit switch, but when the limit switch is pressed, the motor is getting freeze or struggling to change the direction, sometimes when i turn the shaft by my hands its changing the direction.
NOTE: I had checked my Stepper using the tutorial codes and it was working good.
Any Help Appreciated. Thanks.
![]()
