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

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: