Stepper motor rotates slowly on its own

This is my code...

#include <Stepper.h>

int stepsPerRev = 2048;

Stepper stepper (stepsPerRev, 11,9,10,8); // ( IN4,IN2,IN3,IN1)

int btn1 = 7;
int btn2 = 6;

void setup() {
 stepper.setSpeed(15);
 
  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
}

void loop() {
 
  if (digitalRead(btn1)==HIGH){
    stepper.step(1);
    }
  if (digitalRead(btn2)==HIGH){
    stepper.step(-1);
    }

}

I want to control the stepping motor using two buttons.

If I keep pressing one button, the motor rotates to one side.
If I keep pressing the other button, the motor rotates in the opposite direction.
I want this kind of control.

But
The motor rotates little by little even though I didn't press the button.
I want to solve these problems.
Geniuses, please help me.

The Stepper motor is 28BYJ-48 and Driver is ULN2003

change to INPUT_PULLUP, assuming your inputs are probably floating around in the air.

If the buttons are connected to the input pin and ground, change the mode to INPUT_PULLUP. If the buttons are connected to the input pin and +5, make sure there is a pull0down resistor between the input pins and ground. What you are seeing is likely the result of a floating pin that is neither a solid high nor a solid low.

1 Like

Read about floating inputs and what to do about them here

http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

1 Like

Thank you for answering Perehama!
I just changed the code and tested it.
The rotation has stopped, but the button is not working. T.T

Just as the spring mechanically pushes open the momentary button when you remove your finger, and without that spring the button may or may not open or close, you have to electrically hold the pin in one state and look for a state change to act on... so as Picky said, you pull them up or down with a resistor and then the button pulls them the other way

This is a typical push-button circuit that pulls the port pin LOW when the button is pressed. It uses the 10k ohm pullup to keep the state high when the button is open. You can omit the cap and diode. they are optional to protect the input from damage. You can also use INPUT_PULLUP instead of an external pullup resistor. And you can optionally invert the logic using an external pull-down resistor and the button will be tied instead to 5 volts.

I'm still a beginner at Arduino, so I don't understand it well.
This is a circuit diagram of my switch.
What should I solve?

  1. tie both buttons to ground instead of 5V
  2. use INPUT_PULLUP in your pinMode() lines
  3. change HIGH to LOW in the following lines
1 Like

Thank you very much.
It's been resolved. :joy:

Thank you again for your kindness.
Have a great day. Professor. :+1: :+1: :+1:

And remove those resistors.

1 Like

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