Motors, Relays and Power
Basic Electronics
Course 1 · Chapter 9 · Motors, Relays and Power
So far the loads have been LEDs, small chips and a motor that was simply switched on and off. A robot is different: its motors need amps, they turn the other way when you ask them to, they spike and stall, and they share a battery with a microcontroller that resets if the supply dips for a few milliseconds. This chapter treats the actuators (DC motors, hobby servos, stepper motors, relays) and the power system (batteries, regulators, capacitors, grounding, fuses, wire) as one subject, because in practice they cannot be separated: most “mysterious” robot faults are a motor and a microcontroller fighting over the same few volts. It uses the transistor switch and the flyback diode of Basic Electronics 5 and Basic Electronics 6, the inductor behaviour of Basic Electronics 4, the PWM of Basic Electronics 8, the divider and power formulas of Basic Electronics 1 and Basic Electronics 3, and the grounding ideas of Basic Electronics 7. The next chapter, Basic Electronics 10, drives all of this from code.
1. The DC motor as a circuit
A brushed DC motor is a coil of wire (the armature) turning inside a magnetic field. For the circuit designer it behaves as two things in series: a resistor R (the copper winding, typically 1 to 30 Ω in hobby motors) and a voltage source E that opposes the supply. That source is the back-EMF: any conductor moving through a magnetic field generates a voltage, and the faster the shaft turns, the larger it is. A small inductance L (about 1 to 5 mH) sits in series as well, and matters for PWM (section 2). One motor, three numbers:
Worked example: one small hobby motor
Motor M1 (the same 6 V hobby motor as Basic Electronics 6) has, from its datasheet: nominal 6 V, winding resistance R = 5.0 Ω, no-load speed 8000 rpm and no-load current I0 = 0.10 A. Everything else can be calculated.
Reading a motor datasheet
| Datasheet line | What it tells you | M1 (6 V hobby motor) |
|---|---|---|
| Nominal (rated) voltage | The voltage at which the other figures are quoted; 50 to 150 % of it is usually safe for short use; the PWM average sets the real voltage | 6 V |
| No-load speed and current | w0 and I0; gives Ke with R. A large I0 means a poor motor (friction) | 8000 rpm, 0.10 A |
| Stall torque and stall current | The worst case for the supply and the driver; the torque at which the motor gives up | 7.2 mN·m, 1.2 A |
| Terminal resistance R | Winding resistance, measured with the shaft still; V/R = stall current. Check it with a meter on the 200 Ω range (it varies as you turn the shaft by hand, since brushes contact different coils: take the average) | 5.0 Ω |
| Torque constant Kt (mN·m/A) or speed constant (rpm/V) | Torque per amp; the reciprocal of Ke in matching units: 1 / (0.6875 V per krpm) = 1455 rpm per V | 6.57 mN·m/A, 1455 rpm/V |
| Terminal inductance L | Sets the electrical time constant L/R and the PWM ripple (section 2) | 2 mH (τ = L/R = 0.4 ms) |
| Rated (continuous) torque and current | What the motor can do for hours without overheating; usually a quarter to a third of stall | about 0.25 A |
| Maximum efficiency and the current at which it occurs | Where to aim the gearing | 50 % at 0.35 A |
Gear motors: trading speed for torque
A hobby motor spins at thousands of rpm with a few mN·m of torque; a wheel needs the opposite. A gearbox of ratio N divides speed by N and multiplies torque by N, minus the friction of the gears (efficiency ηg, 0.6 to 0.9 depending on type: a worm drive is about 0.4 to 0.6, spur gears about 0.8 to 0.9 per stage pair). The motor sees only the output torque divided by N, so its current for a given job falls with N, and the stall current of the motor is unchanged (the gearbox does not change V/R).
2. PWM speed control and its frequency
Basic Electronics 8 built a PWM generator from a 555. Every microcontroller has hardware PWM on several pins. The motor is switched fully on and fully off at a fixed frequency f; the fraction of each period that it is on is the duty cycle D. Because the winding inductance keeps the current smooth, the motor responds to the average voltage:
Choosing the frequency
- Above hearing. The motor and its wires act as a loudspeaker for the PWM frequency; anything from about 1 kHz up to 15 kHz sings. 20 kHz or more is silent to most adults. This is the main reason motor drivers are driven at 20 to 30 kHz.
- Well above 1 / (2 π τ) where τ = L/R. For M1 τ = 0.4 ms (400 Hz): any f above about 5 kHz keeps ripple small.
- Not too high. Each edge costs switching loss in the driver (Psw about ½ V I (trise + tfall) f, an estimate), and the driver chip has a maximum PWM frequency (the L298N is slow: use at most about 5 to 10 kHz; the TB6612 handles 100 kHz). At 31 kHz with 100 ns edges the loss is tiny; at 31 kHz with a slow L298N it is not.
- A coarser step at higher frequency. A timer counting 8 bits (256 steps) at 16 MHz gives 16 MHz / 510 = 31.4 kHz (phase-correct mode). Higher frequency means fewer counts per period: at 100 kHz on a 16 MHz timer there are only 160 steps. 31 kHz with 256 levels is a good compromise on an Arduino Uno; a Raspberry Pi Pico and an ESP32 have 16-bit hardware PWM and give 20 kHz with thousands of levels.
Dead zone and a gentle start
A real motor does not move until the voltage overcomes stiction (static friction), typically 15 to 30 % of rated voltage: for M1, if starting needs 0.2 A, D × 6 V must exceed 0.2 × 5.5 Ω = 1.1 V (the 5.5 Ω includes the driver’s 0.5 Ω), which is D = 18 %. Below that the motor hums and draws current without moving. A program maps 0 to 100 % of the command onto 18 to 100 % of the duty. Also, ramp the duty up over 50 to 300 ms rather than jumping from 0 to 100 %: at the instant of a jump the back-EMF is zero and the motor draws its full stall current (section 10 shows what that does to the supply).
3. The H-bridge: four switches, two directions
One transistor switches a motor on and off in one direction (Basic Electronics 6). To reverse a motor you must reverse the voltage across it. The H-bridge does this with four switches arranged as the letter H, the motor being the crossbar:
The bridge is just the four-way version of the switch of Basic Electronics 6: each of the four is a MOSFET with its gate driven by a signal. The control logic usually takes two inputs per motor (IN1 and IN2, which become the left-leg and right-leg commands) and one PWM input. The tables of the chips in section 4 look like this.
Shoot-through and dead time
A MOSFET does not turn off the instant its gate is pulled low: the gate capacitance (Ciss, Basic Electronics 6) has to discharge through the gate resistor. If Q1 is commanded off while Q3 is commanded on in the same instant, for a few hundred nanoseconds both are conducting and the supply is shorted through two tiny resistances. With RDS(on) of 25 mΩ each at 12 V that is 12 / (0.025 + 0.025) = 240 A (limited in practice by the supply, wires and the transistors’ own heating, but every pulse stresses them, and the supply droops, upsetting the logic too). The cure is dead time: after switching one transistor off, wait a short time before turning its partner on.
Freewheeling, body diodes and braking
A motor winding is an inductor; the current through it cannot stop instantly (Basic Electronics 4). When PWM switches one transistor off, the current must find another path. Every MOSFET has a body diode (a parasitic diode between source and drain, anode on the source for an N-channel part) which provides it automatically; this is the flyback protection of Basic Electronics 5 built in, so a bridge needs no external flyback diodes. Follow the current in the forward state with Q1 as the PWM switch and Q4 held on:
High-side drive
The low-side transistors are easy: gate to a logic signal, source to 0 V. The high-side transistors are harder. An N-channel MOSFET turns on when its gate is some volts above its source, and the source of Q1 sits at the motor voltage when Q1 is on. To keep Q1 on at 12 V the gate must go to about 22 V. Three solutions: a P-channel MOSFET (turns on with the gate below the source; simple, but its RDS(on) is two to three times higher than a comparable N-channel); an N-channel with a bootstrap capacitor, which a driver chip (IR2104, DRV8871) charges while the low side is on and then lifts the gate with (this is why such bridges cannot hold 100 % duty for ever); or a charge pump inside the driver (the DRV8833, TB6612 and most modern chips do this internally).
4. Integrated motor drivers
You will rarely build a bridge from four loose MOSFETs. Driver chips contain the bridge, the dead time, the high-side drive, the flyback paths and often thermal and current protection. Four common families, with typical datasheet values (the real datasheet of the exact chip is the authority):
| Driver | Technology | Motor supply | Current per bridge | Voltage lost (high + low) at 1 A | Heat in the chip at 1 A | Logic and PWM | Notes |
|---|---|---|---|---|---|---|---|
| L298N (the red module) | Bipolar (BJT) transistors | about 7 to 46 V | 2 A (two bridges) | about 2.2 V (about 1.4 V upper plus 0.9 V lower) | 2.2 W per bridge: heatsink needed | 5 V; PWM up to about 5 to 10 kHz (slow) | No internal diodes (the module adds slow 1N4007s). Wastes 2 V: a 6 V motor on a 6 V supply gets 3.8 V. Fine for 12 V motors at a modest current |
| L293D | Bipolar, with clamp diodes | 4.5 to 36 V | 0.6 A (1.2 A peak), two bridges | about 2.6 V at 0.6 A | 1.5 W at 0.6 A | 5 V logic; also the chip in the Tinkercad Circuits parts list | Small and simple; DIP-16 for breadboards; poor efficiency |
| TB6612FNG | MOSFET | 2.5 to 13.5 V | 1.2 A continuous (3.2 A peak, short), two bridges | about 0.5 V (R about 0.5 Ω high plus low) | 0.5 W | 2.7 to 5.5 V logic: works with Uno, Pico, ESP32; PWM up to 100 kHz; STBY pin | The usual choice for small robots; built-in dead time and diodes |
| DRV8833 | MOSFET | 2.7 to 10.8 V | 1.5 A RMS (2 A peak), two bridges | about 0.36 V (R about 0.36 Ω total) | 0.36 W | PWM straight on the IN pins; nSLEEP; accepts 3.3 V and 5 V inputs; current limit by a sense resistor | Ideal for 3.3 V logic and 3 to 9 V motors; very efficient |
5. Servos
A hobby servo is a small gear motor, a position sensor (a potentiometer on the output shaft) and a control circuit in one case. The circuit compares the commanded position with the potentiometer and drives the motor in whichever direction closes the gap: a complete feedback loop with no code on your side. It has three wires: ground (brown or black), supply (red, 4.8 to 6 V for a standard servo) and signal (orange or yellow). The command is a pulse, repeated every 20 ms (50 Hz): the width of the pulse sets the angle.
Current, spikes and a separate supply
A servo draws almost nothing while holding still (about 10 mA for a 9 g micro servo such as the SG90), a few hundred milliamps while moving, and a large stall current (about 650 mA for a micro servo, 2.5 A for a large metal-gear servo such as the MG996R) when the arm meets an obstacle or the command asks for an impossible position. Three servos moving together at 0.25 A each draw 0.75 A, and the same three stalling draw about 1.95 A. The motor is switched hard by a bridge inside the servo, so the current changes quickly, and the supply wires carry noisy spikes. Consequences:
- Do not power servos from the 5 V pin of an Arduino (it is limited by the USB port to about 500 mA total and shares the voltage with the processor) or from a board’s 3.3 V pin. Use a separate 5 V to 6 V supply (four NiMH cells give 4.8 V; a 5 V buck converter, section 9, works too).
- Join the grounds of the servo supply and the board; without a common ground the signal has no reference. This is the only connection between the two supplies.
- Put 470 µF to 1000 µF across the servo supply next to the servos, to supply the spikes locally.
- A 3.3 V signal (Pico, ESP32) usually works with a 5 V servo (the servo’s input threshold is typically about 2 V), but check it; put a 220 Ω resistor in the signal line to protect the pin.
- Set the position once and stop the pulses (detach) if the servo need not hold against a load: a holding servo draws current and buzzes. A servo holding against the load gets warm.
6. Stepper motors at circuit level
A stepper has several coils instead of one, and no brushes. Energise the coils in a repeating sequence and the rotor jumps one step at a time, so the position is set by counting steps, not by feedback (open loop). A typical motor has 200 steps per revolution (1.8° per step; a 0.9° motor has 400). Holding position costs full coil current even at rest, and a missed step (the load exceeded the torque at that speed) goes undetected.
| Unipolar | Bipolar | |
|---|---|---|
| Wires | 5 or 6: each of two coils has a centre tap, which goes to the supply | 4: the ends of two coils (A and A-bar, B and B-bar) |
| Driving | Each half-coil switched to ground by one transistor per half: a simple ULN2003 transistor array does it | Each coil needs its current reversed: an H-bridge per coil, which is what the A4988 and DRV8825 contain |
| Torque | Lower: only half the coil is used at a time | Higher: all of the copper is used |
| Example | 28BYJ-48 (5 V, geared, commonly quoted at 2048 steps per revolution in full-step mode, slow and weak, with a ULN2003 board) | NEMA 17 (e.g. 1.5 A per phase, 200 steps per revolution) with an A4988 or DRV8825 |
| Identifying the wires | With the meter on ohms and the motor disconnected: two wires belonging to the same coil read a low resistance (a few ohms), wires of different coils read open. A unipolar centre tap reads half the resistance of the full coil. Never guess the pairing and connect: reversed coil ends make the motor shudder instead of turn. | |
Why a stepper wants a higher supply: the coil’s L/R time
The rated current of a NEMA 17 motor (1.5 A) at its coil resistance (1.8 Ω) is only 2.7 V, so it looks as if a 3 V supply suffices. But each coil is an inductor (say 3.2 mH) and, when the controller switches the current on (or reverses it), the current rises as in Basic Electronics 4: τ = L/R = 3.2 mH / 1.8 Ω = 1.78 ms. At each step the current must change direction or magnitude in a fraction of the step time, and at 2.7 V it would need about five time constants (9 ms): the motor could only manage about 100 steps per second. With a 12 V supply the same current is reached in −τ ln(1 − 2.7 / 12) = 0.45 ms, and a chopper driver holds the current at the limit by switching the 12 V rapidly on and off (a tiny buck regulator per coil). That is why a 2.7 V motor is run from 12 V to 24 V: the supply voltage sets how fast current can change (and so the top speed), the current limit sets the torque.
Driver chips and the current limit
The A4988 and DRV8825 modules have two bridges, a chopper current limit set by a trimmer, and two logic inputs for motion: STEP (one rising edge = one step) and DIR (the direction). Three MS (mode) pins select microstepping: instead of energising the coils fully on or off, the driver feeds them with a sine-and-cosine pair of currents so the rotor can stop between full-step positions. With 1/16 microstepping a 200-step motor has 3200 steps per revolution (0.1125° per step) and runs much more smoothly and quietly.
7. Relays and solid-state relays
A relay is an electromagnet pulling a metal contact: a small current in a coil switches a separate, electrically isolated circuit. It is the oldest way to switch a load that is too big, too high-voltage or of the wrong kind (mains, AC, or a signal that must be isolated) for a transistor. The contacts are COM (common), NO (normally open, closed when the coil is powered) and NC (normally closed, open when powered).
Contact ratings: AC and DC are different
A typical 5 V hobby relay is stamped 10 A 250 VAC / 10 A 30 VDC. The two figures are not alike because a DC circuit has no zero crossing: when the contacts open, an inductive DC load (a motor, a solenoid) can sustain an arc that welds or erodes them. As a rule, derate to about half for DC inductive loads, add a flyback diode (or a snubber) across the load, and use a bigger contact than the arithmetic suggests. Contacts also bounce for 1 to 5 ms on closing (the debounce problem of Basic Electronics 8, now in the output), and wear out: a mechanical relay is rated for about 100 000 switchings at full load, so do not PWM a motor through one.
Solid-state relays (SSR)
A solid-state relay has no moving parts: an LED inside lights a photo-detector which turns on the output (a triac for AC, a pair of MOSFETs for DC). The input is a current (typically 3 to 32 V DC, 10 to 15 mA), electrically isolated from the output.
| Mechanical relay | Solid-state relay (AC triac type) | Solid-state relay (DC MOSFET type) / plain MOSFET | |
|---|---|---|---|
| Drop across the switch | Contact resistance 30 to 100 mΩ: 0.05 Ω at 8 A = 3.2 W | About 1.2 V: 8 A × 1.2 V = 9.6 W (heatsink needed) | RDS(on) of a few mΩ: a fraction of a watt |
| Load supply | AC or DC, any voltage up to the rating | AC only (a triac cannot turn off DC) | DC only; polarity matters (the + and − marks) |
| Switching speed | 5 to 15 ms; wears out | Turns on at the next zero crossing of the mains; unlimited life | Microseconds, PWM possible |
| Isolation | Full (coil to contacts) | Full (opto-isolated) | SSR: full. A plain MOSFET: none |
| Off state | Perfect open circuit | Leaks a few mA (a mains lamp may glow faintly) | Very small leakage |
| Noise | Click and an arc that radiates noise; needs a flyback diode and suppression | Silent | Silent |
| Use it for | Occasional switching of anything up to its rating; mains; dissimilar voltages; a main battery cut-off | Frequent switching of mains heaters and lamps | Robot loads: motors, solenoids, strips (a MOSFET); isolated DC switching (the SSR) |
Relay modules (a relay, a transistor, a flyback diode, an LED and often an opto-isolator on a small board) are active LOW in many cases (the board relay turns on when the input is pulled to ground), have a 5 V coil, and a jumper that decides whether the opto-coupler is powered from the same 5 V (convenient, not isolated) or from a separate supply (isolated). A 3.3 V board (Pico, ESP32) driving an opto-isolated active-low module works with the module’s own 5 V supply; check that the input current (about 5 to 15 mA) is within the pin limit.
8. Batteries
The power source decides how long the robot runs and whether it browns out. Four numbers define a battery: its nominal voltage (and the range it covers while discharging), its capacity (mAh or Ah: the charge it holds), its maximum discharge current (often expressed as a multiple of capacity, the C-rate) and its internal resistance, which makes the voltage sag under load.
| Chemistry | Cell voltage (nominal; full to empty) | Typical AA / cell figures | Internal resistance | Notes for robots |
|---|---|---|---|---|
| Alkaline (AA, 1.5 V) | 1.5 V; 1.6 to 0.9 V | About 2500 mAh at low current, much less at 1 A | 0.15 to 0.3 Ω per cell, rising as it empties | Cheap and everywhere, but sags badly under motor currents; not rechargeable |
| NiMH (AA, rechargeable) | 1.2 V; 1.4 to 1.0 V | 1900 to 2500 mAh; handles 1 to 2 A | 0.02 to 0.05 Ω per cell | Safe, forgiving, 4 cells = 4.8 V (a servo supply); a good first robot battery |
| Li-ion (18650, 3.6 or 3.7 V) | 3.6 or 3.7 V; 4.2 to 3.0 V | 2000 to 3500 mAh; often 10 A or more for a good cell | 20 to 60 mΩ | Large energy; needs a protection circuit and a proper charger |
| LiPo (pouch pack, 3.7 V per cell) | 3.7 V; 4.2 to 3.0 V; 2S = 7.4 V (8.4 V to 6.0 V), 3S = 11.1 V (12.6 V to 9.0 V) | 500 to 5000 mAh, 20C to 100C discharge | 5 to 30 mΩ per cell | The lightest, strongest pack for drones and fast robots; the most dangerous (below) |
| LiFePO4 (3.2 V per cell) | 3.2 V; 3.65 to 2.5 V | 1500 to 6000 mAh | Low | Safer chemistry, flat discharge curve, 4 cells = 12.8 V; heavier than LiPo |
Lithium cells: protection and safety
- Charge limits. A lithium cell is charged at constant current up to 4.20 V, then at constant voltage until the current falls to about C/10: the job of a lithium charger (TP4056 for a single cell, a balance charger for LiPo packs). Charging past 4.2 V, or a cell below 3.0 V (or one that has been deeply discharged and then charged), can overheat and ignite it. Never charge with a bench supply set to “about right”, and never charge a swollen or damaged pack.
- Discharge limit. Do not go below 3.0 V per cell (3.3 V while under load is a sensible cut-off). Use a protection circuit module (PCM / BMS) on single cells and packs, or a low-voltage alarm on LiPo packs: it cuts off at the over-discharge, over-charge and short-circuit limits. Bare, unprotected cells are for builders who add their own protection.
- Short circuits. A 25C 2200 mAh pack can push 55 A into a short: wires glow, insulation melts, the pack catches fire. Fuse the battery (section 12) close to the positive terminal, and never rest a loose lead where it may touch the other lead.
- Physical damage. A punctured, crushed or swollen lithium pack can ignite after a delay. Retire it, discharge it (a bulb or a resistor) and take it to a battery recycling point; do not put it in household bins.
- Storage and charging. Charge lithium packs on a non-flammable surface (a ceramic tile, a metal tray or a LiPo bag), not unattended, not on a bed or sofa. For storage over a week leave them at about 3.8 V per cell.
9. Regulators: linear against switching
A battery’s voltage changes as it discharges and does not suit a 5 V or 3.3 V chip. A voltage regulator turns a variable, higher input into a fixed output. There are two very different kinds.
Linear regulators: simple, quiet, and wasteful
A linear regulator is a transistor acting as a variable resistor, continuously adjusted to keep the output at the target. The extra input voltage is burnt as heat in that transistor; the current in is the same as the current out (plus a little for the regulator itself, the quiescent current). It needs the input to exceed the output by at least the dropout voltage, or it can no longer regulate.
Switching regulators: efficient, noisy
A buck converter (step-down) switches the input on and off at 100 kHz to several MHz through an inductor and a diode (or synchronous MOSFET); the inductor smooths the pulses (Basic Electronics 4), and the output is the average, just as in PWM motor control. A boost converter steps up, a buck-boost or SEPIC can produce an output either above or below the input (for a single Li-ion cell feeding 3.3 V or 5 V).
| Linear (7805, AMS1117, LDO) | Switching (buck, boost, buck-boost module) | |
|---|---|---|
| Efficiency | Vout / Vin: poor if the drop is large | 85 to 95 %, nearly independent of the voltage drop |
| Heat | (Vin − Vout) × I; needs a heatsink above about 1 W | Small; the module stays cool |
| Noise | Very quiet, fast response, no external inductor | Ripple and spikes at the switching frequency; layout matters |
| Step up? | No | Boost and buck-boost types can |
| Parts | One chip and two capacitors | A ready-made module; or a chip with an inductor, diode and capacitors |
| Typical robot use | 3.3 V for sensors from a 5 V rail; small loads | 5 V from a 7.4 V or 11.1 V pack for the board, servos and sensors; battery-powered builds |
10. Current budgets and brown-outs
A current budget lists every load on each supply rail with its typical and its peak current, then checks the sum against what the battery, the regulator and the wires can deliver. It is a table, ten minutes of work, and it prevents most of the faults of a first robot. Always include the peak column: the stall current of every motor, the servo stall, the Wi-Fi bursts of an ESP32 (about 240 mA average when transmitting, with spikes to 500 mA), and the inrush of any capacitive load.
Brown-outs: when the supply dips
Microcontrollers watch their supply: if it falls below a threshold (a brown-out detector: about 2.7 V on the ATmega328P of the Uno at its usual setting, about 2.4 to 2.8 V on an ESP32), they reset rather than run on a voltage too low for the logic to work reliably. A robot whose motors share a battery with its processor can brown out each time a motor starts: the motor’s stall current drags the battery voltage down across its internal resistance, and the processor sees the dip.
11. Bulk capacitors, decoupling and grounding
The capacitors of Basic Electronics 4 do three jobs on the motor side, at three different sizes:
- Bulk capacitor (100 µF to 1000 µF, electrolytic, rated at least twice the supply) across the supply of every motor driver and servo, as close as possible to it: a local reservoir that supplies the sudden current of switching edges and start-ups, so the wires (and the battery) need not. The ripple on a supply of capacitance C when the load draws a pulse of charge is dV = I × dt / C: a 1.2 A start-up pulse of 50 µs on 470 µF gives dV = 1.2 × 50e−6 / 470e−6 = 0.13 V.
- Decoupling capacitor (100 nF ceramic) across the supply pins of every chip, as in Basic Electronics 4 and Basic Electronics 8.
- Motor noise capacitor. A brushed motor sparks at its commutator, which radiates noise that disturbs the radio, the sensors and the microcontroller. Solder a 100 nF ceramic capacitor across the motor terminals, and (optionally) one from each terminal to the metal case (each 100 nF). The leads must be short, soldered on the motor itself.
Grounding: separate the high currents from the sensitive ones
Wires have resistance (section 12) and inductance, so a current through a ground wire creates a small voltage drop along it. If the motor’s return current (an amp or more, pulsing at the PWM rate) shares a piece of wire with a sensor’s ground, that drop appears in series with the sensor’s signal as noise (the supply ripple and ground noise of Basic Electronics 7). The remedy is a star ground: every return path goes back to one point, which is the battery’s negative terminal (or one point on the ground rail), instead of being daisy-chained through each other.
12. Protection and wiring: fuses, reverse polarity, wire gauge
Fuses and polyfuses
A fuse protects the wire and the battery, not the load: it must blow before the wire overheats and before a short-circuit current from a strong battery melts something. Choose it above the largest legitimate peak (the budget of section 10: 2.96 A, so 5 A) and below the rating of the thinnest wire it protects (22 AWG: about 7 A for chassis wiring). Use a fast-blow type for a lithium pack, close to the battery’s positive terminal. A polyfuse (resettable PTC fuse) is a part whose resistance rises sharply when heated by an overcurrent: it has a hold current (it stays conducting up to this, say 1.1 A) and a trip current (about twice the hold current, say 2.2 A); once tripped it stays high-resistance until the current is removed and it cools. It is slow (seconds), has some resistance (0.1 to 0.3 Ω) and is good on a USB port or a sensor supply; it will not protect against the 55 A short of a LiPo.
Reverse-polarity protection
Connecting a battery the wrong way round destroys chips and electrolytic capacitors in an instant, and is easy to do with a bare two-wire connector. A series diode (Basic Electronics 5) blocks reverse current but drops 0.3 to 0.7 V and burns VF × I: a 1N5819 Schottky at 3 A (0.45 V) wastes 1.35 W. A P-channel MOSFET in the positive rail (Basic Electronics 6) does the same with a tiny loss, as follows.
Wire gauge and voltage drop
Copper has resistance, so a long thin wire between the battery and the motor driver drops volts and wastes power. Choose the wire for both: heating (the current rating of the insulation) and the voltage drop over the length. The round-trip length (out and back) is what counts.
| Wire (AWG) | Resistance (mΩ per metre) | Safe current in chassis wiring | Drop for 2 A over 1 m out and 1 m back | Typical use |
|---|---|---|---|---|
| 26 (breadboard jumper) | about 133, plus 50 to 100 mΩ per contact | Under 1 A | 0.53 V and more (the contacts add) | Signals only |
| 24 | 84.2 | 3.5 A | 0.34 V | Sensors, small servos |
| 22 | 53.0 | 7 A | 0.21 V | Signal and light power wiring, small motors |
| 20 | 33.3 | 11 A | 0.13 V | Motors and battery leads up to a few amps |
| 18 | 20.9 | 16 A | 0.084 V | Main battery wiring |
| 16 | 13.2 | 22 A | 0.053 V | Heavy motors, fast robots |
13. Which board for motors and power?
| Job | Best board | Why |
|---|---|---|
| One or two DC motors with speed and direction (this chapter’s Build A), 5 V driver parts, first project | Arduino Uno or Nano | 5 V logic matches the TB6612 and L293D, hardware PWM on pins 3, 5, 6, 9, 10 and 11, analogWrite() in one line, the timer can be raised to 31 kHz, and Tinkercad Circuits simulates it; rugged pins |
| Four wheels, encoders, closed-loop speed, or 3.3 V parts (DRV8833) | Raspberry Pi Pico | Eight PWM slices (16 outputs) with 16-bit resolution, programmable I/O (PIO) blocks to count encoder pulses and produce step pulses precisely, 3.3 V logic that suits the DRV8833 directly |
| Many servos, Wi-Fi control from a phone, a small walker | ESP32 | 16 LEDC PWM channels for servos, wireless built in; but its Wi-Fi bursts and its brown-out threshold make a separate, solid supply essential (section 10) |
| A Linux robot with a camera and a set of servos | Raspberry Pi with a PCA9685 servo board or a motor HAT | Software PWM on a Raspberry Pi jitters (a visible servo twitch); a PCA9685 over I²C generates sixteen exact 50 Hz channels itself. Power the servos from their own supply, never the Pi’s 5 V pin |
| Stepper axes | Pico (PIO), or any board plus A4988/DRV8825 | A steady step pulse train at kilohertz rates needs hardware timing |
Basic Electronics 10 writes the code for these cases and Basic Electronics 11 compares the boards in full.
14. Build it: a motor with PWM speed and direction, and a power budget for a small robot
Build A drives M1 through a TB6612FNG driver from an Arduino Uno: a potentiometer sets the speed, a push button reverses the direction. The Uno is the most suitable board here, for the reasons in section 13. Build B works out and then measures the power system of a small robot: a current budget, a comparison of a 7805 linear regulator and a buck converter feeding the same load, and a brown-out test in the simulator.
Parts list
| Part | Value | Used in |
|---|---|---|
| Board | Arduino Uno (or Nano) and a USB cable | A |
| U1 | TB6612FNG breakout module (pins named VM, VCC, GND, STBY, AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, AO1, AO2, BO1, BO2) | A |
| M1 | Small 3 to 6 V hobby DC motor (about 0.1 A no-load, 1.2 A stalled) and a 4 × AA battery holder (6 V) | A |
| P1 | 10 kΩ linear potentiometer | A |
| SW1 | Push button (tactile switch) | A |
| C1 | 100 µF electrolytic, 16 V (motor supply bulk capacitor; + leg to the motor +) | A |
| C2, C3 | 100 nF ceramic: C2 from VCC to GND at the module, C3 across the motor terminals | A |
| U2 | 7805 regulator (TO-220), with 0.33 µF ceramic on the input and 0.1 µF on the output | B |
| U3 | Adjustable buck converter module (LM2596 or MP1584 type), with input and output capacitors on the module | B |
| RL | 15 Ω, 5 W (or two 33 Ω 2 W in parallel, about 16.5 Ω) load resistor: 5 V / 15 Ω = 0.333 A, 1.67 W | B |
| Supply | 7.4 V: a bench supply (current limit 1 A) or a 2S Li-ion/LiPo pack with a protection board and a fuse | B |
| Breadboard, jumpers, multimeter | The meter must have a 10 A (or at least 400 mA) current range; a second meter, or an oscilloscope, is useful | A, B |
Build A: motor speed and direction with a TB6612FNG
Breadboard layout for the small parts (the module and the Uno connect by jumper wires as in the diagram; many modules come with header pins you can place straight into the board, in which case follow the pin names, not positions). Top rails: + rail = 5 V from the Uno, − rail = GND. Bottom rails: + rail = motor pack +, − rail = motor pack − and the common ground.
- Pot fully towards end B (GND), duty 0: the motor does not move; D9 reads 0 V.
- Turn it a quarter of the way: duty 64/255 = 25 %. D9 about 1.25 V average, the motor voltage about 1.45 V, and the motor may still be at rest (stiction, section 2: it needs about 1.1 V to start). Turn it a little further and it starts to spin.
- Half way: D9 about 2.5 V, motor volts about 2.95 V, speed about 3500 rpm (if you have a tachometer or a phone app). Three quarters: motor volts about 4.45 V, about 5700 rpm. Fully: motor volts 5.95 V, about 7900 rpm.
- Press the button: the motor stops for 0.3 s and restarts in the other direction. Keep the meter on AO1 and AO2: the reading changes sign.
- Hold the shaft briefly (no more than two seconds) at full speed: the current rises to about 1.09 A (6 V / 5.5 Ω, the 0.5 Ω being the driver) and the motor volts fall to about 5.5 V, the driver’s drop. The chip warms; the speed does not return until you let go.
- Remove C1 for a test (battery disconnected first): at start-up and at reversal, the 5 V reading of the logic may glitch as the supply wire dips, and the motor is noisier. With C1 fitted the supply is steadier.
- Change the line
TCCR1B = ...to a comment (power cycle the board): the PWM falls to 490 Hz and the motor whines audibly at low speed, and the motor current ripple (section 2) is about fifty times larger.
Build B: a power budget and a regulator comparison
B1, the budget. Copy the table in section 10 onto paper and replace the figures by your own: measure the current of each load in turn with the meter in series (the Uno from a 5 V supply through its 5 V pin with the meter in series, each sensor from the 5 V rail, and the motor on its own pack: running, then stalled for a second at most). Add the margins and check the sum against the regulator, the fuse, the wire and the battery.
B2, linear against switching. Feed the same load, RL = 15 Ω from 5 V, from each regulator in turn, with 7.4 V at the input and the meter in series in the input lead.
B3, the brown-out, in the simulator only. The real test destroys cells; the model shows the effect safely. See the simulator notes below.
Simulators
Falstad Circuit Simulator (falstad.com/circuit). H-bridge: four MOSFETs from the Transistors group (P-channel at the top, N-channel at the bottom, so the gate drives are 0 V and 12 V logic levels), a 12 V source, and the motor modelled as a 5 Ω resistor in series with a 2 mH inductor (use Falstad’s own DC motor element if your version lists one). Switch the gates by logic inputs (Inputs and Sources) so that top-left and bottom-right are on and the other two are off, and read the motor voltage on the scope: ±12 V. Then deliberately switch both transistors of one leg on: the source current jumps to hundreds of amps (the model has no wire resistance), which is shoot-through. Turn the PWM leg off and watch the current continue through the body diode of the other low-side FET (add a diode across each MOSFET, anode at the source of the N-channel). Brown-out (B3): a 5.2 V source in series with a 1.3 Ω resistor (the pack and wiring), a node N after the resistor, two 5 Ω resistors in parallel in series with a 2 mH inductor and a switch from N to ground for the motors, and from N a Schottky diode to a node L carrying a 1000 µF capacitor and a 22 Ω load (the logic). Close the switch and read N and L on the scope; open it after a moment to see the recovery. Without the switch closed, N = 5.2 V; closed, N falls to about 3.4 V (two 5 Ω windings: 5.2 / (1.3 + 2.5) = 1.37 A), and L, held by the capacitor, falls much more slowly at first. The 5 V regulator comparison is a calculation unless your Falstad version lists a regulator part.
Tinkercad Circuits (tinkercad.com): the component list has the Arduino Uno, a DC motor, a hobby servo, a potentiometer, a push button, a battery pack, a multimeter, an oscilloscope and an L293D motor driver (the TB6612 is not listed). For Build A, wire the L293D as follows and paste the same sketch: pin 1 (EN1) to D9, pin 2 (IN1) to D7, pin 7 (IN2) to D8, pin 3 (OUT1) and pin 6 (OUT2) to the motor, pin 8 (VS) to the 6 V battery +, pin 16 (VSS) to 5 V, pins 4, 5, 12 and 13 to GND, pins 9 (EN2), 10 and 15 to GND, with the battery − joined to the Uno GND. The L293D is a bipolar driver that drops roughly 1.5 to 2.5 V, so the motor voltage is lower than with the TB6612 (section 4); the simulated motor shows the shape of the behaviour, not the same numbers. Tinkercad runs the sketch’s PWM at its own accuracy; the oscilloscope on D9 shows the duty cycle directly. A servo on D10 with the Servo library (myservo.write(angle)) reproduces section 5.
Expected readings
| Measurement | Calculated | Real (typical) | Simulator |
|---|---|---|---|
| A: D9 average at pot 0 / 25 / 50 / 75 / 100 % | 0 / 1.25 / 2.51 / 3.76 / 5.00 V (duty 0 / 25.1 / 50.2 / 75.3 / 100 %) | Within 0.1 V (the pot’s end positions are never exact) | Same (the oscilloscope shows the duty) |
| A: D9 frequency | 31.4 kHz with the Timer 1 line; 490 Hz without | 31 kHz (a frequency meter or a scope) | Whatever the simulator models |
| A: motor average volts at the same positions | 0 / 1.45 / 2.95 / 4.45 / 5.95 V (D × 6 V − 0.1 A × 0.5 Ω) | Within about 0.3 V (battery sag, motor and module differences) | 1 to 2.5 V lower (L293D) |
| A: motor starts at a duty of | about 18 % (1.1 V) | 15 to 30 % | Model dependent |
| A: speed at 25 / 50 / 75 / 100 % | 1380 / 3560 / 5750 / 7930 rpm | ±20 % | Model dependent |
| A: battery current at 25 / 50 / 75 / 100 % | 25 / 50 / 75 / 100 mA (D × motor current of 0.10 A) | Within a factor of 1.5 (the motor’s current rises a little with speed) | Model dependent |
| A: AIN1 / AIN2 forward, reverse | 5 V / 0 V, 0 V / 5 V | 4.9 to 5.0 V / 0.00 V | 5 V / 0 V |
| A: stall current at full duty (held for under 2 s) | 6 / 5.5 = 1.09 A; motor volts about 5.45 V | 0.9 to 1.2 A | Model dependent |
| B2: 7805, Vin 7.4 V, RL = 15 Ω | Vout 5.00 V, Iout 0.333 A, Iin 0.338 A, Pin 2.50 W, Pout 1.67 W, efficiency 66.6 %, loss 0.84 W | Vout 4.95 to 5.05 V; Iin 0.34 A; efficiency 65 to 67 %; case about 50 to 60 °C above ambient | (calculation) |
| B2: buck module at 88 % | Vout 5.00 V, Iin 0.256 A, Pin 1.89 W, loss 0.23 W | Iin 0.24 to 0.28 A; efficiency 85 to 93 %; module slightly warm; Vout ripple 20 to 60 mV p-p on a scope (the 7805 shows under 1 mV) | (calculation) |
| B2: input current saved by the buck | 0.338 − 0.256 = 0.082 A (24 % less) | 0.06 to 0.10 A | — |
| B3: node N with the motors off / on | 5.2 V / 3.4 V | (not measured; do not test on real cells) | 5.2 V / 3.4 V |
| B3: node L after closing the switch | Falls by 0.2 V per ms (I/C = 0.2 A / 1000 µF) from 4.9 V, then follows N minus 0.3 V | — | Falls and follows N minus the diode drop |
Real readings spread because the motor, the pot and the batteries are all loose-tolerance parts. What does not vary is the structure: speed rises with the average voltage and starts only above the stiction threshold; reversing swaps the sign of the motor voltage; the stall current is limited only by the winding and driver resistances; the linear regulator’s input current equals its output current, so its efficiency is Vout/Vin whatever the load, while the buck converter’s input current is lower as the input voltage is higher; and a battery with internal resistance sags under a stalled motor by exactly I × Rinternal.
15. Challenges
Quick reference
- DC motor: V = I R + Ke ω; T = Kt I; Ke = Kt in SI; stall current V/R; no-load speed (V − I0R)/Ke; speed falls linearly with torque; maximum power at half stall torque; maximum efficiency (1 − √(I0/Istall))2; size every part for the stall current
- Gear motor: speed / N, torque × N × ηg; the motor’s stall current is unchanged
- PWM: Vavg = D × V; ripple ΔI = V D (1 − D) / (L f); use 20 kHz or more (silent, low ripple) if the driver allows; dead zone about 15 to 30 %; ramp the duty at start-up
- H-bridge: Q1+Q4 forward, Q2+Q3 reverse, Q3+Q4 brake, none = coast; never both switches of one leg (shoot-through = V / (R1 + R2)); dead time about 2 × Rg Ciss ln(Vdrive/Vth); body diodes give the freewheel path; loss I²(Rhigh + Rlow)
- Drivers: L298N loses about 2.2 V (BJT; for 12 V and up); TB6612 about 0.5 V at 1 A (1.2 A continuous); DRV8833 about 0.36 V (1.5 A RMS, 10.8 V max); L293D 0.6 A; use a MOSFET driver below 12 V
- Servo: 50 Hz; 1 to 2 ms = 0 to 180° (1.5 ms centre); separate 5 to 6 V supply with 470 µF; common ground; stall 0.65 A (micro) to 2.5 A (large)
- Stepper: 200 steps per revolution (1.8°); bipolar with A4988 (I = Vref / (8 Rs), Rs = 0.068 Ω) or DRV8825 (I = Vref / (5 Rs), Rs = 0.1 Ω); supply 8 to 35 V with 100 µF at the module; microstep torque about sin(90°/m); never connect the motor live
- Relay: coil current V/R (70 Ω, 5 V: 71 mA); flyback diode across the coil; derate DC inductive contacts to about half; SSR: triac for AC only (1.2 V drop), MOSFET type for DC; no mains on a breadboard
- Batteries: LiPo 3.0 to 4.2 V per cell (3.7 V nominal, 2S 7.4 V); NiMH 1.0 to 1.4 V; runtime = Q × 0.8 / I; C-rate = I / Q; sag = I Rinternal; protection, fuse, fire-safe charging; never below 3.0 V per cell
- Regulators: linear P = (Vin − Vout) I, efficiency Vout/Vin, needs dropout headroom (7805 2 V, AMS1117 1.1 V, LDO 0.2 V); buck Vout = D Vin, 85 to 95 %, noisy; boost Vout = Vin/(1 − D)
- Budget and brown-out: list typical and peak current per rail, add 30 %; start-up dip = Istart × Rinternal; below the processor’s threshold (about 2.7 V on an Uno, about 2.4 V on an ESP32) it resets; cures: separate supplies, buck-boost, soft start, bulk capacitor (t = C ΔV / I)
- Capacitors and grounds: bulk 100 to 1000 µF at every driver and servo, 100 nF at every chip, 100 nF across a brushed motor; star ground to the battery minus; thick wires for motor current
- Protection and wire: fuse above the peak and under the wire rating; polyfuse hold/trip; P-channel MOSFET for reverse polarity (loss I²R) rather than a diode (VF I); drop = I × Rwire × 2 × length (22 AWG 53 mΩ/m, 18 AWG 21 mΩ/m)
Next, Basic Electronics 10 puts a microcontroller board at the centre of these circuits: digital inputs and outputs, the ADC for sensors, hardware PWM for the motor of this chapter, interrupts for encoders and bumpers, and how to read a sensor and drive a motor safely, with the most suitable board chosen for each example.