loading

Step 7: Using a Stepper Motor with an Arduino

Working with the Arduino coding platform, there is a wide range of example libraries to get you started. Connecting a stepper motor to an Arduino is a different than connecting a DC motor to the board. Because stepper motors have to pulse in a specific way for the rotor to spin, there is a special Stepper library and function built into the Arduino code platform. You can read more about it here.


Opening the Arduino software, browse to "File>Examples>Stepper>stepper_oneRevolution"

This program drives a unipolar or bipolar stepper motor, by attaching the motor to digital pins 8 - 11 of the Arduino. After the sketch is loaded on to the Arduino board, the motor should revolve one revolution clockwise, then one revolution moving counter-clockwise.

The example code is an excellent point to start from, you can certainly make edits to the sketch to suit your needs. The delay is listed in microseconds, so if you want there to be no break between it's revolutions, you can set delay to delay(10). Or if you want it to spin for a long time you can change stepsPerRevolution to equal = (1000000) 

How you modify the sketch will depend on what you are trying to accomplish with the motor. Playing with some of the other sketches in the example sketch library will help you develop a greater understanding of how stepper motors are able to communicate with Arduino Boards.

Thanks.<br>
<p>do servos have noise issues like motors do? and are there low limits on current that the arduino can supply to drive them i.e. what servos suit?</p>
<p>thanks! i did it with a 1m pot., before uploading the code, the servo swept. some cracking sound were there. its okay i guess</p>
<p>You're an inspiration with excellent support. Something else that may be of interest is the control of the steppers on V2 shields though a sensor. I needed such a program and I have attached a compiled sketch below:</p><p>#include &lt;Wire.h&gt;#include <br>&lt;Adafruit_MotorShield.h&gt;#include <br>&quot;utility/Adafruit_PWMServoDriver.h&quot;<br> <br>#Define PotIn = 0</p><p>// Create the motor shield object with <br>the default I2C addressAdafruit_MotorShield <br>AFMS = Adafruit_MotorShield(); // <br>Or, create it with a different I2C address (say for stacking)// <br>Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60); <br> <br>// <br>Connect a stepper motor with 200 steps per revolution (1.8 degree)// <br>to motor port #1 (M1 and M2)</p><p>// Connect a stepper motor with 200 steps <br>per revolution (1.8 degree)// <br>to motor port #2 (M3 and M4)Adafruit_StepperMotor <br>*myMotor1 = AFMS.getStepper(200, 1);Adafruit_StepperMotor <br>*myMotor2 = AFMS.getStepper(200, 2);</p><p>int potinPin = (A0); //assigns pin <br>for potentiometer readingsint <br>stepsPin = 2;//assigns pin for stepper motor steps</p><p>int previous = 0;//to store initial potentiometer values</p><p>int Val = 0;//to store new potentiometer values</p><p>int stepsValue = 0;</p><p>int potinValue = 0;</p><p>void setup() { Serial.begin(9600);</p><p> // <br>set up Serial library at 9600 bps</p><p>AFMS.begin(); // create with the default <br>frequency 1.6KHz <br>//AFMS.begin(1000); // OR with a <br>different frequency, say 1KHz <br>myMotor1-&gt;setSpeed(10); // 10 rpm</p><p>myMotor2-&gt;setSpeed(10);// 10 <br>rpm }void <br>loop() {potinValue = analogRead(potinPin); //Reads sensor <br>stores first value</p><p>previous = map(potinValue, 0, 1023, 0, 544); //Turns ohms into steps</p><p>delay (500);</p><p>potinValue <br>= analogRead(potinPin); //Reads new sensor stores value</p><p>val = map(potinValue, 0, 1023, 0, 544); //Turns ohms into steps</p><p>// <br>conditional statements determining movement and direction </p><p>if(val <br>== previous){// if potentiometer didn&rsquo;t <br>register movement </p><p>stepsValue <br>= 0; // steps equal zero</p><p>}</p><p>else <br>if( val &gt; previous){//if potentiometer moves clockwise</p><p>stepsValue <br>=(val &ndash; previous); //steps move motor forward microsteps </p><p>myMotor1-&gt;step((stepsValue),FORWARD,MICROSTEPS);</p><p>myMotor2-&gt;step((stepsValue),FORWARD,MICROSTEPS);</p><p>}</p><p>else(val <br>&lt; previous){ //if potentiometer <br>moves counterclockwise </p><p>stepsValue <br>=(previous &ndash; val); // steps move motor backward microsteps</p><p>myMotor1-&gt;step(stepsValue),BACKWARD,MICROSTEPS);</p><p>myMotor2-&gt;step(stepsValue),BACKWARD,MICROSTEPS);</p><p>}</p><p>}</p>
<p>You're and inspiration. Something else that may be of interest is the control of the stepper motors on the V2 motor shields by a sensor. I needed such a program and wrote the sketch below:</p><p>#include &lt;Wire.h&gt;#include <br>&lt;Adafruit_MotorShield.h&gt;#include <br>&quot;utility/Adafruit_PWMServoDriver.h&quot;<br> <br>#Define PotIn = 0</p><p>// Create the motor shield object with <br>the default I2C addressAdafruit_MotorShield <br>AFMS = Adafruit_MotorShield(); // <br>Or, create it with a different I2C address (say for stacking)// <br>Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60); <br> <br>// <br>Connect a stepper motor with 200 steps per revolution (1.8 degree)// <br>to motor port #1 (M1 and M2)</p><p>// Connect a stepper motor with 200 steps <br>per revolution (1.8 degree)// <br>to motor port #2 (M3 and M4)Adafruit_StepperMotor <br>*myMotor1 = AFMS.getStepper(200, 1);Adafruit_StepperMotor <br>*myMotor2 = AFMS.getStepper(200, 2);</p><p>int potinPin = (A0); //assigns pin <br>for potentiometer readingsint <br>stepsPin = 2;//assigns pin for stepper motor steps</p><p>int previous = 0;//to store initial potentiometer values</p><p>int Val = 0;//to store new potentiometer values</p><p>int stepsValue = 0;</p><p>int potinValue = 0;</p><p>void setup() { Serial.begin(9600);</p><p> // <br>set up Serial library at 9600 bps</p><p>AFMS.begin(); // create with the default <br>frequency 1.6KHz <br>//AFMS.begin(1000); // OR with a <br>different frequency, say 1KHz <br>myMotor1-&gt;setSpeed(10); // 10 rpm</p><p>myMotor2-&gt;setSpeed(10);// 10 <br>rpm }void <br>loop() {potinValue = analogRead(potinPin); //Reads sensor <br>stores first value</p><p>previous = map(potinValue, 0, 1023, 0, 544); //Turns ohms into steps</p><p>delay (500);</p><p>potinValue <br>= analogRead(potinPin); //Reads new sensor stores value</p><p>val = map(potinValue, 0, 1023, 0, 544); //Turns ohms into steps</p><p>// <br>conditional statements determining movement and direction </p><p>if(val <br>== previous){// if potentiometer didn&rsquo;t <br>register movement </p><p>stepsValue <br>= 0; // steps equal zero</p><p>}</p><p>else <br>if( val &gt; previous){//if potentiometer moves clockwise</p><p>stepsValue <br>=(val &ndash; previous); //steps move motor forward microsteps </p><p>myMotor1-&gt;step((stepsValue),FORWARD,MICROSTEPS);</p><p>myMotor2-&gt;step((stepsValue),FORWARD,MICROSTEPS);</p><p>}</p><p>else(val <br>&lt; previous){ //if potentiometer <br>moves counterclockwise </p><p>stepsValue <br>=(previous &ndash; val); // steps move motor backward microsteps</p><p>myMotor1-&gt;step(stepsValue),BACKWARD,MICROSTEPS);</p><p>myMotor2-&gt;step(stepsValue),BACKWARD,MICROSTEPS);</p><p>}</p><p>}</p>
<p>does there is no need of driving ic like ULN etc. required with the uno Board to drive the stepper motor??</p>
<p>Hi, this tutorial is awesome. I've been looking for the stepper motor depicted in step 6 and 7. Can you please tell me the model number and (if you remember) where you bought it?</p>
<p>I ripped it out of an old printer - sorry.</p>
<p>Thanks a lot for your time dedicated to this short and clean tutorial</p><p>Now motors like this are not a mistery for me</p>
<p>Thank you for your interesting post. I am new to Arduino. bought a starter kit recently. Not even know how to start with yet.</p><p>Have bought a .</p><p>Stepper Motor Drive Controller Board Module L298N Dual H Bridge DC For<br>Arduino from<br><a href="http://www.ebay.com/itm/Stepper-Motor-Drive-Controller-Board-Module-L298N-Dual-H-Bridge-DC-For-Arduino-/181029044810?pt=LH_DefaultDomain_0&hash=item2a262c024a" rel="nofollow">http://www.ebay.com/itm/Stepper-Motor-Drive-Controller-Board-Module-L298N-Dual-H-Bridge-DC-For-Arduino-/181029044810?pt=LH_DefaultDomain_0&amp;hash=item2a262c024a</a>.</p><p>The supplier advised me that I can use this as stand alone to drive my </p><p>STEP MOTOR 28BYJ-48 5V DC</p><p>But failed.</p><p>please advise.</p><p>Regards</p>
I just want to make a device that runs a motor that will pull a string at a constant rate. It has to be a smooth pull, not a jerky one, and it needs to pull at a variable rate from like one inch per 3 seconds to maybe 2 inches per second. What kind of motor would you recommend? Would I really need to have an Aurduino board or could I hook up a battery directly to it somehow?
Thx for the great instruction! <br>but how come my arduino restarts when I turn the knob to fast? can this be solved or is something broken? it seems like when the servo moves to fast it restarts I'm not sure what's going on, hope to hear from you.
Huh. That's strage! I wonder why.... I imagine that it is incapable of processing analog data that quickly.
I built the &quot;world's simplest motor&quot; for my kids. They loved it. Thank you for the idea and the excellent read on motor theory. Good job!
This worked great for me.
this is one of the most comprehensive tutorial about how to use a <a href="http://www.intorobotics.com/tutorials-and-resources-to-control-stepper-motor-with-arduino-board/" rel="nofollow">stepper motor with arduino board</a>
Awesome illustration for a stepper motor!
This is a great 'able, Audrey. Thanks!
Thank you for your very clear explanation of the various motor types and the videos. I have saved it in my grandsons' &quot;favourites&quot; for the time when they will study this subject. I have already made them many toys using all these motor types - I favour PICAXE as I think BASIC is easier to learn for kids.
very nice, great info and put together well.
Hello. <br>I would be very happy if someone could help me where I can find servo motor. <br>I mean, I usually enjoy myself dismounting printers and scanners. And I have never seen a motor like that. <br> <br>att, <br> <br>Jolizo
Hobbyking is a good source.
While this is a good rig to experiment with and learn about stepper motors, be careful about the load you put on your Arduino. Steppers and DC Motors can send spikes of current back to the controlling circuitry when switching from on to off due to inductance, as well as possibly drawing more current than your Arduino is designed to supply. For more permanent projects you should get or build a motor control board to isolate the more delicate logic circuits.
A stepper motor seems to be a regular motor, except it's segments are separated, unlike a regular motor where the segments are wired in a series to make the motor run at a constant speed and no controller is needed other than the volts/amps. Now this may be very simplified, but would this be accurate?
DavAnt- thanks for that interjection, I was specifically looking to see if the TD62003AP was an acceptable Darlington substitute for the ULN2003. Appreciated!
<div class="media_embed"> <iframe frameborder="0" height="315" src="http://www.youtube.com/embed/ELB-XPDBOFI" width="560"></iframe><br> <br> Vibrating unit for modular robots :)&nbsp;<a href="http://www.instructables.com/id/How-to-build-your-Modular-Vibrating-Robot/" rel="nofollow">http://www.instructables.com/id/How-to-build-your-Modular-Vibrating-Robot/</a></div>
I new to complex circuits and arduino. Wat is arduino?. <br>And do u program arduino with a computer?<br>if yes - Which language ?<br><br>Plzz answer. <br><br> ----- curious beginner
Coolnits,<br> Arduino is a microcontroller (like a mini, programmable computer)<br> You program it using the Arduino IDE (the arduino program-writing software)<br> You can go to Arduinio.cc for a LOT of good information about the Arduino.<br> It is programmed in C / C++<br> <br> -Ryan
Coolnits, Arduino is a very powerful microcontroller which is based on an opensource platform. The website www.hackaday.com can fill you in. Seems to be ONE of the up and coming nextgen controllers. Free software downloads, free manuals and tutorials too! I have it on my wish list but have yet to actually try one or do any work with one, so I can't tell you much more other than it is based on Object Oriented Code. What's that? Dunno. Yet. I do get the Idea that it is quite popular with techies who dive deep. Good luck.
I got the link to this the day after I'd finally figured out how to get a stepper working with my Arduino.<br><br>Timing is everything. :)
You should use a ULN2003 IC to drive stepper motors using the arduino! If i am not wrong it is a darlington array..
Yes, I got a stepper out of an old UMAX scanner, and it is controlled through a Toshiba TD62003AP, which is a Darlington array. There are also some smd capacitors to smooth it out I guess
If you don't want to pay for a Arduino board and you know a little about electronics, you can build a stepper motor controller from a 555 timer IC, a 4017 decade counter IC and a ULN 2003 Darlington transistor array. To adjust the speed simply put a potentiometer ( the higher the resistance, the slower it goes) between pins 7 and 8 of the 555 timer IC. The direction can be changed using a DPDT switch from the output of the ULN2003. This works simplest when running 3 phase stepper motors which are commonly used in aviation. There isn't much information on the net about 3 phase stepper motors and how to run them, but I combined knowledge about the 3 ICs and many schematics.
I have to correct myself. To adjust the speed, put a potentiometer between pins 2 and 7 of the 555 timer IC.<br>
Where can you get the small axle?
This is a great starters tutorial for motors! It would be a perfect entry for the teachers-contest!
I Agree
Are you the Jeff Yates from (Paravet-Computers are here Co)<br>The inventor of Mr Salty water chlorinator and the Originater of the First Usuable Chineses Computer Keyboard. In which case please take credit for realising my basic theory with Barry to take the stepper motor to its 1,000 degree. If I remember right that took 6 months and $30,000 to realise, congradulations, Student 6 of 20 pass
nice presentation,because you have done so well and like experimenting.<br>History back in the eighties they wanted stepper motors to go beyond 1.5-3 degrees I offered a solution . by oscillating the fields with different wave lengths and forms,1,000 th of one degree was achievable using the same motors..Keep up the good work.
The brushes are just part of the commutator. They're the switch that keeps the coil at the right polarity to keep spinning. They don't propel anything.<br><br>Also I agree with Clemtasm. A constant voltage across an 'ideal' motor will give constant speed with varying load. In real motors the speed drops with higher load due mostly to the resistance of the windings.
When the load on the motor increases the current consumed by the armature will also increase hence the flux will&nbsp;increase. Speed in&nbsp;inversely&nbsp;proportional&nbsp;to flux. So the speed of the motor has to decrease with increase in load over a constant load. Even in case of an ideal DC motor. Please let me know if I am wrong.
It's been a long time since I've dealt with theoretical emag, so I'm not sure. In an ideal motor model with zero ohm windings (I think even in an ideal motor, sometimes you will account for winding resistance, depends who considers it ideal..) speed is strictly proportional to voltage and torque is strictly proportional to current.<br>In a simple motor model (where you do take winding resistance into account, but do not take reactivity or friction into account,) it's pretty easy to deal with the winding resistance. Speed will just be (Vin - Tout/Ka*r)*Kv. Or if you know V and I, just (Vin - I*r)*Kv.
This was great but how do i wire a 6 lead stepper motor to an arduino?
You need a Stepper motor driver circuit...
Hey, if you Stepper motor has that many leads, I am guessing that you will have to connect it to an additional power source. Do you know the model number of the Stepper? If you do, I recommend seeking out the datasheet on the motor so that you know how much power you will need.
It is a lin engineering 4118L-04 I believe it is a unipolar motor but i haven't been able to find its exact spec sheet
It's a unipolar stepper, Arduino has a special tutorial for it here:<br> <a href="http://www.arduino.cc/en/Tutorial/StepperUnipolar" rel="nofollow">http://www.arduino.cc/en/Tutorial/StepperUnipolar</a> All the chips needed for driving it along with the software to step through the windings is mentioned on that page
Be sure to use a motor driver... the Arduino is designed around an Atmel processor which can support a maximum of 20mA from a port, not to exceed 50mA for the entire chip. A typical stepper motor can be 2A which is 2000mA or 400% of what the chip is designed for.<br><br>4-wire Steppers are driven by alternating the flow through the coils forward/backward so you have two wires per coil. The 6-wire steppers are designed to have the center set to ground, then you energize the coil you want. The interface can be as simple as a 2N2222 transistor per coil. (Or larger MOSFET if more current is needed.) <br><br>a 6-wire stepper can also be driven as a 4-wire by not connecting the center lead and thereby using the entire coil. Again... use a bidirectional motor driver. (H-Bridge) to drive them.
&quot;Speed of the motor can be controlled by the amount of current reaching the coils from the battery to the commutator.&quot;<br><br>Should be changed to:<br><br>&quot;Speed of the motor can be controlled by the amount of voltage applied to the coils from the battery to the commutator.&quot;<br><br>Voltage is proportional to speed and current is proportional to torque.
The speed of a DC motor can be controlled by limiting the current supplied to the commutator. Speed and Torque are inversely proportional.

About This Instructable

369,681 views

959 favorites

License:

Bio: i work at instructables! i make things with technology, eat pho, and play synthesizers.
More by audreyobscura: When Hot Glue Isn't Glue Kinect Projects 3D Printed Cookie Cutters
Add instructable to: