Introduction: 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

Step 1: The Problem

Picture of 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

Picture of 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.

Picture of 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

Picture of 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

Picture of 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:


FlyingDrMike (author)2016-09-25

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.

eszjm (author)FlyingDrMike2017-08-06

additional to "There is also a system called micro-stepping ..."

and there is also an system: geared stepper ... Step mode + Gear ratio

dvrrao (author)FlyingDrMike2016-11-05

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

FlyingDrMike (author)dvrrao2016-11-05

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

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?

LorisT1 (author)voxcontinental2017-07-26

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

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

Solderman39 (author)2017-07-21

the truth of the matter, well said.

Arduino .# listen up ,