I have a stepper motor, arduino, stepper motor module and these buttons:
But every time I try to wire it all up and test the button, the button just turns off the arduino instead of making the motor turn. Any suggestions to make this setup work or any suggestions for a button to make this work would be greatly appreciated! Thanks
My code:
#include <Stepper.h>
// Define the number of steps per revolution (adjust depending on your stepper motor)
const int stepsPerRevolution = 2048; // Example for 28BYJ-48 stepper motor
// Create a Stepper object (Specify the pin numbers connected to the motor)
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // Pins 8, 9, 10, 11 for motor
int buttonPin = 2; // Pin connected to the button
int buttonState = 0; // Variable to store button state
int lastButtonState = 0; // Variable to store previous button state
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
// Set the stepper motor speed (adjust to your needs)
myStepper.setSpeed(15); // Speed in RPM
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the button state
// Check if the button is pressed (low because of pull-up resistor)
if (buttonState == LOW && lastButtonState == HIGH) {
// Button has been pressed, so start the stepper movement
moveStepper();
}
lastButtonState = buttonState; // Store the current button state for the next loop
}
void moveStepper() {
// Move the stepper motor from one position to another
// Move 110 degrees (stepper motors move in steps, so you must calculate how many steps)
int stepsToMove = stepsPerRevolution * 110 / 360; // Calculate steps for 110 degrees
// Rotate motor 110 degrees clockwise
myStepper.step(stepsToMove); // Move stepper motor in steps
// Wait for 30 seconds at 40 degrees
delay(30000);
// Rotate motor back to the initial position (150 degrees, same as start position)
myStepper.step(-stepsToMove); // Move stepper motor back by the same number of steps
// Wait for 30 seconds at 150 degrees
delay(30000);
}











