Scherz & Monk, Chapter 15

Making Things Move

Three motor families — DC, servo, stepper — trade control granularity for complexity. But they all bow to the same three ideas: switch power instead of burning it (PWM), reverse polarity with four switches (the H-bridge), and tame the inductive kickback (the flyback diode). Master those and you can drive anything that spins.

Prerequisites: Ohm's law & P=I²R + transistors as switches + inductors & back-EMF + duty cycle
17
Chapters
5
Simulations
0
Assumed Knowledge

Chapter 0: The Melting Potentiometer

You have a 6 V hobby DC motor, a fresh 6 V battery, and a potentiometer. You want to slow the motor down, so you do the obvious thing: wire the pot in series, like a dimmer for a lamp. Crank it up and the motor spins. Turn the knob to "slow it down" and… the pot gets scorching hot, the motor barely slows before it shudders to a stall, and a minute later there's a wisp of smoke and a melted wiper. Meanwhile your friend's robot runs the same motor at any speed from a crawl to full tilt, cool to the touch, driven by a single microcontroller pin.

What is the micro doing that your potentiometer isn't? The answer is the spine of this entire chapter. A series resistor controls speed by dissipating the unwanted energy as heat. The microcontroller controls speed by switching the motor fully on and off thousands of times a second, so the average power is whatever fraction you choose — with almost no waste. This trick is called pulse-width modulation (PWM), and it is why nothing in a well-designed motor drive ever melts.

Let's see exactly why the resistor cooks. Say the motor's winding resistance is about 3 Ω and at full speed it draws roughly 1 A from 6 V. To halve the voltage at the motor you'd add a 3 Ω series pot. Now the circuit is two 3 Ω resistances in series, so the current drops and the pot itself sinks a big chunk of the power.

Ppot = I² × Rpot  ≈  (1 A)² × 3 Ω = 3 W

A typical trimpot is rated for a fraction of a watt. Three watts in a part rated for 0.25 W is a meltdown. Worse, the motor is not a fixed resistor: as it slows, its self-generated back-voltage (back-EMF, Chapter 4 here) collapses, so it tries to draw even more current near stall — right when the pot is hottest. The control scheme fights itself.

The core swap. Linear (resistive) control wastes the difference between supply and motor voltage as heat: efficiency at half speed is roughly 50%, and all the lost power lands in one tiny component. Switching (PWM) control never sits at a partial voltage — the switch is either fully on (near-zero voltage across it, so near-zero loss) or fully off (no current, so no loss). Average delivered power = duty cycle × full power, at >95% efficiency. Same knob feel, no smoke.
Why the Pot Melts: Power Dissipation vs Speed Setting

Drag the speed knob from full to slow. The bars compare where the power goes in two schemes: a series resistor (red = wasted heat) vs PWM switching (teal = delivered, tiny loss). Watch the resistor's heat peak in the middle — the worst place for it.

Target speed 50%
Why is a series potentiometer a poor way to control a DC motor's speed?

Chapter 1: Inside a DC Motor

The humble brushed DC motor is the simplest electromechanical device you'll meet: two leads, and that's the whole interface. Apply a voltage one way, it spins one way; reverse the voltage, it spins the other way; more voltage, faster. There's no logic, no protocol, no feedback wire. That brutal simplicity is its appeal — and its limitation.

Inside, a wound coil (the rotor or armature) sits in the field of permanent magnets (the stator). Current through the coil makes it an electromagnet; it's pushed by the stator field and rotates. A clever mechanical switch called the commutator — a split ring with carbon brushes — flips the coil's current every half-turn so the push never reverses. The motor keeps spinning as long as current flows.

The three numbers that describe any DC motor

Two facts govern its behavior. First, speed rises with applied voltage: a motor rated 6 V at 6000 rpm spins roughly proportionally slower at lower voltage. Second, the spinning coil generates its own opposing voltage, the back-EMF, proportional to speed:

Vapplied = I × Rwinding + Vbemf     where Vbemf = ke × ω

This single equation explains everything. At full speed under no load, ω is high, so Vbemf is large and nearly cancels Vapplied — the current I = (Vapplied − Vbemf)/R is small. But the moment you stall the shaft (ω = 0), Vbemf collapses to zero and the full supply drives current through just the tiny winding resistance. That's the dreaded stall current.

Torque, and the stall-current spike

Torque is proportional to current: T = kT × I. More current means more twist. Since stall kills the back-EMF, stall current is enormous — often 1000% or more of the free-running current. A motor that sips 0.3 A spinning freely can demand 3–5 A locked.

Worked example — stall current. A 6 V motor with winding resistance R = 1.2 Ω runs freely drawing 0.4 A. Stalled, Vbemf = 0, so Istall = V/R = 6 / 1.2 = 5 A — over 12× the free-run current. Your driver transistor and supply must survive that surge, or budget a current limit. This is also why motors get hot when overloaded: P = I²R climbs with the square of current.

DC motors typically spin 3000–8000 rpm and come rated for 1.5–24 V. Two practical limits bracket the supply: run them below about 50% of rated voltage and they may not produce enough torque to overcome their own friction — they stall and just heat up. Run them above about 130% of rated voltage and they overheat and wear the brushes fast. The usable window is narrower than you'd guess.

DC Motor: Voltage → Speed, Current & Back-EMF

Slide the applied voltage on a 6 V / 6000 rpm motor. Watch speed, back-EMF, and current. Toggle "stalled" to lock the shaft and see the current spike. Note the safe band (50–130%).

Applied voltage 6.0 V
Why does a DC motor draw its largest current at the instant of stall (locked rotor)?

Chapter 2: PWM Speed Control

Chapter 0 showed the problem. Here's the fix in full. Instead of feeding the motor a partial voltage through a resistor, we feed it the full supply — but only for a fraction of each tiny time slice. Switch fully on, then fully off, then on again, thousands of times per second. The motor's mechanical inertia and electrical inductance can't respond to each individual pulse; they average them. The motor behaves as if it's running on the average voltage.

The fraction of each cycle the switch is ON is the duty cycle. The averaging gives a beautifully simple result:

Vavg = duty × Vsupply

And because the switch is never partway on, almost no power is burned in the switching transistor. When it's on, the voltage across it is near zero (so I²R loss is tiny). When it's off, the current is zero (so no loss). All the delivered power reaches the motor. That's why a PWM drive can sit at 30% speed indefinitely without warming up, while the series pot at 30% would be an inferno.

Worked example — tuning a fan to 4000 rpm

You have a 12 V motor that spins about 6000 rpm at full voltage. You want roughly 4000 rpm. Since speed tracks average voltage, find the voltage first:

Vavg = (4000 / 6000) × 12 V = 8 V  →  duty = 8 / 12 = 0.667 = 67%

So you program a 67% duty cycle. At a 1 kHz PWM frequency (period = 1 ms), the ON time per cycle is thigh = 0.667 × 1 ms ≈ 0.67 ms, with 0.33 ms off. No resistor, no heat, exact speed.

The stall floor is real. PWM does not repeal physics. Below roughly 50% duty on this motor, Vavg drops under ~6 V — below the ~50%-of-rated threshold from Chapter 1 — and the motor can't make enough torque to overcome static friction. It buzzes and stalls instead of crawling. If you genuinely need slow, smooth low-end torque, that's a job for a gearbox or a stepper, not a lower duty cycle.

Why a high switching frequency matters

If you switch too slowly (say 20 Hz), you'll hear and feel the pulses — the motor cogs and whines audibly. Push the frequency well above hearing (1–20 kHz) and it goes silent and smooth, because the motor's inductance filters the chop into clean average current. The trade-off: faster switching means the transistor spends more total time mid-transition, where it does dissipate — so very high frequencies bring back a little switching loss. Audio-band-just-above (~20 kHz) is the usual sweet spot.

PWM Duty → Motor Speed (Showcase)

Slide the duty cycle. The scope trace shows the switched waveform; the gauge shows Vavg = duty×12 V; the motor spins at the corresponding speed. Below 50% the rotor stalls (red) — just like the real thing.

Duty cycle 67%
A 12 V motor is driven by PWM at 40% duty cycle. What average voltage does the motor effectively see?

Chapter 3: Direction & the H-Bridge

A DC motor reverses when you reverse the polarity of the voltage across its two leads. Swap the wires and it spins backward. But you can't swap wires in software. So how does a robot reverse a motor under microcontroller control? With four switches arranged in the shape of an H — the H-bridge.

Picture the motor as the horizontal bar of the H. Above it sit two switches connecting each motor lead to the positive supply (call them the high-side switches, top-left A and top-right B). Below it, two more connect each lead to ground (low-side, bottom-left C and bottom-right D). The motor's left lead joins A and C; the right lead joins B and D.

The diagonal rule

To drive the motor forward, close one diagonal pair: A + D. Now the left lead is at +V and the right lead is at ground — current flows left-to-right, motor spins forward. To reverse, close the other diagonal: B + C. Now the right lead is +V and the left is ground — current flows right-to-left, motor spins backward. Two diagonals, two directions. It's that clean.

A + D closed → FORWARD
Left lead to +V (via A), right lead to GND (via D). Current flows left→right through the motor.
B + C closed → REVERSE
Right lead to +V (via B), left lead to GND (via C). Current flows right→left — reversed polarity, reversed spin.
A + C (or B + D) → SHORT CIRCUIT
Both switches on the same side connect +V straight to GND through nothing but the transistors — a dead short. This is called shoot-through and it destroys the bridge instantly.
The forbidden state. The single most important rule of H-bridges: never turn on both switches on the same side at once. Top-left and bottom-left (A+C) ties the supply rail directly to ground with no motor in between. The current is limited only by the transistors' resistance, which is tiny — so the current is huge, and the bridge dies in microseconds. Real driver ICs add a "dead time" delay when switching directions precisely to guarantee one side fully opens before the other closes.

The H-bridge also does PWM — and braking

Here's the elegance: you don't need a separate speed-control circuit. PWM one of the active diagonal switches on and off, and you control speed and direction with the same four transistors. Want forward at 60%? Hold A on, PWM D at 60%. Want to brake? Close both low-side switches (C + D) — this shorts the motor's two leads together, and the spinning rotor's own back-EMF drives a current that opposes its motion, stopping it fast. (Both low-side on with the motor between them is safe; it's both on one side that shorts the supply.)

H-Bridge Direction Toggle (Showcase)

Click the four switches (A, B top; C, D bottom) to open/close them. Close the A+D diagonal for forward, B+C for reverse. Close both on one side and the bridge flashes a SHORT warning. The motor in the middle spins to match.

Tip: click a switch directly on the diagram to toggle it.

In an H-bridge, which switch combination produces a destructive short ("shoot-through")?

Chapter 4: Back-EMF & the Flyback Diode

Every motor is, electrically, a coil of wire — an inductor. And inductors have a stubborn personality: the current through them cannot change instantly. Push current through a winding, then suddenly open the switch, and the inductor refuses to let that current stop. It generates whatever voltage it takes to keep the current flowing for an instant — and "whatever it takes" can be hundreds of volts, even from a 6 V supply.

This is the inductive kickback (also called back-EMF in the switching sense, distinct from the speed-back-EMF of Chapter 1). The governing law is the inductor equation:

VL = L × (dI / dt)

When you open a switch, you're trying to drive dI/dt to a huge negative value in a fraction of a microsecond. With dt tiny, VL spikes enormous. That spike appears across your switching transistor as a reverse voltage and punches a hole right through it — one of the most common ways beginners destroy a driver transistor.

The fix: a flyback diode

Place a diode across the motor winding, oriented so it's normally reverse-biased (cathode to +V, anode to the switched node). In normal operation, current flows through the motor and the diode does nothing. But the instant the switch opens and the inductor tries to fling its voltage the "wrong" way, that voltage now forward-biases the diode. The diode opens a path, and the inductor's current circulates harmlessly in a loop through the winding and diode, decaying gently as its energy bleeds off as heat.

Vspike clamped to Vsupply + Vdiode ≈ Vsupply + 0.7 V
Worked reasoning — from BANG to 0.7 V. Without a diode: open the switch, dI/dt is huge, and VL rings up to perhaps 100–300 V — far beyond a small transistor's breakdown rating, so it fails. With a 1N4001 flyback diode: the moment the node voltage rises ~0.7 V above the rail, the diode conducts and pins it there. The current that was flowing in the motor now flows through the diode instead, looping back into the winding. The spike never exceeds ~0.7 V above supply. The transistor survives. One cheap diode, total protection.

The standard parts are the rectifier diodes 1N4001 or 1N4002 — cheap, robust, and fast enough for motor switching. In an H-bridge you need four of them (one across each transistor), and most integrated driver ICs (like the L293D) build them in. For relays and solenoids — also inductors — the exact same flyback diode is mandatory.

Back-EMF / Flyback Visualizer

A switch drives a motor-inductor. Click "Open Switch" to interrupt the current. Without the diode, the voltage spikes — "BANG!" Toggle the flyback diode on, then open the switch again: the current loops safely and the spike clamps to ~0.7 V above the rail.

What is the purpose of a flyback (freewheeling) diode placed across a motor winding?

Chapter 5: RC Servos

A DC motor spins; it has no idea where it is. Often you don't want endless spinning — you want an arm to swing to exactly 37° and hold there. That's the job of an RC servo: a small geared DC motor packaged with a position sensor (a potentiometer on the output shaft) and a control circuit, all in one box with three wires — power, ground, and signal.

Inside, the servo runs a tiny closed loop. The control circuit compares where the shaft is (read from the feedback pot) against where it's told to be (from the signal wire), and drives the motor to null the difference. When the arm reaches the commanded angle, the error is zero and the motor stops, holding position against load. You command position; the servo handles the rest.

The pulse-width command

The genius of the servo standard is how the command is encoded: a pulse width, not a voltage or a number. The signal wire receives a pulse roughly every 20 ms (a ~50 Hz frame), and the duration of each pulse sets the target angle:

The angle is linear in pulse width across the 1.0–2.0 ms window:

angle ≈ (pulsems − 1.0) / (2.0 − 1.0) × 180°
Worked example — commanding 135°. Plug into the formula: pulse = 1.0 + (135/180) × (2.0 − 1.0) = 1.0 + 0.75 = 1.75 ms. Sanity-check the anchor: 90° → 1.0 + (90/180)×1.0 = 1.5 ms = neutral. ✓ And anything narrower than 1.5 ms drives clockwise of center, anything wider drives counter-clockwise. The pulse repeats every ~20 ms; only its width carries information.

Servos run on 4.8–6.0 V and pull surprising current under load, so power them from a stout supply, not the microcontroller's regulator. Generating the pulse is trivial: a 555 timer in monostable mode, a dedicated PWM peripheral, or — the modern path — one line of an Arduino Servo library call (servo.write(135)), which translates the angle to the 1.75 ms pulse for you.

Servo Pulse-Width → Arm Angle

Slide the pulse width from 1.0 to 2.0 ms. The horn rotates 0–180°; 1.5 ms snaps to 90° (neutral). The 20 ms frame and the internal feedback-pot dial are shown live.

Pulse width 1.50 ms
An RC servo receives a 1.5 ms pulse every 20 ms. Where does the horn go?

Chapter 6: Stepper Motors

The servo gives you precise position over ~180°, with feedback. The stepper gives you precise position over unlimited rotation, without feedback — open loop. You don't tell a stepper an angle; you tell it to take a certain number of discrete steps, and it clicks around exactly that far. Pulse it 50 times, it advances 50 steps. No sensor, perfectly repeatable. This is why steppers run 3D printers, CNC machines, and camera mounts.

Inside, the stator has several coils (phases) arranged in a ring, and the rotor is a toothed magnetic gear. Energize one coil and the nearest rotor tooth snaps into alignment with it. Energize the next coil in sequence and the rotor advances by one tooth-pitch — one step. Cycle the coils in order (1000 → 0100 → 0010 → 0001 → repeat) and the rotor walks around the ring, one crisp step per coil change.

Step angle: the master equation

How far is one step? It's set entirely by the mechanical tooth count, expressed as the step angle:

step_angle = 360° / (steps per revolution)     steps = target_angle / step_angle
Worked example — the classic 1.8° stepper. The most common stepper has a 1.8° step angle. Steps per revolution: 360° / 1.8° = 200 steps/rev. To rotate exactly 90°: steps = 90° / 1.8° = 50 steps. Pulse the sequencer 50 times and the shaft is at 90°, dead on, no measurement needed. Want finer resolution? Half-stepping energizes two adjacent coils at once between the single-coil positions, halving the step to 0.9° — now 90° needs 100 half-steps. Microstepping pushes resolution further still.

Steppers come with different lead counts that tell you the internal wiring: 4 leads = bipolar (two coils, driven by an H-bridge per coil), 5 or 6 leads = unipolar (center-tapped coils, simpler drive), and 8 leads = universal (wire it either way). Step angles range from a coarse 90°/step up to a fine 0.72°/step. Unipolar steppers are the friendliest to drive — a chip like the ULN2003 (seven Darlington switches with built-in diodes) just grounds each coil in turn.

Stepper Coil-Firing Sequencer

Press Step+ / Step− to fire the four stator coils in sequence (1000→0100→0010→0001). The toothed rotor snaps to the energized coil. Toggle half-step to insert two-coil-on positions and halve the angle.

A stepper motor has a step angle of 1.8°. How many steps make one full revolution?

Chapter 7: Driving from a Microcontroller

A microcontroller pin can source maybe 20 mA at 5 V. A motor wants amps at 6, 12, or 24 V. You cannot wire a motor straight to a GPIO pin — the pin (and likely the whole chip) will die. Between the brain and the muscle you always need a buffer / driver stage: a transistor or driver IC that takes the pin's tiny logic-level signal and switches the motor's big current.

The simplest case — one DC motor, one direction — is a single transistor (a logic-level MOSFET or a Darlington) with the motor and a flyback diode in its drain/collector circuit. The pin's PWM signal switches the transistor; the transistor switches the motor. Add the flyback diode from Chapter 4 and you're done. For bidirectional control, you need the full four-switch H-bridge of Chapter 3.

The driver ICs you'll actually use

ICWhat's insideBest for
ULN20037 Darlington switches + built-in flyback diodesUnipolar steppers, relays, one-direction loads
L293 / L293DDual H-bridge (two motors, both directions); 'D' adds internal diodesTwo DC motors or one bipolar stepper, ~600 mA
L298Dual H-bridge, higher current (~2 A/channel)Bigger DC motors & bipolar steppers
SAA1027Stepper translator (handles sequencing internally)Unipolar steppers from simple step/direction inputs
The three non-negotiables of any motor drive. (1) Isolate the current — never run motor current through a logic pin; use a transistor or driver IC. (2) Protect against kickback — every inductive load gets a flyback diode (or use a 'D' part that has them built in). (3) Separate the supplies — motors throw electrical noise and voltage dips onto the rail; give the motor its own supply (or at least heavy decoupling) so the brownout doesn't reset your microcontroller. Tie the grounds together, but split the +V.

The modern path: libraries do the hard part

You rarely hand-roll the timing anymore. On an Arduino, analogWrite(pin, 170) emits a ~67%-duty PWM for DC speed; the Servo library turns servo.write(135) into the 1.75 ms pulse; the Stepper library turns stepper.step(50) into the 50-pulse coil sequence for a 90° move on a 1.8° motor. The software hides the protocol — but the hardware (driver IC + flyback diodes + separate supply) is still entirely your responsibility, and that's where projects fail.

From Pin to Motor: The Driver Chain

Toggle each protection element to see what happens to the signal chain. Remove the driver and the pin overloads; remove the diode and the transistor gets a kickback spike; share the supply and the micro browns out.

Why can't you connect a DC motor directly to a microcontroller output pin?

Chapter 8: Connections & Summary

Three motor families, one toolkit. Whatever you're driving, the same three ideas recur: PWM to set average power without burning heat, the H-bridge to reverse polarity in software, and the flyback diode to tame the inductive kickback. Choosing the right motor is the first decision; the toolkit is the same afterward.

Decision table: which motor?

 DC MotorRC ServoStepper
MotionContinuous spinConstrained angle (~0–180°)Continuous, in discrete steps
Position controlNone (open shaft)Precise, closed-loop (built-in pot)Precise, open-loop (count steps)
FeedbackNoneInternal potNone needed
Control signalPWM duty (speed)Pulse width 1.0–2.0 ms (angle)Step + direction pulses
Wires23 (V, GND, signal)4 / 5 / 6 / 8
Speed / torqueHigh speed, low precisionModerate, holds positionLow speed, high holding torque
Use it forWheels, fans, pumpsSteering, arms, pan/tilt3D printers, CNC, precise positioning

Equation & fact cheat-sheet

ConceptFormula / factTab
Resistive lossP = I²R — why the series pot melts0
Motor voltage balanceVapplied = I·R + Vbemf; Vbemf = keω1
Torque & stallT = kT·I; Istall = V/R (back-EMF = 0)1
Safe voltage bandstalls below ~50% rated, overheats above ~130%1, 2
PWM averageVavg = duty × Vsupply2
H-bridgediagonal pair = direction; same side = SHORT3
Inductive kickbackVL = L·(dI/dt); clamp to ~0.7 V above rail4
Servo pulse1.0/1.5/2.0 ms = 0°/90°/180°; frame ~20 ms5
Servo angleangle = (pulse−1.0)/(2.0−1.0)×180°5
Stepperstep_angle = 360°/steps-per-rev; half-step halves it6
Driver ICsULN2003, L293/L298, SAA1027; flyback diodes 1N4001/27
The universal three. If you remember nothing else: PWM = control power by switching, not burning. H-bridge = reverse with four switches, never two on a side. Flyback diode = the inductor's escape valve. Every motor driver on earth is built from these.
The worked numbers, one more time. 135° servo → 1.75 ms pulse. 1.8° stepper → 200 steps/rev, 50 steps for 90°. 67% duty → 8 V avg on a 12 V motor → ~4000 rpm. Below 50% duty → stall. No flyback → a hundreds-of-volts spike → dead transistor.

Where this connects in the book

"Don't throttle a motor by burning the difference — switch it on and off and let inertia do the averaging."
— The whole chapter in one sentence

You can now drive a wheel, swing an arm, or position a print head — and protect every transistor on the way. Next, Chapter 16 turns electrons into sound.

You need precise, repeatable positioning over unlimited rotation with no feedback sensor. Which motor and what makes it suitable?
← Ch 14: Programmable Logic Ch 16: Audio →