Controlling Stepper Motors with the Arduino

Updated 28 October 2021

Stepper Motors are a bit of a halfway mark between DC Motors and Servo motors, but they find a sweet spot that allows them to be perfect for certain applications. There are still a few main differences to be aware of when it comes to working with stepper motors, though, I’ll introduce the basic ideas first then we will look at driving a stepper with an Arduino.

Stepper Motor animated image showing each phase being powered

Firstly, stepper motors are brushless DC motors, they rely completely on electromagnetic rotation, unlike their brushed counterparts. Another difference is that stepper motors have the permanent magnets on the rotating part of the shaft (the rotor). The rotor is a magnetic gear-shape and the with the coils on the outside (the stator) being electrically controlled. The teeth of the gear-shaped rotor make up what are known as the steps of your motor. Using the outside coils in sequence, the rotor can be precisely controlled. The final difference is that a stepper motor has a holding torque (measured in kg/cm) that refers to how much force the motor can hold at a position, in other words, it can be held still unlike DC motors. The downside is that while you have a precise method to control stepper motor movement, you haven’t got a feedback system we see in a servo to let your controller know where the motor is positioned.

This puts stepper motors in a great position for uses in printers and other CAM applications. Step count is an important specification for those applications; it relates how many steps the motor has per revolution and by design, this relates to the smaller movements a motor might need in those applications.

The relationship between phase (groups of coils) and polarity for steppers motors is simple enough. The number of wires coming from your motor will loosely let you know how many phases and poles your motor has. A unipolar stepper motor will energize the coils the same way every time, only allowing your motor to turn one way. Bipolar motors will offer bi-directional movement by the same logic. You can get different drivers to operate with stepper motors, this enables you to run them as either uni or bi-directional depending on how many phases you have.

As the coils are constantly energized (either holding or rotating), we need a way that we can provide enough power to our motor without frying our control board (Remember it's 20mA max on Uno IO pins). We call the family of circuits for this purpose Motor Drivers and there is a little math behind finding the right driver for your board.

We aren’t going to delve into the theoretical applications of H-Bridges & transistors to reverse current flow and power motors in this article. We are just going to look at the everyday-joe friendly math behind finding a driver for your motor. All you’ll need is basic algebra and common sense today.

Firstly, collect the following specifications from your motor’s datasheet. These 2 specifications will be used to find a driver. You can alternatively get the information from your driver and match a motor to it.

If you have a motor and are looking for a motor driver, go ahead and find out the:

-          Amps per phase of your motor (will be given as A/phase)

-          Resistance per phase of your motor (will be given as ohms, Ω)

If you have a driver and need to find a motor to drive off it, go ahead and find the:

-          Max current per phase (A/phase)

-          Max motor voltage (given in Volts, V)

All you need to do is ensure that your motor operates under the maximum voltage and current limitations of your driver. For example; The Big Easy Driver from SparkFun has a max voltage of 30v and a current limitation of 1.4-1.7A/phase. Taking a stepper motor such as this one (12V, .3A), we can see that it is well within the boundaries of the driver.

I grabbed a slightly older motor/driver combination from around the workshop and found it had very little supporting resources for it. The process should be pretty easy to iron out anyways, especially since we will be using the stepper motor library. We will just need to ensure the phases are connected correctly so they 'fire' in the right sequence to turn our motor. To interface our motor/driver circuit with some good old’ fashioned digital IO we will be using the Arduino Uno. To get started we want to connect our motor driver up to the digital IO pins we will be controlling it with. We will use pins 8, 9, 10, 11 for this particular 4 wire stepper motor.

Using the Stepper.h library we have a few commands we can use for control. Similarly, to the Servo.h library we used in our Servo tutorial, we will need to initialize our Stepper using the command:

Stepper [StepperName]([Steps/Revolution], [Pin1], [Pin2], [Pin3], [Pin4]);

Once we have done this we have the freedom to control the stepper using a couple of commands. These commands are all prefaced with your [Steppername]. And they include:

Step(value); - Steps the motor specified number of steps.
setSpeed(value); - sets the rpm the motor will perform at.

A combination of these commands can essentially control our motor however, we like. The Arduino stepper library allows us to pass a negative integer to the steps command too. This will move the motor counterclockwise instead of clockwise.

See the code below for how our simple one turns forward one turn backward:

#include 

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution of motor

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //Initialise your stepper motor on pins 8-11

void setup() {
myStepper.setSpeed(60); //sets the speed of the motor in rpm
}

void loop() {

  myStepper.step(stepsPerRevolution); //used to move motor a certain number of steps, negative values will step the motor backwards

  myStepper.step(-stepsPerRevolution);
}

Uploading our code to the Arduino Uno and we see the motor spinning just as expected. That's how simple it is to use the stepper library and a stepper motor.

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.