4

I have a stepper motor connected to my Arduino like this using the ULN2003A Darlington Array:

Circuit Diagram
Image (Ignore the potentiometer)

And I have programmed it with the following code:

#include <Stepper.h>

int in1Pin = 22;
int in2Pin = 23;
int in3Pin = 24;
int in4Pin = 25;

Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);

void setup()
{
    pinMode(in1Pin, OUTPUT);
    pinMode(in2Pin, OUTPUT);
    pinMode(in3Pin, OUTPUT);
    pinMode(in4Pin, OUTPUT);

    motor.setSpeed(25);
}

void loop()
{
    int steps = 360;

    motor.step(steps);
    delay(500);
}

At the moment the motor rotates clockwise, how could I have it so it rotates in the opposite direction?

My code was copied and edited from here.


The problem was that I had the two middle wires the wrong way round as said here: http://forum.arduino.cc/index.php?PHPSESSID=kvi8dt2b5en5hhk02dlmjrotl5&topic=143276.msg‌​

1 Answer 1

2

The answer is simple. Just pass a negative number of steps as an argument to motor.step();.

Another note: You forgot a semicolon on your second to last line. IIRC this doesn't matter in C, but it's just bad practice to do this. If you add a line of code below that, then it won't work.

Example code:

#include <Stepper.h>

int in1Pin = 22;
int in2Pin = 23;
int in3Pin = 24;
int in4Pin = 25;

Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin);

void setup()
{
    pinMode(in1Pin, OUTPUT);
    pinMode(in2Pin, OUTPUT);
    pinMode(in3Pin, OUTPUT);
    pinMode(in4Pin, OUTPUT);

    motor.setSpeed(25);
}

void loop()
{
    int steps = 360;
    motor.step(steps);
    delay(100);

    steps = -360;
    motor.step(steps);
    delay(500); //Semicolon added
}
6
  • Thanks about the semicolon, I was copying out my code in a rush. Also, I've tried a negative number of steps but it doesn't work.
    – Beta Decay
    Commented May 3, 2014 at 12:42
  • @user12321 I'm not quite sure. Does it compile? This page says that a negative number will go the other way. All I can say is to make sure you copied all of the code right. I can't tell what "doesn't work" means. Commented May 3, 2014 at 12:57
  • Well it compiles and runs but carries on rotating in the same direction
    – Beta Decay
    Commented May 3, 2014 at 12:58
  • I found the solution at the here: forum.arduino.cc/index.php?PHPSESSID=kvi8dt2b5en5hhk02dlmjrotl5&topic=143276.msg109779#msg109779. I had the wires the wrong way round.
    – Beta Decay
    Commented May 3, 2014 at 13:34
  • @user Does the negative work then? Commented May 3, 2014 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.