loading

Step 6: Stepper Motor

There are a number of different types of stepper motors, but in this tutorial we will specifically be addressing bipolar stepper motors. Bipolar stepper motors typically have 4 pins, which correspond to two coils. To use a stepper, you need to power these two coils in phase with alternating polarity.

To figure out which two pins make up a single coil, insert an LED into any two pins on its socket and rotate the motor shaft. If the LED lights up, you found one coil. The other two pins should make up the other coil.

To make a bipolar motor spin:
  1. Power the first coil.
  2. Next power the second coil with reverse polarity.
  3. Then power the first coil with reverse polarity.
  4. Finally, power the second coil.
To reverse the motor direction of a bipolar stepper, simply reverse the polarity of the second coil.

Here is code to make the stepper spin in one direction:

Here is code to make the stepper spin in the opposite direction:

<p>Can you help me. i have an OSEPP Motor and Servo sheild that is Arduino compatiable but i have no idea how to work it...any thoughts?</p>
<p>Thanks for the tutorial, I made it and it was great! I also like the way you put in the code meaning and code.</p>
i am using arduino adafruit motor control shield V1 ,for controlling 4 12v dc motors....but i am confused about the power supply for the motor shield as i read on the manual of adafruit fruit that 9v supply will not work(why no idea)!! what sort of power supply other than lipo can i supply?? and how much should i supply..for the 4 dc geared motors(12V,300rpm)
<p>A different power supply and some relays might prove useful. Note the relays are to be controlled by the board but the 12 V power source should not be grounded to the board itself. </p>
<p>I followed the instructions, and it worked fine. When I installed a remote control, the receiver did not pick up any signal or pick up wrong signal when the motor is spinning. When the motor is not spinning, it could pick up signal and everything worked. This is the same effect as of before installing the motorshield. Does anyone else have this problem?</p>
to which pin , you connected the IR reciever??
For the stepper motor code, what is the difference between the two versions that makes them spin the motor in opposite directions? The two code sets look the same to me, but since I am just starting with this, I can easily be overlooking the critical difference. And is &quot;delaylegnth&quot; supposed to be &quot;delaylength&quot; (as I said, just starting, so I am not familiar with the syntax quirks). Thanks!
delaylegnth is a variable, so it was defined (misspelled) by me. If you correct one instance of the spelling, just be sure to correct all of them in the code. <br /><br />The difference between the two is the direction of Channel B in each of the codes. Look at the difference of sequence for: digitalWrite(13, HIGH); //Sets direction of CH B
<p>hi great project</p><p>+_____+_________+_________+__________+__________+_________+</p><p>i have a problem with my external power supply</p><p>i tried everything, from disconnecting and connecting the bridge under the board right below the VIN pin i tried using a 9v jack i connected the 9v battery to the arduino's VIN pin directly.</p><p>============</p><p><strong><em>NO MATTER WHAT I TRY IT ONLY WORKS WITH THE USB POWER SUPPLY</em></strong></p><p>============</p><p>to make matters worse, i bought another one yet still no use i borrowed two of them from my friend yet STILL no use</p><p>============</p><p>im deeply in need of you</p><p>============</p><p>P.S i know how to program it really good my problem lies on the power supply </p><p> cheers</p><p> mwnllt</p><p>+___________+__________+___________+_____________+__________+______+</p>
<p>WOW,</p><p>just to the point of giving up I FINNALY got it.</p><p>all that it needed was amps.</p><p>I was using an lakline 9V battery which supplied 500mAh, which of course wasnt enough..</p><p>so i bought a couple of rechargeable batteries and VOLA it worked</p><p>keep in mind that those rechargeable batteries were 2200mAh @ 1.2 volts x5 so that was enough to get those motors moving!! </p><p>thanks anyways and great project.</p><p>cheers.</p>
<p>I do not speak English. friend you could upload some pictures of the connection to external source. as is the connection of the bridge. Thank you</p>
<p>u have to use a regular battery pack for the arduino itself, then an extra 9v battery.</p>
Now that you have pointed it out, the difference between the versions is obvious. Thank you!
<p>Harshvardhan and Javier I think both of your code will not work as in both of your codes you have chosen pins 2,3,4,5 which actually are not PWM pins so your code will not work as analogWrite function only works with the PWM pins</p>
<p>is this code ok to drive 2 motors front right back and left.</p><p>void setup() {<br>pinMode (2,OUTPUT);//RIGHT MOTOR<br>pinMode (3, OUTPUT);//RIGHT MOTOR<br>pinMode (4,OUTPUT);//LEFT MOTOR<br>pinMode (5, OUTPUT);//LEFT MOTOR<br>}</p><p>void loop() {<br> //front <br> digitalWrite (2, LOW);//RIGHT MOTOR<br> analogWrite (3, 250);//RIGHT MOTOR<br> digitalWrite (4, LOW);//LEFT MOTOR<br> analogWrite (5, 250);//LEFT MOTOR<br> //left<br> digitalWrite (2, LOW);//RIGHT MOTOR<br> analogWrite (3, 150);//RIGHT MOTOR<br> digitalWrite (4, HIGH);//LEFT MOTOR<br> analogWrite (5, 105);//LEFT MOTOR<br> //back<br> digitalWrite (2, HIGH);//RIGHT MOTOR<br> analogWrite (3, 125);//RIGHT MOTOR<br> digitalWrite (4, HIGH);//LEFT MOTOR<br> analogWrite (5, 125);//LEFT MOTOR<br> //right<br> digitalWrite (2, HIGH);//RIGHT MOTOR<br> analogWrite (3, 105);//RIGHT MOTOR<br> digitalWrite (4, LOW);//LEFT MOTOR<br> analogWrite (5, 150);//LEFT MOTOR<br>}</p>
<p>Hi everybody,</p><p>I have a Arduino uno connect to a Arduino Motor Shield r3, and i am trying to control a stepper motor from Maxuino (this one http://www.gotronic.fr/pj-784.pdf).</p><p>I have been following the <a href="https://vimeo.com/digitalcoleman" rel="nofollow">Chris Coleman</a> tutorial on vimeo but i might have a problem with the setting of the pin.</p><p>From the arduino software, this code works:</p><p>int delaylegnth = 30;</p><p>void setup() {</p><p> //establish motor direction toggle pins</p><p> pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???</p><p> pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???</p><p> //establish motor brake pins</p><p> pinMode(9, OUTPUT); //brake (disable) CH A</p><p> pinMode(8, OUTPUT); //brake (disable) CH B</p><p>}</p><p>void loop(){</p><p> digitalWrite(9, LOW); //ENABLE CH A</p><p> digitalWrite(8, HIGH); //DISABLE CH B</p><p> digitalWrite(12, HIGH); //Sets direction of CH A</p><p> analogWrite(3, 255); //Moves CH A</p><p> delay(delaylegnth);</p><p> digitalWrite(9, HIGH); //DISABLE CH A</p><p> digitalWrite(8, LOW); //ENABLE CH B</p><p> digitalWrite(13, LOW); //Sets direction of CH B</p><p> analogWrite(11, 255); //Moves CH B</p><p> delay(delaylegnth);</p><p> digitalWrite(9, LOW); //ENABLE CH A</p><p> digitalWrite(8, HIGH); //DISABLE CH B</p><p> digitalWrite(12, LOW); //Sets direction of CH A</p><p> analogWrite(3, 255); //Moves CH A</p><p> delay(delaylegnth);</p><p> digitalWrite(9, HIGH); //DISABLE CH A</p><p> digitalWrite(8, LOW); //ENABLE CH B</p><p> digitalWrite(13, HIGH); //Sets direction of CH B</p><p> analogWrite(11, 255); //Moves CH B</p><p> delay(delaylegnth);</p><p>}</p><p>But in Maxuino it doesn't works. The lights on the motor shield board arent moving the same way, as if the pins wasn't setting up correctly, but i can't found how to solve it.</p><p>It is kind of an emergency, i'm trying to make it work for friday. If someone has any idea that could help me to understand whats going on, i will be glad to read it. Thanks in advance!</p><p>Pierre</p>
<p>I m new in Arduino and i m trying to make. i m using this motor controler can you plz help me how to connect it with arduino and dc motor.??</p><p>plz reply..</p>
<p>i think the adafruit motor shield library would work fine for that, and they have a great website w/ tutorials (adafruit.com), but if it doesnt work just youtube or google sainsmart arduino motor shield tutorial</p>
I am stuck with the same problem
<p>go to adafruit.com </p>
<p>it is theirs</p>
<p>I would like to know if i want to drive 4 motor, can i buy 2 motor shield and add those 2 shield togehter on arduino uno board??</p>
<p>yes u could, but you couldnt drive each of the four motors separately, only two at a time.</p>
<p>If your device requires more current and you are feeling rich you can just piggyback another motor shield on top and and join the connection block together.<br></p>
<p>HI ALL....NEED HELP..THIS LOOKS LIKE THE HARDWARE I NEED..BUILDING A MODEL TRAIN TURNTABLE..WANTING TO PUT IN 10-16 STOPS CONTROLLED BY MOMENTARY PB..USING STEPPER MOTOR DIRECT CONNECTION..IT LOOKS LIKE THE ARDUINO AND MOTOR SHIELD ARE WHAT I NEED PLUS ???..TO CONTROL STOPS..SAY TO GO FROM NUMBER 6-10 .WITHOUT MAKING COMPLETE REVOLUTION..ANY HELP,I WOULD BE GREATFUL..THANKS RON..PS..I TYPE IN LARGE..EYES NOT SO GOOD..</p>
<p>You may try using &quot;<a href="https://processing.org/" rel="nofollow">https://processing.org/</a>&quot; which is an advanced arduino based PDE file writing app. If you are up to the programming aspects it should be able to allow you to create what you need. larystoy@yahoo.com</p>
<p>Did you ever get it figured out? This is what I'm trying to do as well.</p><p>train459 [at] gmail</p>
Podria alimentar el shield con 24 voltios para un canal a 2 amp para usar un solo motor. b_ playa1@hotmail.com muchas gracias
<p>Does it matter what order you connect the two coils? What if you reverse the two wire to one of the coils, will it still work?</p>
<p>For that matter, how do you connect a uni-polar stepper? Where does the fifth wire go?</p>
<p>I have a arduino motor shield, and I was using it with my arduino uno rev3.This birthday I bought a arduino mega 2560r3. So I just wanted to know if it would work with my shield</p>
<p>I have a arduino motor shield, and I was using it with my arduino uno rev3.This birthday I bought a arduino mega 2560r3. So I just wanted to know if it would work with my shield</p>
<p>What if i want to control 4 motors? what would be the possible solutions? thank you. :)</p>
<p>Hi there,</p><p>Thanks for this instructable, very good and clear</p><p>I have a problem with the power.</p><p>I have 2 motor, the arduino uno + motorshield v3</p><p>When i'm in USB, no need of external power, everything works.</p><p>If i put USB + shield external power -&gt;</p><p>But as soon as i unplug USB, no matter what, nothing works:</p><p>9v External Uno + 9v external motor shield -&gt; nothing happen</p><p>is there some specific wiring to do when using 2 external power supply?</p><p>Thanks again for your help</p>
<p>Hi </p><p>Do you have a solution and a code to control these two motors dc with an analog Joystick like <a href="http://www.robotshop.com/eu/fr/mini-capteur-manche-cytron.html" rel="nofollow">http://www.robotshop.com/eu/fr/mini-capteur-manche...</a></p><p>Thank you for your help</p>
<p>I am also looking for a solution to this. Please let me know if you find out.</p><p>Thanks </p>
<p>I find a system by software for can control one unipolar stepper motor using one Arduino Motor Shield R3</p><p><br>I use this and there are an example of 3 unipolar stepper motors using 3 Arduino Motor Shield R3 into my free APP &quot;Arduino CNC Controller&quot; I put instructions and pictures explain how to connect 3 boards, I use one Arduino Mega 2560 R3</p><p>Really there are 2 examples, the first example is using 3 bipolar stepper motors, that is normal example, and second example is using 3 unipolar/monopolar stepper motors.</p><p><br>Is free on Google Play: &quot;Arduino CNC Controller&quot;</p><p>https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller</p><p><a href="https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller" rel="nofollow">https://play.google.com/store/apps/details?id=net....</a><br><br>This APP have a Window of &quot;Help and documentation&quot; in witch you can read this source code example.<br><br>Or you can download on:</p><p><a href="http://sourceforge.net/projects/arduinocnccontroller/files/" rel="nofollow">http://sourceforge.net/projects/arduinocnccontroll...</a></p>
<p>I find a system by software for can control one unipolar stepper motor using one Arduino Motor Shield R3</p><p><br>I use this and there are an example of 3 unipolar stepper motors using 3 Arduino Motor Shield R3 into my free APP &quot;Arduino CNC Controller&quot; I put instructions and pictures explain how to connect 3 boards, I use one Arduino Mega 2560 R3</p><p>Really there are 2 examples, the first example is using 3 bipolar stepper motors, that is normal example, and second example is using 3 unipolar/monopolar stepper motors.</p><p><br>Is free on Google Play: &quot;Arduino CNC Controller&quot;</p><p>https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller</p><p><a href="https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller" rel="nofollow">https://play.google.com/store/apps/details?id=net....</a><br><br>This APP have a Window of &quot;Help and documentation&quot; in witch you can read this source code example.<br><br>Or you can download on:</p><p><a href="http://sourceforge.net/projects/arduinocnccontroller/files/" rel="nofollow">http://sourceforge.net/projects/arduinocnccontroll...</a></p>
<p>I find a system by software for can control one unipolar stepper motor using one Arduino Motor Shield R3</p><p><br>I use this and there are an example of 3 unipolar stepper motors using 3 Arduino Motor Shield R3 into my free APP &quot;Arduino CNC Controller&quot; I put instructions and pictures explain how to connect 3 boards, I use one Arduino Mega 2560 R3</p><p>Really there are 2 examples, the first example is using 3 bipolar stepper motors, that is normal example, and second example is using 3 unipolar/monopolar stepper motors.</p><p><br>Is free on Google Play: &quot;Arduino CNC Controller&quot;</p><p>https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller</p><p><a href="https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller" rel="nofollow">https://play.google.com/store/apps/details?id=net....</a><br><br>This APP have a Window of &quot;Help and documentation&quot; in witch you can read this source code example.<br><br>Or you can download on:</p><p><a href="http://sourceforge.net/projects/arduinocnccontroller/files/" rel="nofollow">http://sourceforge.net/projects/arduinocnccontroll...</a></p>
<p>I find a system by software for can control one unipolar stepper motor using one Arduino Motor Shield R3</p><p><br>I use this and there are an example of 3 unipolar stepper motors using 3 Arduino Motor Shield R3 into my free APP &quot;Arduino CNC Controller&quot; I put instructions and pictures explain how to connect 3 boards, I use one Arduino Mega 2560 R3</p><p>Really there are 2 examples, the first example is using 3 bipolar stepper motors, that is normal example, and second example is using 3 unipolar/monopolar stepper motors.</p><p><br>Is free on Google Play: &quot;Arduino CNC Controller&quot;</p><p>https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller</p><p><a href="https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller" rel="nofollow">https://play.google.com/store/apps/details?id=net....</a><br><br>This APP have a Window of &quot;Help and documentation&quot; in witch you can read this source code example.<br><br>Or you can download on:</p><p><a href="http://sourceforge.net/projects/arduinocnccontroller/files/" rel="nofollow">http://sourceforge.net/projects/arduinocnccontroll...</a></p>
<p>I find a system by software for can control one unipolar stepper motor using one Arduino Motor Shield R3</p><p><br>I use this and there are an example of 3 unipolar stepper motors using 3 Arduino Motor Shield R3 into my free APP &quot;Arduino CNC Controller&quot; I put instructions and pictures explain how to connect 3 boards, I use one Arduino Mega 2560 R3</p><p>Really there are 2 examples, the first example is using 3 bipolar stepper motors, that is normal example, and second example is using 3 unipolar/monopolar stepper motors.</p><p><br>Is free on Google Play: &quot;Arduino CNC Controller&quot;</p><p>https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller</p><p><a href="https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller" rel="nofollow">https://play.google.com/store/apps/details?id=net....</a><br><br>This APP have a Window of &quot;Help and documentation&quot; in witch you can read this source code example.<br><br>Or you can download on:</p><p><a href="http://sourceforge.net/projects/arduinocnccontroller/files/" rel="nofollow">http://sourceforge.net/projects/arduinocnccontroll...</a></p>
<p>I find a system by software for can control one unipolar stepper motor using one Arduino Motor Shield R3</p><p><br>I use this and there are an example of 3 unipolar stepper motors using 3 Arduino Motor Shield R3 into my free APP &quot;Arduino CNC Controller&quot; I put instructions and pictures explain how to connect 3 boards, I use one Arduino Mega 2560 R3</p><p>Really there are 2 examples, the first example is using 3 bipolar stepper motors, that is normal example, and second example is using 3 unipolar/monopolar stepper motors.</p><p><br>Is free on Google Play: &quot;Arduino CNC Controller&quot;</p><p>https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller</p><p><a href="https://play.google.com/store/apps/details?id=net.sourceforge.arduinocnccontroller" rel="nofollow">https://play.google.com/store/apps/details?id=net....</a><br><br>This APP have a Window of &quot;Help and documentation&quot; in witch you can read this source code example.<br><br>Or you can download on:</p><p><a href="http://sourceforge.net/projects/arduinocnccontroller/files/" rel="nofollow">http://sourceforge.net/projects/arduinocnccontroll...</a></p>
<p>hi,</p><p>Thank you for that great tuto ! </p><p>Could you please help me ? I made my own bot (2 sensors light to A3 and A4, 2 motors on A and N). It's ok and work fine. But as soon as i unplug usb cable, motor shield starts blinking (i tried 9V to the power of the shield, 12V too; i tried to plug a battery to the shield AND to the arduino... Nothing. Motors don't work. If i plug just one motor, that's ok. What could be the problem ? Is the shield broken ? It works fine with USB,..</p><p>Thanks for all that great stuff.</p>
<p>Thank you Sir R<a href="http://www.instructables.com/member/randofo/" rel="nofollow">a</a>ndofo!</p><p>This is what I really need, I hope you can also give us sample of 2 L293D and 4 motors running in one Arduino. </p>
<p>I have a question, This code, delaylegnth = delaylegnth - 1, makes the delaylegnth decrease by 1 everytime the loop repeats. How would I make the delaylegnth decrease by .1?</p>
<p>Nevermind, i figured it out. Just use 'delayMicroseconds' instead of 'delay'</p>
<p>Oh and BTW, awesome instructable! Helped a ton.</p>
<p>hello, can i have a code for 2 dc motor control? none of those i have works, how can i know that the shield works?</p>

About This Instructable

717,088 views

689 favorites

License:

Bio: My name is Randy and I founded the Instructables Design Studio. I'm also the author of the books 'Simple Bots,' and '62 Projects to ... More »
More by randofo: Hard Candy Recipe 2-Ingredient Candy Teeth Candy
Add instructable to: