Introduction: Arduino Stepper Motors

When we need precision and repeatability, a stepper motor is always the solution. With the way it is designed, a stepper can only move from one step to the next and fix in that position. A typical motor has 200 steps per revolution; if we tell the motor to go 100 steps in one direction, it will turn exactly 180 degrees. It gets interesting when we only tell it to go one step and it turns exactly 1.8 degrees.

Stepper motors are found in printers, scanners, industrial robot arms, 3D printers, and pretty much in every precision motion device.

There are two types of stepper motors: unipolar and bipolar. Unipolar stepper motors are easy to control at the cost of low efficiency and power. Bipolar stepper motors have much higher efficiency and torque; however they are much harder to control. To fully control one, two H-bridges are required. Luckily there are multiple Arduino compatible bipolar stepper drivers out there. Here we will explore a few options.

We can control a bipolar stepper motor using the Arduino Motor Shield. Here is what we need:

This is a simple guide on identifying the type. A four-cable stepper motor is usually bipolar. With six cables, it is most probably unipolar where the two center coil cables have to be connected together. There are some versions with only five cables that are also unipolar and already have the two center coils connected together internally. Also, there are stepper motors with eight cables, but they are incredibly rare. They are also unipolar and the four center cables have to be connected together.

This instructable and many more can be found in my Arduino Development Cookbook available here. :D

Step 1: How to Connect Them

We connect the stepper motor to the shield using the following steps:

  1. Carefully mount the Arduino Motor Shield on top of the Arduino. Be careful not to bend any pins.
  2. Identify the two coils. Use a multimeter to measure the resistance between all the wires. The ones with a low resistance in between are the coils.
  3. Connect the four stepper wires to the main terminal output of the Shield. One coil goes to one motor output and the other one to the other output.

Check the image for how it should look.

Step 2: Code

The following code will spin the stepper motor 100 steps in one direction and 100 steps in the other:

// Include the Stepper library<br>#include 
// Declare the used pins
int dirA = 12;
int dirB = 13;
int pwmA = 3;
int pwmB = 11;
// Declare a Stepper motor with 200 steps 
Stepper stepper1(200, dirA, dirB);
void setup() {
  // PWM pins require declaration when used as Digital
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  
  // Set PWM pins as always HIGH
  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  
  // Set stepper motor speed
  stepper1.setSpeed(60);
}
void loop(){
  // Turn the stepper 100 steps which means 180 degrees
  stepper1.step(100);
  // Wait half second
  delay(500);
  
  // Turn the stepper 100 steps back which means 180 degrees
  stepper1.step(-100);
  // Wait half second
  delay(500);
}

Step 3: Code Breakdown

The code declares a stepper motor, selects a speed, and makes it turn in both directions.

Here, we declare the stepper motor. The syntax requires the number of steps of the motor as the first parameter and then the pins to which we connected the motor. We only need to declare the two direction pins of the Arduino Motor Shield. The direction pins sets which direction the coils will be excited in:

Stepper stepper1(200, dirA, dirB);

In a normal DC motor operation of the Motor Shield, the two PWM pins select how much power we attribute to each motor. However, as this is a stepper, we always want full power; so we will simplify and directly set the PWM pins always as HIGH:

// PWM pins require declaration when used as Digital<br>pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
  
// Set PWM pins as always HIGH
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);

Another important step is declaring the speed at which we want the motor to turn. If, for example, we set a speed of 60 RPM as in this case, and the motor has 200 steps, it will take around 5 milliseconds to increment one step. The speed of the motor can be changed at any time.

stepper1.setSpeed(60);

Lastly, to make the motor move, we need to order the number of steps to increment. If we feed a negative number of steps, it will move in the opposite direction. Note that the step() function will pause the execution of the program until the motor spins completely. If, for example, we set 200 steps at a speed of 1 RPM, it will take one full minute until the Arduino will continue execution.

stepper1.step(-100);
Arduino assumes the stepper moves while it orders it to move. The stepper
has no feedback circuit, so if we hold the shaft of the motor, the Arduino will believe it is moving though the stepper might not be moving.

Step 4: More About Steppers

Stepper motors differ from normal DC motors in that, rather than just spinning in one direction or another, they move in small increments in a given direction. These small increments are called steps. We can tell a stepper to go one or more steps in one particular direction. They are not necessarily fast, but they have high precision and quite some torque. For example, the paper feeder on a printer uses a stepper motor. 3D printers and CNC machines use them for very high precision and repeatability.

A bipolar stepper motor only has two coils with no center tap, in contrast to a unipolar design. This means that the coils have to be turned on in both directions at different times. As a comparison, a bipolar stepper is exactly like two DC motors which always have to be controlled in the opposite direction at the same time. When one coil is excited in one direction, the other one has to be reversed. By shifting this, we generate a pulse which makes the stepper spin.

More topics regarding motors such as brushless, transistor drivers or motor speed control can be found in my Arduino Development Cookbook available here. :D

Comments

author
sujith john (author)2017-05-25

Perfect!! just what I was looking for. Complete detailed and practical.

What happens if I connect battery to a stepper motor or Arduino directly to a stepper motor ?

author
Build_it_Bob (author)2017-01-26

I was able to use your code to move my first bipolar stepper motor...thanks!

Build_it_Bob

author
mohamadb12 (author)2016-12-21

hi
my stepper motor gives whistle voice during rotation.
Who knows what the problem is?

my stepper motor is 42BYGH40388

author
afridave (author)2016-10-19

is it in any way possible to drive two steppers of one motor shield?

author
afridave (author)2016-10-19

and I assume that if you wish to drive two motors you require two motor drives?.....I have a motor shield ..also another motor driver..not sure which one....will have to experiment......is it feasible to do a two wheel drive tracked tank chassis using stepper motors?.....I have two really large...23....stepper motors....I assume that this will be very slow moving but quite powerful...

author
االحمداني (author)2015-07-16

Of great projects that use the shield Ardino

I'm working electronics engineer and I have the attention of drone

author
RBA2000 (author)2015-07-11

How about the heat of the stepper?

Does the stepper heat up if you move it to a point and keep it there for a while?

I wonder.

author
cornelam (author)RBA20002015-07-12

If you limit the current in the coil, no.

They are designed to be kept "on".

author
jeana2 (author)2015-07-09

Thanks for the info....

author
jeana2 (author)2015-07-09

Merci pour l'info...

author
econjack (author)2015-07-09

Make sure you realize that the graphic above is the controller board for the stepper, not the Arduino board. The Arduino just can handle the current draw needed by most steppers and it is REALLY hard to get that white smoke back into the Arduino once it gets out. I used my stepper to move a 10W solar panel to track the sun during the day when operating a low-power ham radio station in the field.

author
KevCarrico (author)2015-07-09

steppers can have AMAZING torque and/or holding power, depending on what type you use and your available power source. during certain positioning actions, all the internal coils can be under full power to hold the motor shaft in position, or move it with great strength.

author
KevCarrico (author)2015-07-09

great post -- i learned to make one play music last year:

http://youtu.be/AGivxR39bO8

author
wmada (author)2015-07-05

hey. I have a question. i havent used a stepper motor as of yet. i am using a mini servo to control ping sensor how can i sub the servo for the stepper. the reason being is the servo is too small to mount new.robot head.

author
cornelam (author)wmada2015-07-05

I would continue to use a Servo, they have more torque, they are much easier to control and they come in ALL sizes.
Check servocity.com , there you can find anything.

author
pcook5 (author)cornelam2015-07-09

Servo can have 2 meanings. There is the hobby servo - as used on RC cars and aircraft, and an actual servo motor. A Servo motor can be DC brush type or AC Brushless. Typically a DC servo is controlled with an analog signal, plus or minus 10VDC. An AC servo can be configured for an analog control signal, a stepper (or positioning) interface or and a pre-programmed indexer. The Positioning - or Stepper interface allows precision with a simple control - like an Arduino, with the capability of much more torque and speed, depending on the size of the drive.

Big names are Yaskawa and Mitsubishi with companies like Automation Direct being at the lower end of the price/performance range.

author
bricabracwizard (author)wmada2015-07-05

Also servos tend to be smoother running whereas stepper motors can be jerky.

author
cornelam (author)bricabracwizard2015-07-05

Servos can also be jerky, especially analogue ones under heavy load. I would use a digital servo in any high load situation.

author
bricabracwizard (author)cornelam2015-07-05

True! Like you said....they come in all sizes, make sure the one you use is capable of doing the job and digital is better. I remember when creating some effects using hydraulics and when I fine tuned them the demo would run smoothly and when the 'so-called' experts set it up it would be jerky....you know the saying: horses for courses? By the way I love the way you have set out your instructables... very informative as well!

author
martinhui (author)2015-07-04

I have modified the code a bit to work, I don't used the shield, not sure if it works with a shield?

#include <Stepper.h>

// Declare the used pins

int dirA = 12;

int dirB = 13;

int pwmA = 3;

int pwmB = 11;

// Declare a Stepper motor with 200 steps

Stepper stepper1(200, dirA, dirB, pwmA, pwmB);

author
cornelam (author)martinhui2015-07-04

I can't really understand the question. What driver are you trying to control?

author
srah1 (author)2015-07-02

Great instructable!

If anyone needs more precision than 1.8 degrees, you can use stepper drivers, for example chips A4988 and DRV8825. There's probably others, but these 2 are very prevalent in the 3D printing community.

About This Instructable

77,092views

768favorites

License:

Bio: Inventor, Author, Senior R&D Engineer, Entrepreneur, Forbes 30 Under 30
More by cornelam:Arduino Servo MotorsArduino Distance SensorsArduino Stepper Motors
Add instructable to: