# Arduino Motor Shield Tutorial

## 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

http://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

http://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);

}
```

Remove these ads by Signing Up
Tranber7223 days ago

Hi

Do you have a solution and a code to control these two motors dc with an analog Joystick like http://www.robotshop.com/eu/fr/mini-capteur-manche...

triumphtotty1 year ago
This is exactly what I was looking for! I need to run a motor from a TIMER so that other code (serial commands) can be processed, so stripping your code down and reworking it into an ISR routine worked like a charm.

Thanks!

tt
lumia_berkek1 year ago
thanks it was great for me , it works boss
freefloater1 year ago
does this also work for a solenoid? I need it to spin in each direction.
AntonioLopez2 years ago
Why can't I see my pictures on my post?
AntonioLopez2 years ago
Hi, I have the same Arduino and same motor shield. I uploaded the sketch, but I can't make it do anything, and I mean anything. Do I have to change the serial port which is five now?

thanks.......
thedanger2 years ago
Great info, I have been wanting to do something with arduino, anything really. I ended up getting an uno and motor shield. But I'm new to all of this, and there is very little on the motorshield as far as....lets say , make it do something. I followed your instructable and got this stepper up and running first try. One question though, how can i control the rpms/speed, and can i just tell it to take "x" amount of steps and then reverse the same number back?
firephotoputer2 years ago
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 "delaylegnth" supposed to be "delaylength" (as I said, just starting, so I am not familiar with the syntax quirks). Thanks!
randofo (author)  firephotoputer2 years ago
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.

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
2 years ago
Now that you have pointed it out, the difference between the versions is obvious. Thank you!