Lesson 9 of 13
Control a motor
Objective: Drive a DC motor safely: the pin switches a TIP120 driver, the motor's own battery does the work, and PWM sets the speed.
Teal: the signal path (a few mA from the pin). Red: the power path (the battery's current). The rotor is drawn slowed down.
While the sketch runs, a zoomed-in view shows the pin switching, and a chart draws the duty, the motor's average voltage and its speed over the last 4 seconds.
Stage 1 of 7 · What am I building?
Explore
A rover's wheels need far more current than a pin can give. In this circuit the pin only works a switch, and the motor's own battery does the work. Select each part on the circuit, or below.
Pick a part. 0/7 inspected.
⚠ Never power a motor from a pin or the board's 5V pin. It gets its own battery, a driver, and a GND shared with the board.
Before you start (optional refreshers)
Voltage, current and resistance
Voltage pushes, resistance restricts, current is the flow. Ohm's law ties them together: I = V ÷ R.
Pins, inputs and outputs
Each pin is configured as an input (the program reads it) or an output (the program drives it). pinMode() chooses.
PWM and duty cycle
analogWrite() switches a ~ pin on and off quickly. The ON fraction (0–255 → 0–100%) sets average power. It is not a true analog voltage.
New ideas in this lesson
Actuators and external power
Motors and servos need more current than a pin gives. Use a driver (transistor), a separate supply, a flyback diode for motors, and a shared GND.
A transistor as a switch
A small current into an NPN transistor's base lets a much larger current flow from collector to emitter. A pin drives the base through a resistor (the signal path); the load's own supply flows through the collector (the power path).
Build it for real · Arduino Uno R3
You need
- Arduino Uno R3 (or compatible) and its USB cable
- Breadboard and jumper wires
- Small DC motor
- TIP120 transistor (NPN Darlington)
- 1N4001 diode
- 1 kΩ resistor
- Separate motor supply (e.g. 4 × AA, 6 V)
Wiring
- Identify the TIP120 legs: printed face toward you, left to right Base, Collector, Emitter. The metal tab is also the collector, so keep it clear of other parts.
- D9 → 1 kΩ → TIP120 base.
- TIP120 emitter → GND. Join the motor supply's − to the board's GND.
- Motor supply + → motor; motor's other lead → TIP120 collector.
- 1N4001 across the motor, striped end toward the supply +.
Before you power up: Never power a motor from a pin or the board's 5V pin. Keep the diode in place, and disconnect the motor supply before changing wiring.
The lab models ideal parts. Real circuits add tolerances, noise and loose wires, so check your wiring against this list before you plug in.
// Control a motor's speed with PWM, through a TIP120 driver
// D9 (a ~ pin) -> 1k resistor -> TIP120 base; emitter to GND
// Motor from the 6 V battery + to the collector; 1N4001 across it, stripe to +
// Battery - joined to the board's GND. Never power a motor from a pin or 5V
const int motorPin = 9; // a ~ pin: it can make PWM
const int speed = 200; // 0 (stopped) to 255 (full)
void setup() {
// nothing to do: analogWrite() makes motorPin an output by itself
}
void loop() {
analogWrite(motorPin, speed); // HIGH for speed/255 of each cycle
}Press ▶ Run.