Using Stepper.h Library

I'm having some trouble using the Stepper.h library included with Arduino. I had the library working just fine at first then when I uploaded the same code(just to verify everything was working right) the stepper motor stopped working. I had two buttons( one for forwards and one for backwards), when I would press ether one of the buttons the stepper would only turn one direction. What should I do? Switch library's?

It is very hard to troubleshoot code that we cannot see. Post the code and tell us what the code actually does and how that differs from what it should do.

A schematic and a photo of your wiring are also often helpful.

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.

The Stepper library is very simplistic and blocks the whole loop while it moves the stepper

Suggest you move to the AccelStepper library
My tutorial on Multi-tasking In Arduino has an example using this library while also doing other tasks, reading user cmds, reading temps etc.
See the detailed Temp Controlled Damper example

#include <Stepper.h>
#define FULLSTEP 8
Stepper stepper(FULLSTEP, 3, 4, 5, 6);
void setup() {
  Serial.begin(9600);
  stepper.setSpeed(1500);
}
void loop() {
  int Read1 = digitalRead(8);
  int Read2 = digitalRead(9);
  if (Read1 == HIGH) {      //Detection button interface to low
    delay(10);
    if (Read1 == HIGH) {
      stepper.step(10);
    }
  }
  if (Read2 == HIGH) {
    delay(10);
    if (Read2 == HIGH) {
      stepper.step(-10);
    }
  }
}

Sorry I forgot to add that to the original post. The code should turn the stepper motor one way when a certain button is pressed and the other way when the other button is pressed. But that is not what is happening, when I press the first button the stepper goes forwards as intended. But when I press the second button the stepper goes the same way as the first button. Hence my problem, the stepper will only turn one way.

Do you have pulldown resistors connected the switch inputs?

A schematic and a photo of your wiring are also often helpful.

I wired up an old 5V stepper to my Uno using the 754410 bipolar stepper schematic of the stepper library reference.

I modified your code to use active low switches (internal pullups), slowed the stepper down some an increased how far the stepper would move with a button push. This code works. Push one button and the motor rotates one way and the other button it rotates the other way.

#include <Stepper.h>
#define FULLSTEP 8
Stepper stepper(FULLSTEP, 3, 4, 5, 6);

const byte lfButtonPin = 8;
const byte rtButtonPin = 9;

void setup()
{
   Serial.begin(9600);
   pinMode(lfButtonPin, INPUT_PULLUP);
   pinMode(rtButtonPin, INPUT_PULLUP);
   stepper.setSpeed(500);
}
void loop()
{
   int Read1 = digitalRead(lfButtonPin);
   int Read2 = digitalRead(rtButtonPin);
   if (Read1 == LOW)        //Detection button interface to low
   {
      delay(10);
      if (Read1 == LOW)
      {
         stepper.step(100);
      }
   }
   if (Read2 == LOW)
   {
      delay(10);
      if (Read2 == LOW)
      {
         stepper.step(-100);
      }
   }
}

You will soon outgrow the stepper library. It blocks, using it with more than one stepper can be a challenge and it will not work with stepper drivers that drive real steppers. The AccelStepper library is better for real uses.

bipolar stepper 754410.jpg

bipolar stepper 754410.jpg

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