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:
```/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan

https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

*************************************************************/

int delaylegnth = 30;

void setup() {

//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???

//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B

}

void loop(){

digitalWrite(9, LOW);  //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, HIGH);   //Sets direction of CH A
analogWrite(3, 255);   //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH);  //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, LOW);   //Sets direction of CH B
analogWrite(11, 255);   //Moves CH B

delay(delaylegnth);

digitalWrite(9, LOW);  //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, LOW);   //Sets direction of CH A
analogWrite(3, 255);   //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH);  //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, HIGH);   //Sets direction of CH B
analogWrite(11, 255);   //Moves CH B

delay(delaylegnth);

}
```

Here is code to make the stepper spin in the opposite direction:
```/*************************************************************
Motor Shield Stepper Demo
by Randy Sarafan

https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/

*************************************************************/

int delaylegnth = 30;

void setup() {

//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???

//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B

}

void loop(){

digitalWrite(9, LOW);  //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, HIGH);   //Sets direction of CH A
analogWrite(3, 255);   //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH);  //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, HIGH);   //Sets direction of CH B
analogWrite(11, 255);   //Moves CH B

delay(delaylegnth);

digitalWrite(9, LOW);  //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, LOW);   //Sets direction of CH A
analogWrite(3, 255);   //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH);  //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, LOW);   //Sets direction of CH B
analogWrite(11, 255);   //Moves CH B

delay(delaylegnth);

}
```

<p>Why is it the digitalWrite is specifying 12, 9 and 3?</p>
<p>This is working. Thanks for this example. </p>
<p>Excellent tutorial, thank you. I just salvaged a stepper motor from an old scanner and few DC motors. Looking forward to use them in a new project.</p>
<p>hie everyone! i have a problem ... i need help in arduino project .i am using lcd shield and stepper motor at the same time but the problem is that these both device had the common pin pin 8 and 9 on arduino board can i change the pin of stepper motor to used other pin </p><p>mail id <br>aqeelkhan4772@gmail.com</p>
<p>Hi,</p><p>I am working on an arduino system using a stepper motor (4SM42HT38-1684A) and a motor shield. I wanted to make the motor turn in one direction, stop and then turn in the other directions. I used the two codes above in two loops but the motor is still going in the same direction (i am sure that the 2nd loop is running but it is still the same direction). Could you help me please ? Is there anything to adapt in theses codes to make them run together ?</p><p>Thanks a lot,</p><p>Regards,</p><p>Adele </p>
<p>Hi<br>Thankyou for your tutorial.<br><br>I am trying to drive a stepper motor 42SH47 - M RS No 535-0401.<br>I have cut the vin link and supplied the motor separately.<br><br>Using the code to move in one direction , I find that the motor is oscillating rather than rotating in one direction only.<br><br>Do you have any suggestions as to what I might be doing wrong ?<br><br>Thanks for your time<br><br>regards<br><br>M</p>
<p>The motor does not have a V-in link. It should have 4 wires. These comprise two separate pairs of motor drive coils which need to be powered in sequence. Each pair needs to be correctly attached to each channel.</p>
Hi<br><br>I decided to up the voltage to the motor. <br>When I got to 3.3 V it started to rotate correctly.<br>The manufacturer states 2.8V supply!!<br><br>I will contact the manufacturer to see if we can explain!.<br>Thanks again for your help and the article.<br><br>Regards<br><br>mark
<p>nice article man it helped me a lot.... sweet and short...</p><p>will you able to explain other pins too (OUT6 OUT5 etc.,)</p>
<p>I HAVE A DOUBT .WHEN I CONNECT MY MOTORS TO CHANNEL 'B'.IT WORKS BUT MY ARDUINO BOARD BELOW ,THE LED WHICH IS CONNECTED TO ON BOARD PIN 13 ALSO GOES ON AND OFF DEPENDING ON MY MOTOR CONTROL.IS THIS A PROBLEM.HOW CAN I SPLVE IT.</p>
<p>Just one more question, servos will also work the same way right?</p>
<p>No. Servos work differently than DC motors. They need a control signal.</p>
<p>Can one also put another shield on top of the first one to support 4 dc motors?</p>
<p>I don't know if that would work. <br><br>However, if that worked at all, you would end up with two sets of synchronized motors. Basically, each channel on each shield would end up being controlled the same.</p>
<p>ok, thanks!</p>
Thanks for the help:) this is such a good tutorial! It really made it clear what you can do with this shield. I have been searching all over the internet and have only found incomplete tutorials. Typos aside, this is a great tutorial! (sorry for typos, I'm swedish...
<p>const int motorPin1 = A0;</p><p>const int motorPin2 = A3;</p><p>void setup()</p><p>{</p><p> pinMode(motorPin1, HIGH);</p><p> pinMode(motorPin2, LOW);</p><p>}</p><p>void loop()</p><p>{</p><p> analogWrite(motorPin1, 255);</p><p> analogWrite(motorPin2, 254);</p><p>}</p><p>can it work for full speed</p>
<p>Thanks for the Instructable! I noticed a little typo in your code, thought I'd let you know. </p><p>digitalWrite(9, HIGH); //Engage the Brake for Channel A<br>digitalWrite(9, HIGH); //Engage the Brake for Channel B</p><p>Should be pin 8 for Channel B. Just in case anyone copied and pasted and is getting weird results. :)</p>
<p>Nice instructable. I really like the idea of connecting an LED to the stepper motor to figure out what pins belong to the same coil.</p>
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 there,</p><p>I know this is a long shot that you'll even see this but I tried both codes (for both directions) and they both spin the motor in the same direction...</p><p>I've been trying hard to understand what I should do, but there is little to no documentation supporting the motor shield. Help?! :)</p>
<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>Hey hey, try someone to use this motorshield with arduino duemilanove? If yes can you help me with it? I am beginner.</p><p>Thanks a lot!</p>
<p>How would you go about using the code above with a switch to stop the stepper motor kind of like a endstop.</p><p>so far I have it stopping but it will stop after the loop is completed, is there a way to stop it right away?</p><p>cheers</p><p>Dave</p>
<p>Will this work on the Arduino Mega 2560 R3? I want to use this in a obstacle avoiding robot.</p><p>And the link to the motor shield is broken</p>
I want my motor to move in all direction. and also want to stop and start it any time not fixed I am connecting arduino with bluetooth module so I want codes for 2motors <br><br><br><br>can any one help me<br>
I have ordered same motor driver shield for my project<br>I have a doubt that can we move 2 motors and 1 stepper sinultaneously???<br>
<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??
<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>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>