Arduino: Stepper Motor Example Sketch Fixed
Intro: 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 ProblemSTEP 1: The Problem
The original code is shown here:
/*
Stepper Motor Control - one revolutionThis 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
*/
#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, 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 resolutionSTEP 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 KillsSTEP 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(60);
to
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 linksSTEP 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: https://www.youtube.com/channel/UCw0RquafdM8U-0hfq...
14 Comments
batvanio 1 year ago
https://www.instructables.com/%D0%9F%D1%83%D1%81%D0%BA%D0%B0%D0%BD%D0%B5-%D0%9D%D0%B0-20BYJ-46-12V-%D0%A1%D1%82%D1%8A%D0%BF%D0%BA%D0%BE%D0%B2-%D0%9C%D0%BE%D1%82%D0%BE%D1%80-Run-20BYJ-46/
janowatzki 3 years ago
phantomrose1999 3 years ago
Astacopsis 5 years ago
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 6 years 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.
MattH325 6 years ago
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!
FlyingDrMike 7 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.
eszjm 6 years ago
additional to "There is also a system called micro-stepping ..."
and there is also an system: geared stepper ... Step mode + Gear ratio
http://42bots.com/tutorials/28byj-48-stepper-motor...
https://www.instructables.com/id/Arduino-Stepper-D...
dvrrao 7 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?.
FlyingDrMike 7 years ago
voxcontinental 7 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?
LorisT1 6 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
FlyingDrMike 7 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
Solderman39 6 years ago
the truth of the matter, well said.
Arduino .# listen up ,