Servos, DC Motors and Transistors
Position a servo with the Servo library, switch DC motors with a transistor and flyback diode, and learn when to use a motor driver board.
Welcome back!
Step 1 of 12Learn
Making things move
Lights and sounds are nice, but the moment a project moves, it feels alive: a robot arm that waves, a blind that opens at sunrise, a car that drives itself around the kitchen. Three kinds of motors cover most Arduino projects:
- Hobby servo: turns to a specific angle, usually 0 to 180 degrees, and holds it. Great for pointers, flaps, steering and robot joints.
- DC motor: spins continuously; speed depends on voltage, direction on polarity. Great for wheels and fans.
- Stepper motor: moves in precise steps (often 200 per revolution) and can hold position. Great for 3D printers, camera sliders and anything that needs exact repeatable motion.
The golden rule from lesson one still applies: a pin supplies about 20 mA, but even a small motor needs hundreds of milliamps and can spike much higher when it starts or stalls. Motors also create electrical noise that can reset your board. So the Arduino only sends control signals, and the motor's power comes from a separate supply through a transistor, a driver board or, for servos, a dedicated power line.
Step 2 of 12Learn
Servos and the Servo library
A hobby servo has three wires: power (usually red, 4.8 to 6 V), ground (brown or black) and signal (orange, yellow or white). Inside are a small DC motor, gears, and a position sensor with a control circuit.
The signal is a pulse sent about 50 times per second (every 20 ms). The width of the pulse sets the angle: roughly 1 ms for 0°, 1.5 ms for 90° and 2 ms for 180° (exact limits vary between models). You don't need to generate those pulses yourself; the built-in Servo library does it:
#include <Servo.h>
Servo flap;
void setup() {
flap.attach(9); // signal wire on pin 9
}
void loop() {
int raw = analogRead(A0);
int angle = map(raw, 0, 1023, 0, 180);
flap.write(angle); // move to that angle
delay(15);
}A single micro servo (like the common SG90) can run from the Uno's 5V pin for testing, but under load it can draw several hundred milliamps and cause brownouts that reset the board. For two or more servos, or bigger servos, use a separate 5 to 6 V supply and connect its ground to the Arduino's GND. On the Uno, using the Servo library disables analogWrite on pins 9 and 10.
Step 3 of 12Practice · Work it out
A servo maps roughly 1.0 ms to 0° and 2.0 ms to 180°, linearly. What pulse width in milliseconds would hold it at 45°?
Step 4 of 12Note
Step 5 of 12Practice · Choose one
Two servos jitter and the Arduino keeps resetting when they move. Both are powered from the Uno's 5V pin. What is the best fix?
Step 6 of 12Learn
Switching a DC motor with a transistor
To let a 20 mA pin control a 500 mA motor, use a transistor as an electronic switch. A small current or voltage at its control terminal lets a much larger current flow through the motor.
A typical low-side switch:
- Motor positive to the external supply (for example, a 6 V battery pack).
- Motor negative to the transistor's collector (NPN) or drain (MOSFET).
- Transistor's emitter or source to ground.
- Arduino pin to the transistor's base through about 1 kΩ (NPN), or to a logic-level MOSFET's gate through about 100 to 220 Ω.
- Battery ground connected to Arduino GND.
- A flyback diode across the motor, with its stripe (cathode) toward the positive supply.
Why the diode? A motor coil stores energy in its magnetic field. When the transistor switches off, that energy produces a voltage spike that can exceed 100 V for an instant and destroy the transistor. The diode gives that current a harmless loop to die out in.
Now digitalWrite(9, HIGH) runs the motor and analogWrite(9, 128) runs it at roughly half speed using PWM. A transistor only switches one direction, though. To run a motor forward and backward you need an H-bridge, usually bought as a motor driver module.
Step 7 of 12Note
Step 8 of 12Practice · Select all that apply
Which parts are needed to run a small 6 V DC motor at variable speed from an Uno, in one direction? Select all that apply.
Select all that apply.
Step 9 of 12Compare
Which motor for which job?
| Aspect | Hobby servo | DC motor | Stepper motor |
|---|---|---|---|
| Motion | Moves to an angle (often 0–180°) and holds | Spins continuously | Moves in fixed steps, can hold position |
| Control from Arduino | Servo library, one signal pin | Transistor for one direction, H-bridge driver for both | Stepper driver board, step and direction pins |
| Typical uses | Flaps, pointers, robot joints, steering | Wheels, fans, pumps | Printers, CNC, camera sliders |
| Main drawback | Limited rotation, modest strength | No position feedback without extra sensors | Needs a driver, draws current even when still |
Step 10 of 12Tap to reveal
Relays: switching other circuits
What a relay is
An electromagnet that pulls a mechanical switch closed. The switch is electrically isolated from the coil, so one circuit can control a completely separate one.
When to use one
For switching loads that need isolation or alternating current, such as a low-voltage AC garden valve, or for switching devices that run from a different supply.
Use a module
Relay modules include the transistor, flyback diode and often an indicator LED, so the Arduino pin only drives a small input. A bare relay coil connected directly to a pin can damage it.
Limits
Relays click, wear out after many thousands of switching cycles and are too slow for PWM. For fast or frequent switching of DC loads, a MOSFET is the better choice.
Step 11 of 12Practice · True or false
A flyback diode is placed across a DC motor so that it conducts during normal operation and helps the motor spin faster.
Step 12 of 12Practice · What would you do?
What would you do?
Lena wants a small robot car whose wheels can drive forward and in reverse at different speeds. Which approach fits?
Lesson complete!