Stepper motors are among the most common actuators for robotics. Like a servo, a stepper motor can be commanded to move back and forth and then hold its position under load. Unlike a servo, which can me moved to a repeatable absolute position, a stepper motor can only be moved to a new position relative to its old position. Moving a stepper motor to an absolute position requires some kind of external position sensor. If a single base or home position can be distinguished, then software can spin the motor until home is detected and then advanced the number of steps required to achieve the desired position.
The rotor (the part of a a motor that moves) of a stepper motor has between 64 and 200 or so teeth around its circumference. The number of teeth determines the number of discrete positions that stepper mode can be in. If you pulse the field windings of a stepper motor in a particular manner, the rotor will move one tooth or step forward or backward and then hold its position even under load.
There are two kinds of stepper motors: Unipolar and Bipolar. Unipolar stepper motors have two center tapped field windings. Each center tap is typically connected to a positive power supply and either end of each winding is pulled to ground by a power transistor to pulse current though the half-winding. Bipolar stepper motors also have two field windings but each end of each winding is driven by a push-pull power transistor pair exactly like a DC motor.
For this class, you will use a stepper motor driver circuit that manages the field winding pulse timing. Controlling the driver will require two GPIO digital output signals: One for direction of rotation and one for stepping the motor to the next position. The stepper motors we will be using are not particularly fast, with a maximum step rate of only 800 steps per second (4 RPM).
const int MaxSteps = 200;
const int Dir = 2;
const int Step = 3;
void setup()
{
pinMode(Dir, OUTPUT); // Configure direction output signal
digitalWrite(Dir, false);
pinMode(Step, OUTPUT); // Configure step output signal
digitalWrite(Step, false);
// Move 100 steps clockwise, at 100 steps per second
digitalWrite(Dir, true);
for (int i = 0; i < 100; i++)
{
digitalWrite(Step, true);
delay(1);
digitalWrite(Step, false);
delay(9);
}
// Move 50 steps counterclockwise, at 100 steps per second
digitalWrite(Dir, false);
for (int i = 0; i < 50; i++)
{
digitalWrite(Step, true);
delay(1);
digitalWrite(Step, false);
delay(9);
}
}
void loop()
{
}