Stepper Motor: Beginners question

Hello folks,

since I am new here I posted this topic in another section, but after I found this here I suppose it needs to go in here.

I want to move a stepper from start to 100 steps, then wait and go back to start on a single klick on a button.

Klick - move 100 steps - wait - move 100 steps back

When the next klick comes it shall repeat above scenario.

Everything works fine, but it repeats gong from A to B and Back 5 times.

Maybe I am having some noob error in my sketch.

May some please guide me out of the darkness and tell me how to stop that wiggling.

Thank you for any help.


Here is my Code:

#include <Stepper.h>
#define STEPS 2038 // the number of steps in one revolution of your motor (28BYJ-48)
Stepper stepper(STEPS, 8, 10, 9, 11);

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  // pinMode(buttonPin, INPUT);

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  stepper.setSpeed(0); // don't move
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  stepper.setSpeed(6); // 6 rpm
  stepper.step(100); // move A to B
  delay(1000); // wait for one second
  stepper.setSpeed(6); // 6 rpm
  stepper.step(-100); // move back from B to A
  stepper.setSpeed(0); // stop
  } else {
    stepper.setSpeed(0); // no button pressed no move
    //if (buttonState == LOW) {
     //   stepper.setSpeed(0);

Everything works fine, but it repeats gong from A to B and Back 5 times.

How have you wired the button switch? It sounds like it is floating in an uncertain state when the button is not pressed. A better way is to use INPUT_PULLUP and wire the switch so it connects the pin to GND when pressed. Then the test should be

if (buttonState == LOW) {

Separately, there is no need to set the speed to 0. When all the steps are finished the motor will stop.

Stepper Motor Basics
Simple Stepper Code

Thanks for the input.
Guess its pretty much an error I will roll my eyes on when I read my post in a few weeks....

Thank you for your patience.

I found a tutorial on pull resistors now.

And you were spot on! This solved the problem immediately.

It now moves the way I want it.

The stop was just a idea if that might help. Sure its useless since I told it to move from A to B and back. Now I know.....