/*
Arduino | hardware-help
Stepper Motor Randomly Changing Directions
Sultan June 29, 2025— 7:17 PM
I am using an ESP32, ULN2003, and a 28BYJ-48 Stepper Motor.
I have it set up so if the left button is pressed then it will go
reverse, if the right one pressed it will go forward (also if any
of the two previous cases, the blue led turns on) and if neither
are/aren't pressed it stays still. For some reason it just changes
directions whenever it likes. This is my code:
*/
//Includes the Arduino Stepper Library
#include <Stepper.h>
// Defines the number of steps per rotation
const int stepsPerRevolution = 2048;
// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 12, 11, 10, 9);
void setup() {
// Nothing to do (Stepper Library sets pins as outputs)
}
void loop() {
// Rotate CW slowly at 5 RPM
myStepper.setSpeed(5);
myStepper.step(stepsPerRevolution);
delay(1000);
// Rotate CCW quickly at 10 RPM
myStepper.setSpeed(10);
myStepper.step(-stepsPerRevolution);
delay(1000);
}