Stepper Motor + Arduino

From ESE205 Wiki
Revision as of 03:24, 17 August 2018 by Ethanshry (talk | contribs) (Ethanshry moved page Controlling Stepper Motors with Arduino to Stepper Motor + Arduino: Fall 2018 Wiki Reworks)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Hardware

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.

This wiki page details the motor shield and its use. The motor wires can be inserted into the drive sockets and tightened with a small screwdriver or connected to the board using the 4 male headers adjacent to the drive sockets.

Arduino

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() {
    Serial.begin(9600);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    String readStr = "";                         // A string to store the bits entered into the serial monitor
    myStepper.setSpeed(200);
    const int stepsPerRevolution = 200;
    int stepCount = 0;
    Stepper myStepper(stepsPerRevolution, 2, 3); // Create the stepper object
}

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

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

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

Here, the stepper object is defined to have 200 steps per rotation, and be controlled by pins 2 and 3 (which correspond to the wires on the X drive socket on the stepper motor shield). Next, the speed is defined by the setSpeed() method. The loops checks to see if anything has been entered into the serial monitor, and if it has, it reads it and converts it to an integer. Finally, the code uses the step(n) command to increment the stepper n steps and update the stepCount variable accordingly.