Stepper Motor + Arduino

From ESE205 Wiki
Revision as of 15:09, 13 December 2016 by Fordicejw (talk | contribs) (Example Code)
Jump to: navigation, search


There are two basic kinds of stepper motor: unipolar and bipolar. A unipolar stepper motor consists of one coil, or winding, per motor phase with a center tap through which current can flow. If one section of the coils is powered, the motor is in one phase. Power the other section and the motor will switch phase. In this way, the current will not need to switch polarity to operate the motor. This leads to a much simpler control circuit.

A bipolar motor will use one winding per phase. In order to switch motor phase, the polarity of the current going through the windings needs to be reversed. This leads to a more complicated method of controlling the motor.

The motor used in our project (Finding Feanor) was a NEMA 17 bipolar stepper. We used an ITEAD Dual Stepper Motor Driver Shield for Arduino that used two A3967 microstepping driver chips to control the steppers.


The Arduino has a convenient library called stepper.h that allows for the control of the motors with several pre-written functions. The complete documentation of the library can be found here.

An important thing to note about the step() function is that it is a blocking function. This means that if step(50) is called, the code will not move to the next line until all 50 steps have been completed. Thus, for the illusion of smooth movement with two motors, it would be necessary to increment one step at a time in rapid succession.

Example Code

This simple arduino sketch allows the user to input a number of steps in the serial monitor for the attached stepper motor to increment.

#include <Stepper.h>
void setup() {
    // put your setup code here, to run once:
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    Stepper myStepper(stepsPerRevolution, 2, 3); // Create the stepper object
    String readStr = ""; // A string to store the bits entered into the serial monitor
    const int stepsPerRevolution = 200;
    int stepCount = 0;

void loop() {
    // Manually controlling the stepper motor to find what step ranges work
    while ( Serial.available() != 0) {
        char c =;           //reads in the first available bit from serial monitor
        readStr += c;

     int n = readStr.toInt();           //converts readStr to an integer value n

     if (n != 0) {
         stepCount = stepCount + n;
         Serial.print("steps: ");
         n = 0;                            //Resets values of n and readStr
        readStr = "";