Step 6: Stepper Motor

Picture of 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:

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)  firephotoputer3 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
Ofibro randofo2 months ago

hi great project


i have a problem with my external power supply

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.




to make matters worse, i bought another one yet still no use i borrowed two of them from my friend yet STILL no use


im deeply in need of you


P.S i know how to program it really good my problem lies on the power supply




Now that you have pointed it out, the difference between the versions is obvious. Thank you!
Train456 months ago

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?

Train45 Train456 months ago

For that matter, how do you connect a uni-polar stepper? Where does the fifth wire go?

Tranber729 months ago


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

Thank you for your help

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 it was great for me , it works boss
freefloater2 years ago
does this also work for a solenoid? I need it to spin in each direction.
Why can't I see my pictures on my post?
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?

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?