A DC motor just spins, and you don't know exactly where it is. But if you want to move a robot arm to a particular angle, or move a 3D printer's head precisely, you need a servo or a stepper.
The servo
A small DC motor, gears, a potentiometer that measures the angle, and a control circuit, all in one box. You tell it "go to 90 degrees", and it goes there and holds.
Control
A pulse every 20 ms (50 Hz), and the pulse width sets the angle:
(Some servos need 0.5 to 2.5 ms to reach a full 180.)
#include <Servo.h>
Servo s;
s.attach(9);
s.write(90); // 90 degrees
If the servo doesn't reach the end, use s.attach(9, 500, 2500) to set the minimum and maximum pulse widths. And be careful not to push it into a hard stop: it will keep pushing, heat up and draw a high current.
Smooth movement without delay
s.write(180) sends the servo there at full speed. To control the speed, step the angle one degree every few milliseconds, using millis:
int pos = 0, target = 180;
unsigned long tStep = 0;
void loop() {
if (pos != target && millis() - tStep >= 15) {
tStep = millis();
pos += (target > pos) ? 1 : -1;
s.write(pos);
}
}
That takes about 2.7 seconds from 0 to 180, and the program keeps running normally.
Torque
Torque is given in kg × cm. A 1.8 kg·cm servo can lift 1.8 kg on a 1 cm arm, or 0.9 kg on a 2 cm arm. The longer the arm, the less weight it can handle.
| Servo | Approximate torque | Gears |
|---|---|---|
| SG90 | about 1.8 kg·cm | Plastic |
| MG90S | about 2 kg·cm | Metal |
| MG996R | about 9–11 kg·cm | Metal |
Wiring
3 wires: brown or black = ground, red = supply (usually 5 V), orange or yellow = signal.
The most important tip: a servo draws a large current at the moment it moves (up to 1 A for a small SG90, and much more for a large MG996R). Don't power it from the Arduino's 5V pin if you have more than one small servo. Use a separate supply, with a common ground.
Is the servo jittering?
- The supply: the most common cause. Put a large capacitor (470 µF or more) across the servos' supply.
- The load is heavy and the servo can't hold.
- The signal is noisy: a long wire next to motor wires.
- Libraries using the same timer (such as SoftwareSerial) can upset the pulses.
And with an ESP32, use the ESP32Servo library, because the ordinary Servo library doesn't work on it. The 3.3 V signal is usually understood by the servo fine.
Types
- 180 degree: the most common.
- 360 degree (continuous): spins continuously, and the signal controls speed and direction rather than angle.
The stepper
It turns in fixed steps. The most common is 200 steps per revolution (1.8° per step). You count the steps and so you know exactly where it is, without a sensor, as long as no steps are lost to an excessive load.
Drivers
- 28BYJ-48 with ULN2003: very cheap for learning, slow and with little torque.
- NEMA 17 with A4988 or DRV8825: the ones in 3D printers and CNC machines. You send it a STEP pulse and a DIR signal, and the driver does the rest. The details are in the A4988 article.
How do you find the coils?
A bipolar stepper has 4 wires = two coils. Set the multimeter to ohms and measure between each pair of wires: the two that show a few ohms (for example 1.5–4) are one coil, and the other two are the second coil. If you read OL, they're from different coils. Connect each coil to two adjacent outputs on the driver (1A and 1B, and 2A and 2B).
Steps per millimetre
In a printer or CNC, you need to know how many steps move 1 mm:
- A GT2 belt with a 20-tooth pulley: one revolution = 40 mm. With 200 steps and 1/16 → 3200 ÷ 40 = 80 steps per mm.
- A T8 lead screw (8 mm lead): 3200 ÷ 8 = 400 steps per mm.
Acceleration and homing
- A stepper can't start suddenly at high speed: it shudders without turning, or loses steps. It must accelerate gradually. The AccelStepper library does this by itself with
setMaxSpeedandsetAcceleration. - At power-up, a stepper doesn't know where it is. That's why printers home: they move until they touch a limit switch, and treat that position as zero.
Important points
- Set the current limit on the driver with the small potentiometer on it, according to the motor's current. The wrong current either overheats the motor or makes it lose steps.
- Microstepping: the driver can divide a step into parts (1/16, for example) for smoother movement.
- A stepper draws current even when stopped in order to hold its position, so it gets warm. That's normal.
- Don't disconnect the motor while the driver is powered; it can be damaged.
Which should you choose?
| You need | The best fit |
|---|---|
| Fast spinning where position doesn't matter | DC motor |
| A specific angle with simple movement | Servo |
| Precise, repeatable positioning (printer or CNC) | Stepper |
FAQ
Does a servo return to its position when the power goes off? No, it stays where it is (unless a load is pulling on the gears), and when powered again it goes straight to the angle in the code. So make the first write in setup a safe angle.
Can I run a stepper without a driver? No. Arduino pins can't carry the coil current, and it needs a sequence of pulses. A ULN2003 is the minimum for a small motor.