I'm completely new to working with Arduino and stepper motors. I'm trying to set up a NEMA 17 stepper motor using an Arduino R3, an A4988 stepper motor driver, and a 12V power supply. However, the motor isn't running; it's grinding and alternating with a low beep sound. How can I let it run (smoothly)?
/*
* Basic example code for controlling a stepper without library
*
* by Dejan, https://howtomechatronics.com
*/
// defines pins
#define stepPin 2
#define dirPin 5
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 800; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(700); // by changing this time delay between the steps we can change the rotation speed
digitalWrite(stepPin,LOW);
delayMicroseconds(700);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 1600; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}
You are trying to make the motor move too fast without acceleration. Add a delay at the end of each loop code. Test the delay value and increase it until you find one that works.
Thank you so much for your answers and for taking the time to think this through. Now my motor is grinding with no beep sound. It sounds like it is working but not really rotating yet. Is it possible that something is just damaged and therefore not really working?
/*
* Improved example code for controlling a stepper without a library
*
* Adjustments made for smoother operation with acceleration.
* by Dejan, https://howtomechatronics.com
*/
// Define pins
#define stepPin 2
#define dirPin 5
void setup() {
// Set the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
// Move in one direction with acceleration
digitalWrite(dirPin, HIGH); // Enable motor to move in one direction
int delayTime = 20000; // Start with a large delay for acceleration
for (int x = 0; x < 800; x++) { // 800 steps for one full rotation
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayTime);
if (delayTime > 1000) delayTime -= 50; // Gradually reduce delay to accelerate
}
delay(1000); // One-second pause
// Move in the opposite direction with acceleration
digitalWrite(dirPin, LOW); // Change rotation direction
delayTime = 20000; // Reset delay for acceleration in the opposite direction
for (int x = 0; x < 1600; x++) { // 1600 steps for two full rotations
digitalWrite(stepPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(stepPin, LOW);
delayMicroseconds(delayTime);
if (delayTime > 1000) delayTime -= 50; // Gradually reduce delay
}
delay(1000); // One-second pause
}
The code you added tells me otherwise! Go back to the original code.
See you are delaying 700 microseconds between steps. Do you understand that code? Your motor cannot make steps every 700 microseconds!
Change the last delayMicroseconds(700) to delay(500) and see if the motor turns.
The motor is not working because you are not paying attention to forum members' instructions.
For example, you did not significantly change the step rate.
Set the current limit properly. This video explains the process for A4988 motor drivers from Pololu, but may not be correct for yours because the current sense resistors vary among manufacturers.
I had similar problems when I started with these steppers. After advice I started using the accelstepper library. Highly recommend to look after acceleration etc.