Arduino: Stepper Motor Example Sketch Fixed

Like most of the example sketches you get with the Arduino IDE the Stepper Motor example which rotates the stepper motor 1 full turn clockwise and then 1 full turn counter-clockwise does not work. It was last updated 7 years ago and a lot has changed since then.

In this Instructable I show you the problems and how to fix them easily so that you can start using your stepper motors.

As usual it is far quicker to go watch through the 5 minute video to see how to do this than to go through the Instructable. However, for completeness you should do both!

On to the Problem

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: The Problem

The original code is shown here:

Stepper Motor Control - one revolution

This program drives a unipolar or bipolar stepper motor. The motor is attached to digital pins 8 - 11 of the Arduino.

The motor should revolve one revolution in one direction, then one revolution in the other direction.

Created 11 Mar. 2007 Modified 30 Nov. 2009 by Tom Igoe



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); }

There are three problems with this code not counting its age, that causes this not to work on most Arduino stepper motor setups. I will go through each problem in turn, explaining the problem, solving the problem, and finally we shall see a fully working replacement code which has been posted to Arduino so that they can update the standard example.

Problem One... We're talking about a resolution

Step 2: Problem One... Revolutions Vs Resolutions

A stepper motor requires x number of steps to complete a single revolution. But the documentation for stepper motors do not always provide you with the information you need.We will assume you are using the S8BYJ-48 Stepper motor and the ULN2003 motor driver as shown in the image.

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

The documentation tells us that it has 64 steps of exactly 5.625 degrees making a perfect 360 degrees. That's great so we have 64 steps to the revolution... yes, and no. We do have 64 steps per rotation but the hardware we are using is setup to use half-steps as default so we will actually have 64 x 64 steps or 4096 steps per rotation.

The default code has us setup at only 200 steps per revolution. So we need to set this to 4096, otherwise we won't get anywhere near a full revolution.

Problem two: Pins to the left of me, Pins to the right...

Step 3: Problem Two: Hardware Setup Is Different to Software Setup.

As you can see the wiring diagram for the stepper motor and controller are straight forward. However, they do not correspond to how the sketch believes the stepper motor will be wired up! the Sketch believes that the pins are sequenced 8, 9,10, 11. However, the pins are not sequenced that way in the controller.

So we need to make another change to the code:

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

should now be changed to

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); // Notice the 9 and 10 have been reversed.

This is the correct sequence for the stepper motor and controller.

Problem Three: Speed Kills

Step 4: Problem Three: Faster Is Not Necessarily Better

Electric motors, including stepper motors are not indestructible. They have their limits and speed is one of them.In the example Arduino sketch we are to set our rpm to 60. However, that is actually far too fast for most modern stepper motor kits for the Arduino. So we need to change this as well; change...



myStepper.setSpeed(10); // 15 may work but is unlikely.

Now your code will work. It will run forwards as it should and in reverse order as it should.

Final words and links

Step 5: In Conclusion

The reason the example code does not work is simply because it is old, almost 7 years old. It was written when Windows Vista was first released, and the iPhone was first announced. A lot has happened since then. Sales of the Arduino have gone through the roof, companies are making hundreds of additional add-on sensor boards and connection boards, controllers and kits for consumers, and we are buying them up in their millions.

These kits contain what are now the de facto standard equipment that those beginning their Arduino learning careers are using. But the example sketches do not target them so beginners are finding that their kit does not work, Arduino does not work and in frustration throw the lot in the garbage. It's a shame really if Arduino only updated their code - three lines of code in this case beginners would be hooked from the get-go.

My YouTube Channel:

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    11 Discussions


    Question 1 year ago on Step 2

    hello, I found changing line 20 to 2050 gave me one revolution, yet I have the stepper motor model described. when I set it to 4096 it got confused and did not move.

    Gilang Ridho

    1 year ago

    Same as my 28BYJ-48 stepper motor, it requires the number of steps in one revolution (48 steps) to be multiplied by another 48 in order to rotates either one revolution or close to it (i saw that sometimes the stepper motor rotates a bit more than one revolution or a bit less). Don't know if i use another stepper motor (i.e. NEMA 17 type), i hope this problem doesn't occur.


    1 year ago

    It hadn't occurred to me that the code and the wiring chart with the off-brand stepper motor controller I was using might be out of synch – I just thought they were broken.

    Thanks for clearing some of these things up and encouraging me to be a little less trusting that out-of-the box code is bug-free, even when it was specifically shippex with a product. It doesn't matter who is at fault; I learned something today, and for that I say thank you!


    3 years ago

    1] You are not being reasonable complaining about Arduino code not working with the stepper hardware you bought. The onus should be with the hardware supplier to ensure compatibility or advise changes (wiring or code) to enable their hardware to work with Arduino.

    2] Half stepping doubles the number of steps. It does not square it. There is also a system called micro-stepping that significantly increases the resolution. You might correct your article to avoid misleading beginners.

    6 replies

    Reply 2 years ago

    sir do you think it is reasonable for you to say that a three dollar stepper motor hardware supplier to provide wiring diagrams and /or code to ensure motor to work with arduino rather than the arduino manufacturers who must be interested in more people successfully use motors and arduino board and start liking the board?.


    Reply 2 years ago

    Arduino have set a standard and given examples and made them public and freely available. It is ridiculous to expect them to change their standard because a third party manufacturer is stupid/ignorant enough to make a device that does not comply. If Arduino were to change their standard then the good suppliers whos products do comply would then not work. There is always the option for any supplier to make a new device and supply a library and examples as to how it can work. Arduino have gone to some lengths to show how this can be done. The onus is on any supplier who says their product works with Arduino to make sure that it does, and if a change in standard is required to provide the necessary information. Your suggestion is like expecting Ford to change their wheel sizes because someone has come up with a new tyre size. I hope this arguement makes sense. Mike


    Reply 2 years ago

    Thanks for this! It made the sketches work of course, but there is one thing I don't understand. You said that the resolution had to be changed to 4096 but I had to set it to 2048 to get it to work at one full revolution. At 4096 nothing happens. Is 4096 a mistake?


    Reply 2 years ago

    hi, 4096 is ok if you go half-step, 2048 is the correct number if you go normal (single-step) or double-step


    Reply 2 years ago

    You should direct this question to the author. If you want to look at a stepper driver that works, accurately and over a wide range and has good explanation of the code then search on instructables for 'Simple, Low Cost and Accurate Stepper Motor Driver' Mike


    2 years ago

    the truth of the matter, well said.

    Arduino .# listen up ,