This is my first time owning an Arduino, I have a stepper motor 28BYJ-48 with ULN2003 driver. I am trying to hook up a button press that will rotate it one full rotation clockwise and stop, and repeat one rotation every button press, but I am having difficulty writing this code as this is the first time I’ve done anything like this before. I’ve spent days watching tutorials and reading websites but can’t figure it out. I am stumped. Can anyone tell me what I am doing wrong? This is the current code I have written but it still does nothing when I upload it to the Arduino.
#include <Stepper.h>
// Define constants for the stepper motor
const int stepsPerRevolution = 2048;
const int stepsPerDegree = stepsPerRevolution / 360;
// Define pins for the ULN2003 driver
const int in1Pin = 8;
const int in2Pin = 9;
const int in3Pin = 10;
const int in4Pin = 11;
// Define pin for the button
const int buttonPin = 2;
// Create a new instance of the Stepper class
Stepper motor(stepsPerRevolution, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() {
// Set the motor speed (RPM)
motor.setSpeed(10);
// Set the button pin as input
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Wait for the button to be pressed
while (digitalRead(buttonPin) == HIGH) {
// Do nothing
}
// Rotate the motor 360 degrees (one revolution)
motor.step(stepsPerRevolution);
// Wait for the button to be released
while (digitalRead(buttonPin) == LOW) {
// Do nothing
}
}
I have, as well as maker guides, I can find all kinds of code where you have to hold the button to have it rotate but nothing that triggers a full rotation and stop. I’ve tried pasting several strips of code together with no success
#include <Stepper.h>
// Define constants for the stepper motor
// const int stepsPerRevolution = 2048;
const int stepsPerRevolution = 200;
//const int stepsPerDegree = stepsPerRevolution / 360;
// Define pins for the ULN2003 driver
const int in1Pin = 8;
const int in2Pin = 9;
const int in3Pin = 10;
const int in4Pin = 11;
// Define pin for the button
const int buttonPin = 2;
// Create a new instance of the Stepper class
Stepper motor(stepsPerRevolution, in1Pin, in2Pin, in3Pin, in4Pin);
void setup() {
Serial.begin(115200);
// Set the motor speed (RPM)
motor.setSpeed(50);
// Set the button pin as input
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Wait for the button to be pressed
while (digitalRead(buttonPin) == HIGH) {
// Do nothing
}
Serial.println("Start Rotation");
// Rotate the motor 360 degrees (one revolution)
motor.step(stepsPerRevolution);
Serial.println("Stop Rotation");
}
ec2021
P.S.: If your sketch does nothing after upload you should probably carefully check your wiring ...!!!