My stepper motor is rotating only in one direction. Need Coding Help
I made all connections with Arduino, driver & motor. The code I had is working fine but it's unable to rotate counter clockwise direction. It's getting jettering, vibrations & again rotating only in clockwise. Here is my code
#include <AccelStepper.h>
// Pin definitions matched to your wiring
#define STEP_PIN 9
#define DIR_PIN 8
#define ENABLE_PIN 11
#define FAULT_PIN 2
// Create AccelStepper object in DRIVER mode
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// User-configurable parameters for motor control
long moveSteps = 1600; // Number of steps to move (adjust based on lead screw pitch)
float maxSpeed = 400.0; // Max speed in steps per second
float acceleration = 200.0; // Acceleration in steps per second^2
bool clockwise = true; // Direction: true = clockwise, false = counterclockwise
void setup() {
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the driver (LOW typically enables)
pinMode(FAULT_PIN, INPUT_PULLUP);
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
Serial.begin(9600);
Serial.println("Stepper control started");
Serial.print("Initial fault state: ");
Serial.println(digitalRead(FAULT_PIN));
}
void loop() {
// Set direction based on clockwise boolean
if (clockwise) {
stepper.moveTo(moveSteps); // Move forward
} else {
stepper.moveTo(-moveSteps); // Move backward
}
while (stepper.distanceToGo() != 0) {
stepper.run();
// Optional: check fault pin and react accordingly
if (digitalRead(FAULT_PIN) == LOW) {
Serial.println("Fault detected!");
// Add fault handling here
break;
}
}
delay(1000); // Pause for 1 second before repeating
// Optionally toggle direction automatically
clockwise = !clockwise;
}
Can you please go through my code & correct me??. TIA
3 Replies
Try this code, see if there is any change, let me know what you find out.
after some 7revs its getting vibrated & resisted when turning in counter clock wise
not severn revs, .
You push code to it does it work just with the code or not?
Your statment says:
The code I had is working fine but it's unable to rotate counter clockwise direction.So is it reversing or not? You know just post a video of the motor running my code. we will go from there. Well after waiting on you for over 2 hours. I guess this must not be that important. So i will see you tomorrow. Heading to bed for the night. Will look over the video tomorrow. G-day/night