How To Get Your First Arduino Stepper Motor Running

With everything set up, I’m going to give you the advice I wish I’d had before spending far too many minutes with the example Arduino sketch “stepper_oneStepAtATime”:

Don’t run stepper_oneStepAtATime!

Instead, run “stepper_oneRevolution” in the examples folder. The oneStep sketch very slowly moves your stepper, and as I found out, it’s hard to even tell if it is moving. Combined with not being sure it’s wired properly, and you’ll be pulling your hair out in no time.

The revolution sketch, however, will move it far enough that you’ll see – even on the 28BYJ-48 (which is geared down by 64X to reduce revolutions and increase torque).

If you’re fortunate, loading and running this sketch should have the Arduino moving the stepper motor back and forth about 1/6 of a turn for this motor (others are different).

I however, was not so fortunate. It turned out that my cables were nonstandard, and the wires were crossed up.

If this happens to you, you can solve this by searching in the sketch for this one line:

Stepper myStepper(stepsPerRevolution,8,9,10,11);

This line indicates the pins going in; the order is important. Get them wrong, and the motor doesn’t move – or moves wrong.

What I did was enter all permutations of these pins, and tested each one, one at a time. You can copy and paste this text into your sketch, selectively adding and removing the comment tags as you compile, upload, and test each one:

Stepper myStepper(stepsPerRevolution,8,9,10,11);
// Stepper myStepper(stepsPerRevolution,8,9,11,10);
// Stepper myStepper(stepsPerRevolution,8,10,9,11);
// Stepper myStepper(stepsPerRevolution,8,10,11,9);
// Stepper myStepper(stepsPerRevolution,8,11,9,10);
// Stepper myStepper(stepsPerRevolution,8,11,10,9);
// Stepper myStepper(stepsPerRevolution,9,8,10,11);
// Stepper myStepper(stepsPerRevolution,9,8,11,10);
// Stepper myStepper(stepsPerRevolution,9,10,8,11);
// Stepper myStepper(stepsPerRevolution,9,10,11,8);
// Stepper myStepper(stepsPerRevolution,9,11,8,10);
// Stepper myStepper(stepsPerRevolution,9,11,10,8);
// Stepper myStepper(stepsPerRevolution,10,8,9,11);
// Stepper myStepper(stepsPerRevolution,10,8,11,9);
// Stepper myStepper(stepsPerRevolution,10,9,8,11);
// Stepper myStepper(stepsPerRevolution,10,9,11,8);
// Stepper myStepper(stepsPerRevolution,10,11,8,9);
// Stepper myStepper(stepsPerRevolution,10,11,9,8);
// Stepper myStepper(stepsPerRevolution,11,8,9,10);
// Stepper myStepper(stepsPerRevolution,11,8,10,9);
// Stepper myStepper(stepsPerRevolution,11,9,8,10);
// Stepper myStepper(stepsPerRevolution,11,9,10,8);
// Stepper myStepper(stepsPerRevolution,11,10,8,9);
// Stepper myStepper(stepsPerRevolution,11,10,9,8);

As you go through, you’ll get odd behavior: most of the time the motor will go in only one direction or the other. However, in my case, 8 out of the 24 possibilities worked – although four were in reverse (note that the code will move the motor’s shaft clockwise first, then counterclockwise – just tape a little triangle of paper to the shaft if you have trouble seeing, like I did in my setup).

So in the end, four entries worked: (8,10,11,9), (9,11,8,10), (10,8,9,11), and (11,9,10,8). If I knew my stepper motors, I suppose I could have predicted it, but instead I just tested and retested. Run through them all yourself, and you’ll have your stepper motor working properly in no time.

Once you get that sketch code going, then you’ll have a working stepper motor and driver board, all under Arduino control! From there, take a look at the other sketches, or program it directly yourself – at least you’ll know it works before you go and break it with some code!

34 thoughts on “How To Get Your First Arduino Stepper Motor Running

  1. I’m ordering the parts to setup my Arduino like yours to run a stepper motor.
    Would you mind if I ask more questions when the parts from eBay arrive?
    Thanks
    Calvin

  2. Hi, I have purchased the same items as you. as far as I know:
    – the jumper is used to activate/deactivate the four red leds (these should be used for “debugging” purposes so there’s no need to let them lighting once our setup is stable).
    – you can plug in a 9v battery into the arduino power plug, and use the Vin line to connect to the + connector of the driver board, this will give separate 9v to the motor (which can run anywhere between 5 and 12v), without draining current from the usb connector.

    good luck!

  3. Hi,
    Thanks for the detailed information. I was able to make my stepper motor work with more or less the same components as you.
    Thanks again.

  4. Hey, Everybody!
    LOOK AT PAGE 2!
    To work with Arduino Stepper Examples you need chance the sequence to (8,10,11,9), (9,11,8,10), (10,8,9,11), or (11,9,10,8).
    #include

    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,10,11,9);

    void setup() {

    myStepper.setSpeed(100);

    }

    void loop() {

    myStepper.step(stepsPerRevolution);

    }

  5. I was about ready to throw these out the window until I came across this post.

    So these motors have a step angle of 5.625. That’s 64 steps so change your code accordingly. I cannot get them to run at a step speed faster than about 100.

    Something is fishy though. at 1000 steps times 64 steps to make a full rotation this motor should be rotating fully over 15 times. But it doesn’t even do a half turn!

    Any ideas?

    • Use the following for (it worked for me)

      Though, I’m still having trouble getting it to rotate two or more times in one direction. I’ll post an update once I figure this out.

      PS. THANK YOU FOR POSTING THIS!!

      #include

      const int stepsPerRevolution = 2050; // change this to fit the number of steps per revolution
      // for your motor
      //2050 = 360deg
      //1025 = 180deg
      //512.5 = 90deg
      //5.6944 = 1deg

      // initialize the stepper library on pins 8 through 11:
      // the order of the numbers below is important, 8,10,11,9 does not equal 8,9,10,11,
      //these numbers correspond to the pins on the digital side of the arduino uno
      // 1N1 on pin 8
      // 1N2 on pin 9
      // 1N3 on pin 10
      // 1N4 on pin 11
      // powered via 5v
      // Max speed is 14RPM @ 5v

      Stepper myStepper(stepsPerRevolution, 8,10,11,9);

      void setup() {
      // set the speed at 14 rpm:
      myStepper.setSpeed(14);
      // 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);
      }

      • This worked for me IcanCwhatUsay not sure how the number of steps per revolution = 2050 as it doesn’t seem to divide down to what the specs say but I’m not going to worry about that if it works. Thanks for the help. Now what to do with the 5 motors and controllers ?

        • Although I haven’t tried it, the Stepper in the code is an object, so in theory you could create myStepper1, myStepper2, etc, and run multiple ones that way…

  6. Not sure what your motor is, so I’ll suggest several things:

    * If you’re using a Bipolar instead of a Unipolar Motor, that won’t work in reverse – here’s a quick overview:
    https://www.utopiamechanicus.com/726/unipolar-bipolar-stepper-drivers-motors/

    * If the motor is geared down, you’ll always be slow – the 28BYJ-48 here is geared down internally, and painfully slow to run.

    * Power may be an issue. Depending on your voltage and current, you may not be giving the motor enough ‘juice’ (technical term!) This driver is around 500mA max per coil, so if your motor needs say 1A, you’ll get poor performance.

    However, if you’re getting OK performance until you speed it up, my best guess is that it’s geared down, and you’re limited in speed. On the other hand, gearing down does make these motors more powerful, so that can be a benefit…

  7. Use the following for (it worked for me)

    Though, I’m still having trouble getting it to rotate two or more times in one direction. I’ll post an update once I figure this out.

    PS. THANK YOU FOR POSTING THIS!!

    #include

    const int stepsPerRevolution = 2050; // change this to fit the number of steps per revolution
    // for your motor
    //2050 = 360deg
    //1025 = 180deg
    //512.5 = 90deg
    //5.6944 = 1deg

    // initialize the stepper library on pins 8 through 11:
    // the order of the numbers below is important, 8,10,11,9 does not equal 8,9,10,11,
    //these numbers correspond to the pins on the digital side of the arduino uno
    // 1N1 on pin 8
    // 1N2 on pin 9
    // 1N3 on pin 10
    // 1N4 on pin 11
    // powered via 5v
    // Max speed is 14RPM @ 5v

    Stepper myStepper(stepsPerRevolution, 8,10,11,9);

    void setup() {
    // set the speed at 14 rpm:
    myStepper.setSpeed(14);
    // 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);
    }

    • UPDATE!!

      Ha! I’m an idiot!

      Once the code I just posted works for you, you can change the line in the loop
      to any multiple of 2050. So it would look like this for 2 revolutions:

      myStepper.step(4100);

      man I can be dumb some times! hahaha

  8. Hi All
    —-
    Got the exact same items, but have a silly problem:
    My motor always turns clockwise!

    Even when I give the step() function negative numbers it still turns clockwise.
    Anyone has an ides?
    I doble checked the wiring, everything seems correct
    When I do single steps back and forth: stepper.step(1) and stepper.step(-1) I see the led cycle trough all four states, meaning the stepper.step(-1) is interpreted wrongly as stepper.step(1)

    Uri

    • Found the answer and for the motor turning only clockwise

      The stepper has to be initialized with the following pin sequence:
      stepper(STEPS, 8, 10, 9, 11);

      Have Fun
      Uri

  9. This sketch works fine with the Ebay kit. Ive added some extra notes for info.

    #include

    const int stepsPerRevolution = 2050; // change this to fit the number of steps per revolution
    // for your motor
    //2050 = 360deg
    //1025 = 180deg
    //512.5 = 90deg
    //5.6944 = 1deg

    // initialize the stepper library on pins 8 through 11:
    // the order of the numbers below is important, 8,10,11,9 does not equal 8,9,10,11,
    //these numbers correspond to the pins on the digital side of the arduino uno
    // 1N1 on pin 8
    // 1N2 on pin 9
    // 1N3 on pin 10
    // 1N4 on pin 11
    // powered via 5v
    // Max speed is 14RPM @ 5v

    Stepper myStepper(stepsPerRevolution, 8,10,11,9);// note out of sequence but it works

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

    void loop() {
    // step one revolution in one direction:
    Serial.println(“clockwise”);
    myStepper.step(stepsPerRevolution);// this line can be commented for constant rotation in this direction
    delay(1);//time between rotations, do not set to 0 if motor is going to change direction

    // step one revolution in the other direction:
    Serial.println(“counterclockwise”);
    myStepper.step(-stepsPerRevolution);// this line can be commented for constant rotation in this direction
    delay(1);//time between rotations, do not set to 0 if motor is going to change direction
    }

  10. I just check some web sites. I think number of steps should be 2048 instead of 2050.

  11. Start simple to get the motor to rotate at a constant counter clockwise direction with the following code…no libraries:

    byte PortMask[8] = {B00100000,B00110000,B00010000,B00011000,B00001000,B00001100,B00000100,B00100100};
    //Use bits 2 through 6 that correspond to pins 10 through 13
    int i;
    void setup()
    {
    DDRB = B00111100; //set digital pins -,-,13,12,11,10,-,- as output
    }
    void loop()
    {
    for (i = 0; i<8; i++)
    {
    PORTB = PortMask[i];
    delay(2);
    }
    }

  12. An easy fix for the ULN2003 board with the male pins is to soften the solder on the bottom of the board and push the 1N1 through 1N4 pins through. Then the ULN2003 will push into a breadboard.

  13. can any one suggested me how to connect stepper motor with Arduino Duemilanove USB Microcontroller Module…
    i have one Arduino Duemilanove USB Microcontroller Module .. so how can i connect stepper motor and how i control stepper motor through USB port

  14. These motors are geared 64 to one. Therefore you would have 64 x 64 steps per rotation or 4096. Change the steps per rev to this and alls hould work well. Good Luck!

  15. Hi have just found this informative site having tried in vane to get a stepper motor running via a Ul2003 board and Ardunio Uno R3 I am now up and running I.E motor runs in one direction ,many thanks for your help
    I am new to both electronics and programming and would appreciate if you could help me run a 28BYJ-48 5volt DC step motor in small increments of about 10 degrees with a pause of about 30secs between moves ,My aim is to move an object along fixed geared rails in small steps for macro photography, any help in the programming would be appreciated

    Regards das10

    • You could probably tweak the code to run for a specific number of steps (=10degress), and then use delay() to wait 30 sec.

  16. Commenting to Computer repair above. Not true, tested. Number of steps should be anyway 2050.

  17. Thank you for taking time to share your tutorial. My motor leads were not what the sketch expected and you help me get it sorted out.

  18. Hi folks, many thanks to all of you for sharing this useful information. I used the code as posted by Amagnu but tweaked the variable “stepsPerRevolution” to new value of 2064. Now it is possible to achieve precise 360 degree motion back and forth. Motor shaft stops always in the same position. No angular shift. Code as follows:

    #include

    const int stepsPerRevolution = 2064; // change this to fit the number of steps per revolution
    // for your motor
    //2064 = 360deg
    //1025 = 180deg
    //512.5 = 90deg
    //5.6944 = 1deg

    // initialize the stepper library on pins 8 through 11:
    // the order of the numbers below is important, 8,10,11,9 does not equal 8,9,10,11,
    //these numbers correspond to the pins on the digital side of the arduino uno
    // 1N1 on pin 8
    // 1N2 on pin 9
    // 1N3 on pin 10
    // 1N4 on pin 11
    // powered via 5v
    // Max speed is 14RPM @ 5v

    Stepper myStepper(stepsPerRevolution, 8,10,11,9);// note out of sequence but it works

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

    void loop() {
    // step one revolution in one direction:
    Serial.println(“clockwise”);
    myStepper.step(stepsPerRevolution);// this line can be commented for constant rotation in this direction
    delay(1000);//time between rotations, do not set to 0 if motor is going to change direction

    // step one revolution in the other direction:
    Serial.println(“counterclockwise”);
    myStepper.step(-stepsPerRevolution);// this line can be commented for constant rotation in this direction
    delay(1000);//time between rotations, do not set to 0 if motor is going to change direction
    }

  19. Hello friends…. I have Stepper Motor NEMA23 10KgCm torque with 5A Microstepping Drive and arduino uno. i need to rotate stepper motor with different speeds can you give me coding of arduino and how to connect with each other please help me friends

  20. Thanks, this worked perfectly. I was pulling out my hair before! Motor runs smoother and cooler!

  21. My brother recommended I may like this website. He used to be totally right. This post truly made my day. You can not consider simply how so much time I had spent for this info! Thanks!

  22. I have problems getting the motor turning at all.

    All that happens is that I feel the motor vibrate when getting the pulses but no movement results at all.

    All I do from my UNO is

    Stepper myStepper(stepsPerRevolution,8,9,10,11)
    myStepper.step(1);

    When I checked the patterns of the LEDs they didn’t look in the correct order so I started exchanging the single outputs till the patterns shown by the LEDs came in the required pattern
    ABCD

    1001
    0011
    0110
    1100

    but no movement results from the motor?!