Stepper Motor will only go in one direction

Im having a little issue with my stepper motor. I was using a basic code from the arduino Library but the motor will only turn in one direction even with a " - " infront of it. It will only vibrate when going in the other direction but not move.

I haven't changed the code but Maybe my motor isn't set up right

This is the wiring configuration I used.

Other than a potential wiring error im stumped.

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

You cannot use the Stepper library with that type of stepper driver. For step/dir drivers like yours (A4988, DRV8825 to name 2) use the AccelStepper library or one of the many others. Lately I have been using MobaTools stepper library.

It is simple to write code to work with those drivers, you don't really need a library for simple stuff. See Robin2's simple stepper code tutorial. And also his stepper basics tutorial may be of interest.

In the future, please post code here so we do not have to download it. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post.

I looked at your examples. Which one are you using as I do not see ANY of the examples allow you to change motor direction?

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

I do not see anything in stepper.h that allows direction change. What do you see that is different?

The negative sign for the counterclockwise rotation, I believe would change the direction. I'm fairly new to this to I could also be wrong.

That is correct. But instead of discussing why you though your code would work, you should focus on the bigger picture presented in reply #1.

Stepper.h is not used with a step/dir driver like you show.

Hi,
In addition to this library not working with this driver,
your code does not match your step driver pins.
In your drawing the driver is connected to pins 0 and 3,
and in the code it says it should be 8, 9, 10, 11.

Remembering that pin 0 is used with the serial, and it is not recommended to use it with the I/O pin.

You can also use the AHEasy Library.. I find this one a little simpler that accelstepper.

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