Stepper Tutorial

Stepper motor parts.
The stepper's insides

Stepper motors are similar to synchronous AC motors, or brushless DC motors. The rotor is magnetized and the stator is four pairs of electromagnets. The motor is controlled by driving current through those electromagnets, which forces the rotor to turn to align with them. Where the rotor on a brushless motor may have four poles, the rotor of the stepper motor has dozens of poles. The many closely spaced poles allow the controller to move the motor a fraction of a revolution and stop.

The wiring of a stepper motor can be 4 to 8 lead. The connections vary based on whether the motor is wired to use bipolar or unipolar drive. Bipolar drive means the voltage on two ends of the coil are first one way, and then reversed. This is generally more efficient than unipolar drive, where one end of the coil is held high and the other end is brought low to energize the coil. Bipolar motors will have 4 or 8 leads. Unipolar will have 5 or 6 leads.

The stepper motor is a Mercury Motion bipolar stepper SM-42BYG011-25. It has 200 steps per revolution (1.8° per step) and a DC resistance of 33Ω per coil. It came from Sparkfun. The data sheet is linked to on the product page. It is about the largest motor I would try to run without a dedicated supply.

The Driver

Stepper motor tutorial.

The Arduino can't supply enough current or voltage to directly drive one of these stepper motors. For that an external driver is required. These L298N driver boards will drive one stepper motor per board. Four Arduino digital outputs are required to drive a single motor.

This driver was picked up on ebay for under $3.00 delivered, and has an L298N motor driver IC, a 5V regulator, and protection diodes. All high-current connections are screw terminals. Signal level connections are header posts.

Arduino Stepper Tutorial Schematic

There are only three parts to this circuit - Arduino Uno, driver, and stepper motor. The wiring should be 26 gauge or larger on the 12V and ground lines. The driver inputs are just 5V signal level. You must provide an external 12V power supply, plugged into the Arduino power jack. If the motor is too large, you will need to run the 12V directly to the driver board, and not run it through the Arduino.

Arduino Code

The Code

The example code is very simplistic. Setting the step rate too high will cause the motor to miss steps. A proper stepper controller accelerates from a low step rate to a high rate, and then decelerates from the high rate to a low rate. This is because inertia tries to prevent the stepper from turning at first, and then tries to keep the motor turning after the electrical drive has stopped. Acceleration and deceleration help prevent dropping steps and overshoot during starts and stops without limiting the maximum speed. It takes software to build a profile and a controller to apply the profile to the drive, but very high speeds can be had. One particular drive would turn a prism 90 degrees in 30 mS from stop to stop, accelerating to 5000 steps per second and immediately decelerating to a stop. The motor just made a "click" sound at that speed.

    
#include <Stepper.h>

// change this to fit the number of 
// steps per rev for your motor.
const int stepsPerRevolution = 200;  

Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 30 rpm:
  stepper.setSpeed(30);
}

void loop() {
  // Turn clockwise one revolution
  stepper.step(stepsPerRevolution);
  delay(500);

  // Turn counterclockwise one revolution
  stepper.step(-stepsPerRevolution);
  delay(500);
}
    

Steps per Revolution vs. Degrees per Step

If you don't know your steps per revolution, but you do know the degrees per step, you can find the steps per revolution by dividing 360 degrees by the degrees per step. A 1.8° per step motor has:

360° ÷ 1.8 = 200 steps per revolution

If you know the steps per revolution, but need the degrees per step, divide 360° by the steps:

360° ÷ 200 = 1.8° per step

Another Driver

Stepper motor driver.
A3987 Stepper Driver with Translator

Another way to drive a stepper motor is with an external translator - a chip that decides which phases toggle and when. The Allegro A3987 is a stepper motor driver with a built-in translator. All you need to do is tell it which direction to go and pulse it once for each step. The A3987 will figure out what to do to make the motor go that direction. It also can microstep, which means it can step even finer than the resolution of the motor. If you have a 400 step per revolution motor, you can get 400, 800, 1600, or 4800 steps per revolution using microstepping.

This particular board is a "bootleg" copy of the one available from Sparkfun. The designer is schmalzhaus.com, but this one says "schmalzhaus.cpm". The board has step and direction inputs, and motor outputs, but also has microstepping selection lines and a few other control lines. One great thing is that it has a built-in 5V or 3.3V regulator. You can specify which you want, and it will not only run on that, it will output it to power your Arduino as well.

A Great Library

Recently I've been doing a project that required turning a wheel by running a motor against its rim. The "gear" ratio is 17:1. At the thirty or so steps per second the motor could reliably run with code like that above, it would take all day to make a revolution. In order to meet the speed requirement, the motor needs to turn at 2500 steps per second! You just can't do that without accelerating and decelerating the motor.

The AccelStepper Arduino library does just that. You supply a position you want the motor to run to (in steps) and it accelerates to maximum speed, runs there, then decelerates to a stop at the position you specified. I encourage you to look into it if you have need of higher step rates. The documentation includes examples.

Perhaps the best thing about it is that it doesn't "block", meaning you tell it to take a step, and it will if the time is right, but then you can execute other instructions before telling it again to take a step. It does have some blocking methods, which will go from the current position to the target position without returning until the motor is stopped.

Arduino Board Logo

 

Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support

Arduino-Board

Stay updated

Sign up if you would like to receive our once monthly newsletter.