I am currently working on a setup that involves an Arduino Uno, NEMA 17 stepper motor and an A4988 motor controller.
Through searching I have gotten everything running well with basic run code. The layout I have is shown in the image, plus a potentiometer wired to A0 as well as 5v and Gnd.
My issue: I want to control the speed of the stepper motor with a potentiometer. I have found about 4 or 5 tutorials that utilize the same code and when mimicking the setup it “works” but there is no way to bring the stepper to a 0 speed which I need. I was able to do what I wanted using an L298N controller but it got beyond excessively hot and not safe to use. Below is the code that sort of works:
If anyone has some insight I would greatly appreciate it.
// Defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Converts the read values of the potentiometer from 0 to 1023 into desired delay values (300 to 4000)
return newCustom;
}
I reckon the logic will be easier to understand if you change this
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
to this
void loop() {
intervalBetweenSteps = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(10); // this is the pulse width
digitalWrite(stepPin, LOW);
delayMicroseconds(intervalBetweenSteps);
}
Start with very low stepper speeds - perhaps 200 millisecs between steps - then experiment to see what faster speeds work.
That did make it much easier to understand. With your clarification I have it running much smoother now and more closely to the range of speed I want.
With that informatioin I was able to finish everything I wanted. I added an if/else statement and also added a pin output to control the "enable" function on the motor driver. Complete code shown below. This allows for a complete stop of the stepper motor at a pot value in the "if" statement range. I did have some weird twitching of the motor when I first made it >1022 only leaving 1023 as a possibility so I assume that is some variation in the pot value so making it >1020 solve the issue. Feel free to point out any optimizations in my code, it works but I'm not expert. Thanks again for all the help.
// Defines pins numbers
const int stepPin = 3;
const int dirPin = 2;
const int enablePin = 4;
int customDelay; // Defines variables
int intervalBetweenSteps = 200;
void setup() {
// Sets the three pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enablePin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
digitalWrite(enablePin,LOW); //Enables motor
}
void loop() {
intervalBetweenSteps = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pulse with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(10); // this is the pulse width
digitalWrite(stepPin, LOW);
delayMicroseconds(intervalBetweenSteps);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
if (customDelay > 1020) {
digitalWrite(enablePin, HIGH); //disables the motor
}
else {
digitalWrite(enablePin, LOW); //enables to motor
}
int newCustom = map(customDelay, 0, 1020, 9000,12000); // Converts the read values of the potentiometer from 0 to 1023 into desired delay values
return newCustom;
}