Stepper motor with button press

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
  }
}

give the duck a go

1 Like

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

Hi @shrunkenned,

although the way you handle the button is not advisable (there is no "debouncing") you are lucky:

Your sketch works ... I assume that you might not have the correct value here

const int stepsPerRevolution = 2048;

What kind of stepper motor do you have? You may try this instead

const int stepsPerRevolution = 200;

Even if you leave away the second while() in loop() it should work, because the function

  motor.step(stepsPerRevolution);

blocks loop() until all steps have been performed. That is the reason why in your sketch debouncing is not necessary ...

This is your sketch but with a few changes and it works in Wokwi simulation:
(see here https://wokwi.com/projects/360659738759204865

Your modified sketch:

#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 ...!!!

2 Likes

Thank you so much for the reply, very helpful! Will try this when I get home! :slightly_smiling_face:

Try:
Stepper motor(stepsPerRevolution, in1Pin, in3Pin, in2Pin, in4Pin);

1 Like

If you have not used Wokwi simulation you might find it quite useful to learn coding ...

There are plenty of examples with a lot of different actuators and sensors.

Good luck!
ec2021

Your initial steps per revolution was correct. A BYJ stepper has 2048 steps per revolution not 200.

That was it!!! For whatever reason that fixed it!! Thank you so much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.