How To Get Your First Arduino Stepper Motor Running

I’m more of a software guy. So when it came time to hook up my stepper motor, I was very concerned that it wouldn’t work, and I’d have no idea how to troubleshoot it.

But I persevered, it worked (as seen on my earlier post) – and so I’m documenting step by step how you can get your stepper motor working on the Arduino.

First off, the setup. I used an Arduino Duemilanove, a 28BYJ-48 stepper motor, and a ULN2003 driver board – all of which I . Here’s the motor and board:

The easy part is this connection – there’s only one way for the motor to plug in:

ULN2003 Stepper Board and 28BYJ-48 connected

As far as the board, there’s just a few things to remember:

  • Everything should be powered off. No power into the board, no running Arduino, nothing. Always disconnect everything before changing or rewiring as well.
  • The board jumper stays put. I used a 5 volt stepper motor, and so didn’t change anything. I actually don’t know if the jumper is used for higher voltages, or what; however, unless you’re using a different motor rating, leave the jumper as is (and if you are, sorry this tutorial can’t be of more help).
  • The +/-5V for these terminals actually caused me a lot of grief. I knew you had to run separate power for the motors, but how to do it exactly was a problem (in theory you could use the power from the Arduino, but the Internet is full of horror stories from people who tried and failed). In the end, I found an adjustable power plug I had lying around, set it to 4.5v (5v wasn’t available as a setting), and connected the two power wires to the two pins on the stepper board.
  • Trying to figure out plus and minus power in the rat’s nest of cables I was using was not fun. However, here’s a tip: using your multimeter set up properly, the black wire will be negative, or ground, unless you connected something wrong and got a reverse reading on your multimeter. Test, and don’t rely on black always being ground, since a mistake can burn out components!
  • The four pins from the board to the Arduino are labelled IN1 through IN4 – connect these to the Arduino pins 8 through 11. I recommend you be consistent: IN1 to 8, IN2 to 9, etc. If you’re using a cable then of course it will be simpler.

By the way, the biggest problem was not the hardware setup, but the wiring: the pins on the stepper board required plugs (female connectors) and the pins on the Arduino required pins (male connectors). I’d ordered more cables, but that didn’t help now; scrounging around I was able to find some parts to cut up. However, if possible, get the proper connectors at the same time you get the board – you’ll be glad you did.

SOME ITEMS YOU MIGHT ENJOY
IC : ULN2003 : ULN2003AN : DIP 16 : DARLINGTON TRANSISTOR ARRAY : 3pcs per lot
$5.99
End Date: Tuesday Jan-23-2018 11:42:57 PST
Buy It Now for only: $5.99
Buy It Now | Add to watch list
IC : ULN2003 : ULN2003Ag4 : DARLINGTON TRANSISTOR ARRAY : 3pcs per lot
$5.99
End Date: Tuesday Jan-23-2018 11:42:57 PST
Buy It Now for only: $5.99
Buy It Now | Add to watch list
ULN2003AN ULN2003 Stepper Motor Driver Board Module for 28BYJ-48 5V 12V Arduino
$0.35 (4 Bids)
End Date: Wednesday Jan-24-2018 13:46:27 PST
Bid now | Add to watch list


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:
    http://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. 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);
    }
    }

  11. 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.

  12. 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

  13. 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!

  14. 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.

  15. 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
    }

  16. 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

  17. 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!

  18. 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?!

Leave a Reply

Your email address will not be published. Required fields are marked *