Hi there
I am new in the arduino world. I tried a project running a stepper motor using arduino uno and an simple on off switch.
The aim is that, if I switch on the switch the motor runs the max power and runs untill I switch it off. Basically a simple thing
I got help regarding the code, did upload libraries and the code to the device, wired everything up, but: no move.
Only fuse (the motor is not moving but )
Would be nice if someone could have a look at my setup. Probably you see what is wrong.
Thanks in advance
#include <AccelStepper.h>
// Define the stepper motor pins
#define STEP_PIN 3 // Pin for sending step pulses to the HBS86H
#define DIR_PIN 4 // Pin for direction control (clockwise/counterclockwise)
#define ENABLE_PIN 5 // Pin to enable/disable the motor (LOW enables the motor)
// Define the on/off switch pin
const int switchPin = 2; // Pin for the on/off switch
bool switchState = false; // To track the state of the switch
// Create an instance of the AccelStepper library
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
void setup() {
// Initialize the pins
pinMode(switchPin, INPUT); // Set the switch pin as input
pinMode(ENABLE_PIN, OUTPUT); // Set the enable pin as output
digitalWrite(ENABLE_PIN, LOW); // Enable the stepper driver (LOW to enable)
// Set the maximum speed and acceleration for the motor
stepper.setMaxSpeed(1500); // Maximum speed in steps per second (adjust as needed)
stepper.setAcceleration(500); // Acceleration in steps per second^2 (adjust as needed)
}
void loop() {
// Read the state of the on/off switch
switchState = digitalRead(switchPin);
if (switchState == HIGH) { // When the switch is ON
// Start the motor with maximum speed and constant torque
if (!stepper.isRunning()) {
stepper.setSpeed(1500); // Set motor speed to maximum (adjust as needed)
}
stepper.runSpeed(); // Continuously run the motor at the set speed
}
else { // When the switch is OFF
// Gradually decelerate the motor and stop it
stepper.setSpeed(0); // Set speed to 0 to stop the motor
stepper.runSpeed(); // Gradual deceleration to stop the motor
}
// Ensure smooth operation by calling run() regularly
stepper.run();
}








