Steppers, Servos, and DC Motors
There's a difference, but it doesn't matter much at first. We are going to start playing with a stepper motor - this is a motor that can be made to rotate at various speeds and directions, one small angular step at a time. More on the fine point distinctions later.
A stepper motor requires four or five wires to control it (we talk about how that works later too) and it draws a lot of power. For these reasons we will use an intermediate device that is specially designed to control a stepper motor in between our motor and our computer controller (arduino). This little one which you can pick up for about $5 is a 5VDC 32-Step 1/16 Gearing Small Reduction Stepper Motor
The microcontroller is lightweight reprogrammable computer with digital and analog outputs and inputs. This means it can be used to get data from sensors and to control things (like motors) based on computations. The one we will use is called an Arduino.
The "inbetween" device is a circuit board that's the same size as an Arduino with some special purpose chips on it. It has pins coming out the bottom that fit right into the top of the Arduino board making all the connections needed between the two. A board like this is referred to generically as a "shield." The one we are looking at is made by Adafruit Industries, a company you should check out. It's called an "Adafruit Arduino Motor Shield"
Here's what we'll need to get started.
Hardware | Software |
---|---|
Arduino board Motor Shield Stepper motor Connecting wires |
The Arduino "Integrated Development Environment" (IDE) code libraries for the shield |
The code libraries make it possible to write code to control the motors at a very high level. By high level we mean that we do not need to concern ourselves with the nitty gritty of what is going on in the motor shield circuitry and on the Arduino board. We can simply say things like "make motor 1 spin fast."
You will need to download the Arduino IDE here and the requisite libraries here. Be sure to do the step referred to as installing the libraries (this means copying library files into the libraries subfolder in the Arduino folder).
Getting Started
Let's wire up a motor like this:
Next we will write (copy) a short "sketch" (i.e., program) written in the Processing language. We use the Arduino IDE to write and check the code and to send it to the Arduino board via a USB cable.
Our code will have four parts
- "include" statements that grab library code for us
- tell the machine to create a "motor shield object"
- designate a stepper motor in that object
- do some setup housekeeping
- run a loop of actions forever (stepping the motor forward and back in various modes)
Here's the logic in a flow chart:
// Include library code
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
myMotor->setSpeed(5); //rpm See documentation
}
void loop() {
Serial.println("Single coil steps");
myMotor->step(200, FORWARD, SINGLE);
myMotor->step(100, BACKWARD, SINGLE);
}
If you are interested in what's in those libraries you can look at them in the Arduino/libraries folder with the Finder or click on these links: Wire.h Adafruit_MotorShield.h, Adafruit_PWMServoDriver.h (note: PWM="pulse width modulation")