Unit 06 · Phase 2 · Movement & control
Motor drivers and encoders
Mission: Unit 05 chose a gearmotor that can climb and stop. Now make it observable: with the wheels lifted, drive each wheel forward, back and stop from the controller, and count its turns with an encoder, so every command gives repeatable signed counts.
The interactive version of this unit needs JavaScript. Below are its steps as text.
Steps
1. A controller pin cannot run a motor
The motor’s wires go straight to one controller output pin and ground. The wheel is lifted off the bench. Press the button to set the pin high.
Why it matters: An output pin is built to send information, not power: its safe limit is a few tens of milliamps. A motor needs hundreds of milliamps to run and over an amp at the moment it starts. On a real board this wiring can damage the pin, so only the model tries it.
2. A switch lets a small signal control a big current
Now the battery’s red power path runs through the motor and one electronic switch to ground. The pin’s blue signal only goes to the switch’s control input. Press the button.
Why it matters: An electronic switch (a transistor) lets a tiny control signal open or close a path that carries much more current. Power and information now travel on separate paths: red from the battery, blue from the controller. This switch can only turn the motor on and off in one direction.
3. Four switches: the H-bridge drives forward
Four switches now surround the motor, drawn like the letter H: S1 and S3 at the top, S2 and S4 at the bottom. Press the button to close S1 and S4.
Why it matters: This arrangement of four switches is an H-bridge. Closing one top switch and the opposite bottom switch puts the full supply across the motor one way round. A motor driver is a chip or board that contains H-bridges and switches them for you.
4. The other diagonal reverses
Open S1 and S4. Press the button to close S3 and S2 instead.
Why it matters: Swapping diagonals swaps which motor terminal is positive, so the motor turns the other way. The same trick explains the “swapped wires” fault: reversing the two motor wires has exactly this effect. Part C uses it.
5. Never close both switches on one side
Press the button to close S1 and S2, both on the left side.
Why it matters: Closing both switches on one side shorts the supply, called shoot-through, and can destroy the switches or the battery wiring. Driver chips take two input signals (here IN1 and IN2) and translate them into safe switch patterns. Both inputs high closes both bottom switches, which brakes the motor; both low opens all four, so it coasts.
6. Switch fast: pulse-width modulation
The scope under the wheel plots the driver’s output. Press the button to switch forward on for half of each cycle and off for the other half.
Why it matters: Switching on and off thousands of times a second is pulse-width modulation (PWM). The fraction of each period spent on is the duty cycle; the motor’s inertia smooths the pulses, so it responds to the average voltage, duty × supply. The scope is drawn hundreds of times slower than a real driver switches.
7. Duty cycle sets speed, above a dead zone
Press the button to sweep the duty cycle from 0% to 100% while the scope and readout follow.
Why it matters: The motor must overcome the gearbox and bearing friction you met in Unit 05 before it turns, so low duty cycles give no motion. Above that dead zone speed is roughly proportional to the average voltage. Real motors are not this straight; your encoder will show how yours behaves.
8. Supply, driver and motor must match
The 6 V pack is swapped for a 12 V supply. Press the button to check the ratings before anything is switched on.
Why it matters: Every part has ratings: the supply voltage must sit inside the driver’s range and at or below the motor’s rating, and the motor’s worst-case current (at stall, I = V ÷ R) must stay below the driver’s limit. These numbers are illustrative; read the datasheets of your own parts. Low voltage still needs care: a short at the battery can heat wires fast.
9. Signals need a common ground
The ground wire between the controller and the driver is removed. Press the button to send forward at 60%.
Why it matters: A voltage is always measured between two points. The driver reads its inputs against its own ground, so it needs a wire back to the controller’s ground to know what “high” means. Without it the inputs float and pick up noise, which looks like a faulty motor.
10. Slots and a light beam count turns
The wheel carries a disc with 20 slots. Sensor A shines a light through the disc. Press the button to turn the wheel one revolution slowly.
Why it matters: An encoder turns rotation into pulses the controller can count. A disc with 20 slots gives 20 pulses per revolution, its counts per revolution. Counting pulses tells you how far the wheel turned, but one sensor cannot tell forward from backward.
11. Two sensors tell direction
Sensor B sits a quarter of a slot behind A. Press the button to turn half a revolution forward, then half a revolution back.
Why it matters: Two channels a quarter period apart are called quadrature. Which one changes first gives the direction, and counting every edge of both (four per slot) gives 80 counts per revolution here. Signed counts are what the bench test checks.
12. Counts become distance and speed
Press the button to run forward at 60% for 2 s and count.
Why it matters: Revolutions = counts ÷ counts per revolution, and distance = revolutions × 2π × radius. Speed is counts in a time window, so a short window gives coarse speed steps: this is encoder quantisation. Longer windows or more counts per revolution make the steps finer.
13. Bench test both wheels
Wheels lifted. Press the button to run the left wheel, then the right, forward then reverse for 2 s at 60%, three times each, with a stop command after each run.
Why it matters: Running one wheel at a time with the wheels lifted keeps the test safe and the cause clear. Repeatable counts show the encoder is reliable; the sign shows whether command, motor and encoder agree. A whole wheel reading the wrong sign usually means swapped motor wires or swapped A and B wires.
14. Swap the wires and pass
With power off, the right motor’s two wires are swapped at the driver. Press the button to rerun the bench test.
Why it matters: The fix happens with the power off: change wiring only on an unpowered circuit. Fixing direction in software is also possible, but fixing the wiring keeps every later unit simple. Unit 07 uses these signed counts to work out where the rover went.
Go deeper
The equations behind the stage
average voltage V_avg = duty × V_supply stall current I_stall = V ÷ R_winding counts/rev CPR = slots × edges counted (1, 2 or 4) revolutions revs = counts ÷ CPR distance d = revs × 2π × r speed rpm = counts ÷ CPR ÷ window_s × 60 speed step Δrpm = 60 ÷ (CPR × window_s)
Syllabus math/physics line: PWM duty cycle and counts per revolution.
Quadrature decoding
Read A and B as a two-bit state that steps 00 → 10 → 11 → 01 → 00 one way and in reverse the other. Each step is ±1 count. A jump of two states means a missed edge: the controller read too slowly for the speed.
What this model leaves out
- Back-EMF, motor inductance and current ripple at the PWM frequency.
- Dead time inside the driver (a short pause between switching sides that prevents shoot-through).
- Voltage drop across the driver’s switches, battery sag and heating.
- Brake versus coast stopping distances measured on a real wheel (the model uses 2 and 9 counts).
- Encoders mounted on the motor shaft before the gearbox, which multiply counts per wheel revolution by the gear ratio.
Watch / read (from the syllabus)
- MIT 6.002 Lecture 1: circuit abstraction (archived 2000 lecture; basic algebra; selected foundations).
- Arduino built-in examples (for PWM output with analogWrite and reading digital inputs).
Video question to answer in your own words: how does an encoder make a motor observable?