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:

Motor model (steady speed): + o---[ R ]---( E )---o - V = I x R + E E = Ke x w T = Kt x I V the voltage across the motor terminals (V) I the motor current (A) R the winding resistance (ohm) w the shaft speed (rad/s); rpm = w x 60 / (2 pi) Ke the back-EMF constant (V per rad/s, i.e. V.s/rad) Kt the torque constant (N.m per A) T the torque the magnetic field produces (N.m) In SI units Ke = Kt (same number: V.s/rad = N.m/A). Datasheets often give them in other units (V per krpm, mN.m per A, oz-in per A), so convert first: Ke [V per krpm] = Ke [V.s/rad] x 1000 x 2 pi / 60 = Ke x 104.72 and Kt [mN.m/A] = Ke [V.s/rad] x 1000. Three things follow from these equations alone: 1. Torque is set by CURRENT (T = Kt I), speed is set by VOLTAGE (w = (V - I R) / Ke). A motor is a "current to torque" and a "voltage to speed" machine. 2. When the shaft is held still (E = 0), the current is limited only by the winding: I(stall) = V / R. This is the maximum, and it is large. 3. Run with no load and the motor speeds up until the back-EMF almost cancels the supply: E = V - I0 R, so the current falls to a small "no-load current" I0 (friction and wind losses). No-load speed w0 = (V - I0 R) / Ke = about V / Ke.

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.

No-load speed: w0 = 8000 x 2 pi / 60 = 837.8 rad/s Ke = (V - I0 R) / w0 = (6 - 0.10 x 5.0) / 837.8 = 5.5 / 837.8 = 0.006565 V.s/rad = 0.6875 V per krpm Kt = Ke = 6.565 mN.m per A Stall current: I(stall) = V / R = 6 / 5.0 = 1.20 A (6 V x 1.2 A = 7.2 W, all of it heat in the winding: 12 times the rated running heat) Stall torque: T(stall) = Kt (I(stall) - I0) = 6.565 mN.m/A x (1.20 - 0.10) A = 7.22 mN.m (the shaft torque; the 0.1 A first goes to friction) Running at a load of 0.25 A: speed w = (V - I R) / Ke = (6 - 0.25 x 5) / 0.006565 = 723.5 rad/s = 6909 rpm torque T = Kt (I - I0) = 6.565 x (0.25 - 0.10) = 0.985 mN.m power P(out) = T w = 0.000985 x 723.5 = 0.713 W P(in) = V I = 6 x 0.25 = 1.50 W efficiency = 0.713 / 1.50 = 47.5 % (loss in the winding I^2 R = 0.0625 x 5 = 0.31 W; friction and other no-load losses I0 x E = 0.1 x 4.75 = 0.475 W; 0.713 + 0.31 + 0.475 = 1.50 W) Relations that save calculating every point: Speed falls LINEARLY with torque: w = w0 x (1 - T / T(stall)) (the straight "torque-speed curve" on a datasheet) Current rises linearly with torque: I = I0 + T / Kt Maximum MECHANICAL power is at half the no-load speed and half the stall torque: I = (V + I0 R) / (2 R) = (6 + 0.5) / 10 = 0.65 A speed 4000 rpm P(out, max) = (I - I0)(V - I R) = 0.55 x 2.75 = 1.51 W but its efficiency is only 6 x 0.65 = 3.9 W in, 1.51 W out = 38.8 % Maximum EFFICIENCY is at a much lighter load: I = sqrt(I0 x I(stall)) = sqrt(0.10 x 1.20) = 0.346 A speed 6208 rpm efficiency = (1 - sqrt(I0 / I(stall)))^2 = 50.6 % So a motor sized to give its job at a quarter of its stall torque runs cool, efficiently and with a long brush life.
Size for the stall, run at a fraction
Everything that carries motor current (the driver, the wires, the fuse, the battery) must survive the stall current, even if the motor normally draws a fifth of it, because a wheel against a wall, a jammed gearbox or simply the instant of switching on (the shaft is still, so E = 0) all draw V/R. For M1 that is 1.2 A against 0.25 A running: a driver rated “1 A continuous” would be marginal. The datasheet of the motor gives the stall current; the driver must have either a rating above it or a current limit, or the program must stop the motor within a second or two of a stall.

Reading a motor datasheet

Datasheet lineWhat it tells youM1 (6 V hobby motor)
Nominal (rated) voltageThe 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 voltage6 V
No-load speed and currentw0 and I0; gives Ke with R. A large I0 means a poor motor (friction)8000 rpm, 0.10 A
Stall torque and stall currentThe worst case for the supply and the driver; the torque at which the motor gives up7.2 mN·m, 1.2 A
Terminal resistance RWinding 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 V6.57 mN·m/A, 1455 rpm/V
Terminal inductance LSets the electrical time constant L/R and the PWM ripple (section 2)2 mH (τ = L/R = 0.4 ms)
Rated (continuous) torque and currentWhat the motor can do for hours without overheating; usually a quarter to a third of stallabout 0.25 A
Maximum efficiency and the current at which it occursWhere to aim the gearing50 % 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).

Gearbox ratio N, efficiency eg: speed(out) = speed(motor) / N torque(out) = torque(motor) x N x eg M1 with a 50:1 gearbox (eg = 0.70), motor at 0.25 A as above: speed = 6909 / 50 = 138 rpm torque = 0.985 mN.m x 50 x 0.70 = 34.5 mN.m = 0.35 kg.cm (a pull of 0.35 kg at 1 cm radius: a 6 cm wheel gets 0.12 kg of push) stall torque at the output = 7.22 x 50 x 0.70 = 253 mN.m (and the gears must survive it: cheap plastic gears strip when a gear motor stalls) power out = 0.713 x 0.70 = 0.50 W (same motor power, less gear loss: speed and torque trade, power does not appear from nowhere) Choosing N for a wheel: wheel speed (m/s) = (output rpm / 60) x pi x diameter. 138 rpm with a 6 cm wheel: 2.3 x 3.1416 x 0.06 = 0.43 m/s.

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:

Average voltage V(avg) = D x V(supply) so speed (no load) = (D x V - I0 R) / Ke (speed is nearly proportional to D, once the motor turns) Why the current is smooth: the inductance L resists changes in current. While the switch is ON the current rises, while OFF it falls (through the freewheel path, section 3). Peak-to-peak ripple (assumes the motor's back-EMF is steady and ignores R): dI(p-p) = V x D x (1 - D) / (L x f) largest at D = 0.5: V / (4 L f) M1 (L = 2 mH, R = 5 ohm, electrical time constant L/R = 0.4 ms) at 6 V, D = 0.5: f = 490 Hz (default Arduino analogWrite on pins 5 and 6 is 980 Hz; on pins 3, 9, 10, 11 it is 490 Hz): 6 x 0.25 / (0.002 x 490) = 1.53 A p-p f = 1 000 Hz: 0.75 A p-p f = 5 000 Hz: 0.15 A p-p f = 20 000 Hz: 0.0375 A p-p f = 31 372 Hz: 0.024 A p-p The motor's average current is only about 0.1 to 0.3 A, so 1.5 A of ripple means the current actually swings through zero and reverses every cycle: bad (heating, torque ripple, a whine at the PWM frequency). By 20 kHz the ripple is a few percent of the average.

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:

+VM o--------+--------------------+ | | [ Q1 ] [ Q2 ] high-side switches (connect a side to +VM) | | A -----+------( MOTOR )-----+----- B the motor is connected between the mid-points A and B | | [ Q3 ] [ Q4 ] low-side switches (connect a side to 0 V) | | 0 V o------+--------------------+ Q1 and Q3 are the left leg, Q2 and Q4 the right leg. Each leg's top and bottom switches must NEVER be on together. Q1 Q2 Q3 Q4 Result 1 0 0 1 FORWARD: current +VM -> Q1 -> A -> motor -> B -> Q4 -> 0 V (A is positive of B) 0 1 1 0 REVERSE: current +VM -> Q2 -> B -> motor -> A -> Q3 -> 0 V (B is positive of A) 0 0 1 1 BRAKE: both motor ends joined to 0 V through Q3 and Q4; the spinning motor's own back-EMF drives a current E/R through the short, which stops it quickly (a "short brake"; Q1 and Q2 on instead gives the same result) 0 0 0 0 COAST: all four off, the motor spins down freely (current may flow through the body diodes: see below) 1 0 1 0 SHOOT-THROUGH: +VM -> Q1 -> Q3 -> 0 V, a short circuit. Same for Q2 and Q4. (forbidden)

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.

Estimate of the turn-off time of a MOSFET driven through a gate resistor (illustrative; the datasheet's switching times are the authority): t(off) is about Rg x Ciss x ln(Vdrive / Vth) Example: Rg = 100 ohm, Ciss = 3.3 nF (IRLZ44N), Vdrive = 5 V, Vth = 2 V: t(off) = 100 x 3.3e-9 x ln(2.5) = 0.30 us Dead time = about 2 x t(off) = 0.6 us (a safety factor, because Ciss and the real transistor change with temperature and the Miller plateau slows the edge) At a PWM frequency of 20 kHz (period 50 us) that costs 0.6 / 50 = 1.2 % of the duty range: negligible. At 200 kHz it costs 12 %. Integrated drivers generate the dead time themselves (section 4); with a hand-built bridge the controller or a gate-driver chip such as the IR2104 does. The conduction loss of a bridge: P = I^2 x (Rds(high) + Rds(low)). Two 25 mohm MOSFETs at 3 A: 9 x 0.05 = 0.45 W. A bridge made of diodes or bipolar transistors that drops 2 V at 3 A would waste 6 W: the reason modern drivers use MOSFETs.

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:

Forward, PWM on Q1, Q4 held ON (motor current flows from A to B through the motor): Q1 ON (the "on" part of the cycle): +VM -> Q1 -> A -> motor -> B -> Q4 -> 0 V. The current rises. Q1 OFF (the "off" part): The motor current cannot stop; it must still flow A -> B through the motor. Its only source is Q3's body diode (anode at 0 V, cathode at A, forward biased because A is pushed below 0 V): 0 V -> Q3 body diode -> A -> motor -> B -> Q4 -> 0 V. A sits at about -0.7 V. The current falls. Loss in the freewheel path = 0.7 V x I (the diode drop). If Q3 is switched ON during the off part ("synchronous rectification", opposite to Q1, with the dead time) the current flows through the channel instead and the loss is only I^2 x Rds(on). Two ways to apply PWM, both used in drivers: Sign-magnitude: one input sets the DIRECTION (held), the other is the PWM; the off part is a slow decay (the motor current recirculates through the low side: "slow decay / brake"). Smooth, lowest ripple, and the motor's speed is proportional to the duty. (TB6612: PWM pin + IN1 / IN2.) Lock-antiphase: the two legs are driven with opposite signals, 50 % duty = stopped, 100 % = full forward, 0 % = full reverse. The supply current reverses every cycle (a "fast decay"); the ripple is much larger, but one pin gives both direction and speed. Rarely a good idea.

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):

DriverTechnologyMotor supplyCurrent per bridgeVoltage lost (high + low) at 1 AHeat in the chip at 1 ALogic and PWMNotes
L298N (the red module)Bipolar (BJT) transistorsabout 7 to 46 V2 A (two bridges)about 2.2 V (about 1.4 V upper plus 0.9 V lower)2.2 W per bridge: heatsink needed5 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
L293DBipolar, with clamp diodes4.5 to 36 V0.6 A (1.2 A peak), two bridgesabout 2.6 V at 0.6 A1.5 W at 0.6 A5 V logic; also the chip in the Tinkercad Circuits parts listSmall and simple; DIP-16 for breadboards; poor efficiency
TB6612FNGMOSFET2.5 to 13.5 V1.2 A continuous (3.2 A peak, short), two bridgesabout 0.5 V (R about 0.5 Ω high plus low)0.5 W2.7 to 5.5 V logic: works with Uno, Pico, ESP32; PWM up to 100 kHz; STBY pinThe usual choice for small robots; built-in dead time and diodes
DRV8833MOSFET2.7 to 10.8 V1.5 A RMS (2 A peak), two bridgesabout 0.36 V (R about 0.36 Ω total)0.36 WPWM straight on the IN pins; nSLEEP; accepts 3.3 V and 5 V inputs; current limit by a sense resistorIdeal for 3.3 V logic and 3 to 9 V motors; very efficient
Same job, 6 V supply, a 6 V motor drawing 1 A (the table's drops are taken at 1 A): motor volts heat in the driver share of the supply's energy reaching the motor L298N 6 - 2.2 = 3.8 V 2.2 W 3.8 / 6 = 63 % TB6612FNG 6 - 0.5 = 5.5 V 0.5 W 5.5 / 6 = 92 % DRV8833 6 - 0.36 = 5.64 V 0.36 W 5.64 / 6 = 94 % The motor's speed follows the voltage it actually gets; the L298N motor also loses torque. With a 12 V motor on 12 V the L298N loses 2.2 / 12 = 18 %, which is tolerable: the case for a bipolar driver is a large voltage, not a small battery. Rule of thumb: for a battery-powered robot below about 12 V, use a MOSFET driver. TB6612FNG truth table for ONE bridge (PWMA = the speed pin, STBY must be HIGH or the chip is asleep): AIN1 AIN2 PWMA STBY Out (AO1, AO2) H L H H forward (AO1 high, AO2 low) H, L, PWM: forward at the PWM duty (the off part is a short brake) L H H H reverse H H H or L H SHORT BRAKE (both outputs low) L L H or L H STOP (outputs off, the motor coasts) x x x L STANDBY (everything off, minimal supply current)

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.

Signal: a pulse every 20 ms (50 Hz); the pulse is HIGH for 1.0 to 2.0 ms (many servos accept 0.5 to 2.5 ms; check the datasheet) pulse width angle (for 1.0 ms = 0 deg, 2.0 ms = 180 deg) duty = pulse / 20 ms 1.00 ms 0 deg 5.0 % 1.25 ms 45 deg 6.25 % 1.50 ms 90 deg (centre) 7.5 % 1.75 ms 135 deg 8.75 % 2.00 ms 180 deg 10 % angle = (pulse - 1.0 ms) / 1.0 ms x 180 deg pulse = 1.0 ms + angle / 180 deg x 1.0 ms (a pulse of 1.72 ms means 129.6 deg) Resolution: 1 us of pulse width = 0.18 deg. A 16-bit hardware timer at 50 Hz (about 0.3 us per count) gives smooth motion; a software loop does not. A continuous-rotation servo uses the same pulse: 1.5 ms = stop, 1.0 ms = full speed one way, 2.0 ms = full speed the other.

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.
Servo supply (separate) Board (Uno, Pico or ESP32) +5 V o--------+-------------------------------------> servo red (supply) | [ C 470 uF ] (the + leg to the supply +, the stripe/- leg below) | 0 V o--------+-------------------------------------> servo brown or black (ground) | +-------------------------------------> board GND the ONLY link between the two supplies board PWM pin o-------[ 220 ohm ]---------------------> servo orange or yellow (signal)

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.

UnipolarBipolar
Wires5 or 6: each of two coils has a centre tap, which goes to the supply4: the ends of two coils (A and A-bar, B and B-bar)
DrivingEach half-coil switched to ground by one transistor per half: a simple ULN2003 transistor array does itEach coil needs its current reversed: an H-bridge per coil, which is what the A4988 and DRV8825 contain
TorqueLower: only half the coil is used at a timeHigher: all of the copper is used
Example28BYJ-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 wiresWith 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.

Current limit (the driver chops the coil current at this value): set it by measuring the trimmer voltage Vref (on the trimmer, against GND) with the meter. A4988 : I(max) = Vref / (8 x Rs) Rs is the sense resistor on the board: 0.068 ohm on current Pololu boards (older and clone boards may use 0.05 or 0.1) Vref = I(max) x 8 x Rs for 1.0 A: Vref = 1.0 x 8 x 0.068 = 0.544 V for 1.5 A: 0.816 V DRV8825: I(max) = Vref / (5 x Rs) Rs = 0.100 ohm: Vref = I(max) x 5 x 0.1 = 0.5 x I(max) for 1.5 A: 0.75 V for 1.2 A: 0.60 V Always set the limit at or below the motor's rated current per phase. Too high: the motor and the driver overheat; too low: lost steps. Microstep pins (A4988; the DRV8825 uses the same idea with MODE0 to MODE2 and reaches 1/32): MS1 MS2 MS3 step size steps per rev (200-step motor) L L L full 200 H L L 1/2 400 L H L 1/4 800 H H L 1/8 1600 H H H 1/16 3200 Step frequency = (rpm / 60) x steps per revolution: 300 rpm at 1/16: 5 rev/s x 3200 = 16 000 steps per second (the controller must produce a 16 kHz pulse train; a Pico's hardware or an Uno's timer can, a slow loop in a program cannot). Microstepping trade-off: the torque that pulls the rotor to the next micro-position is only about sin(90 deg / m) of the holding torque: m = 2: 71 %, m = 4: 38 %, m = 8: 20 %, m = 16: 10 %. So fine microsteps give smoothness, not strength or accuracy; the load-bearing steps are the full ones.
A4988 wiring (names on the module; the DRV8825 module has the same pin names except MODE0-2 for MS1-3 and no RESET/SLEEP link needed on most boards): Motor supply 8 to 35 V o------+--------------> VMOT Logic 3.3 V or 5 V (the board's supply) o-------> VDD | [ C 100 uF, 35 V ] (+ leg to VMOT; as close to the module as possible) | Motor supply 0 V o-------+--------------> GND (next to VMOT) board GND o--------------> GND (next to VDD) and the two GND pins are joined board pin o-----> STEP board pin o-----> DIR ENABLE: LOW = on (leave unconnected or take it to GND) RESET and SLEEP joined together MS1, MS2, MS3: leave unconnected for full step (internal pull-downs), or link to VDD for the table above Coil 1: 1A and 1B Coil 2: 2A and 2B (the two ends of each coil: one coil's two wires, the other coil's two wires) NEVER connect or disconnect a motor while the supply is on: the coil's current collapses into the driver and can destroy it. Without the 100 uF capacitor, the first switching spike can also destroy the module.

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).

Driving a relay coil from a logic pin (the low-side switch of Basic Electronics 6, here a 2N7000-class or 2N3904 stage): +V (coil supply) o-------+-----------------+ | | [ COIL ] D1 (1N4001): cathode (the stripe) | to +V, anode to the collector / drain +-----------------+----- collector (or drain) of the switching transistor emitter (or source) to 0 V; base (or gate) through the resistor of Basic Electronics 6 contact side: load supply o----[ load ]----o NO COM o----> load supply return Coil current I = V(coil) / R(coil). A 5 V relay with a 70 ohm coil: 5 / 70 = 71 mA power 0.36 W A 12 V relay with a 400 ohm coil: 12 / 400 = 30 mA power 0.36 W (both are far above what a microcontroller pin can supply: use the transistor) The coil is an inductor: the diode D1 is not optional (Basic Electronics 5): without it the turn-off spike is hundreds of volts and kills the transistor. Side effect: the diode holds the current in the coil, so the contacts release slower (a few ms longer); a 6 to 12 V Zener in series with D1 releases faster. Pull-in is typically at 75 % of the rated coil voltage and drop-out at 10 to 20 %: a coil on a 3.3 V line sags under a motor load and chatters.

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 relaySolid-state relay (AC triac type)Solid-state relay (DC MOSFET type) / plain MOSFET
Drop across the switchContact resistance 30 to 100 mΩ: 0.05 Ω at 8 A = 3.2 WAbout 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 supplyAC or DC, any voltage up to the ratingAC only (a triac cannot turn off DC)DC only; polarity matters (the + and − marks)
Switching speed5 to 15 ms; wears outTurns on at the next zero crossing of the mains; unlimited lifeMicroseconds, PWM possible
IsolationFull (coil to contacts)Full (opto-isolated)SSR: full. A plain MOSFET: none
Off statePerfect open circuitLeaks a few mA (a mains lamp may glow faintly)Very small leakage
NoiseClick and an arc that radiates noise; needs a flyback diode and suppressionSilentSilent
Use it forOccasional switching of anything up to its rating; mains; dissimilar voltages; a main battery cut-offFrequent switching of mains heaters and lampsRobot loads: motors, solenoids, strips (a MOSFET); isolated DC switching (the SSR)
Mains voltage
A relay or SSR is the usual way a microcontroller switches a mains appliance. The contacts and the mains wiring are lethal. Do not wire mains on a breadboard; use a ready-made, enclosed relay module with screw terminals and an earthed case, keep the low-voltage side and the mains side physically separate, never work on live wiring, and if you are not experienced with mains, do not build this yourself. Everything in this chapter’s builds is battery low-voltage.

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.

ChemistryCell voltage (nominal; full to empty)Typical AA / cell figuresInternal resistanceNotes for robots
Alkaline (AA, 1.5 V)1.5 V; 1.6 to 0.9 VAbout 2500 mAh at low current, much less at 1 A0.15 to 0.3 Ω per cell, rising as it emptiesCheap and everywhere, but sags badly under motor currents; not rechargeable
NiMH (AA, rechargeable)1.2 V; 1.4 to 1.0 V1900 to 2500 mAh; handles 1 to 2 A0.02 to 0.05 Ω per cellSafe, 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 V2000 to 3500 mAh; often 10 A or more for a good cell20 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 discharge5 to 30 mΩ per cellThe 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 V1500 to 6000 mAhLowSafer chemistry, flat discharge curve, 4 cells = 12.8 V; heavier than LiPo
Capacity and C-rate: a cell of capacity Q (Ah) at "1C" delivers Q amps (discharging fully in 1 hour); I = C-rate x Q 2200 mAh pack: 1C = 2.2 A; a 25C pack can deliver 25 x 2.2 = 55 A for short periods (enough to melt a wire: fuse it!) Needed C-rate = I(peak) / Q: 3 A from a 1.8 Ah pack = 3 / 1.8 = 1.67C: any decent cell copes. Runtime: t = (Q x usable fraction) / I(average) usable fraction about 0.8 (do not run a LiPo to empty; NiMH to 1.0 V) 2.2 Ah pack at 0.76 A average: 2.2 x 0.8 / 0.76 = 2.3 hours. (Capacity is quoted at low current; at high current expect 10 to 30 % less, and less in the cold.) Voltage sag under load: V(terminal) = V(open circuit) - I x R(internal) (this, not the nominal voltage, is what the robot sees at start-up; see section 10) a 2S LiPo (R about 60 mohm for the pack) at 10 A: 10 x 0.06 = 0.6 V lost; four half-used alkaline AAs (R about 1.2 ohm in all) at 1.4 A: 1.7 V lost. LiPo / Li-ion state of charge, open circuit, per cell (rough guide only): 4.20 V full; 3.95 V about 75 %; 3.85 V about 50 %; 3.75 V about 25 %; 3.5 V nearly empty. Under load the voltage reads lower than at rest, so use the pack's rest voltage, or a coulomb counter, rather than a reading taken while the motors run.

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.

Power lost in a linear regulator: P = (Vin - Vout) x I(out) (+ Vin x I(quiescent)) Efficiency = Vout / Vin Temperature rise = P x theta(JA) (theta = thermal resistance, C per W) 7805 (TO-220, 5 V, 1 A, dropout about 2 V, so Vin at least 7 V; theta about 65 C/W in free air; Iq about 5 mA): 9 V in, 0.5 A out: P = (9 - 5) x 0.5 = 2.0 W, rise 2.0 x 65 = 130 C (without a heatsink it overheats and shuts down) efficiency 5 / 9 = 56 % 7.4 V in, 0.333 A out (Build B): P = 2.4 x 0.333 + 7.4 x 0.005 = 0.84 W, rise 0.84 x 65 = 55 C (hot to touch but acceptable) efficiency 5 / 7.4 = 68 % AMS1117-3.3 (SOT-223, dropout about 1.1 V, Iq several mA, theta about 100 C/W on a small pad): 5 V in, 0.5 A out: P = (5 - 3.3) x 0.5 = 0.85 W, rise 85 C: too hot for a small pad; at 0.1 A: 0.17 W, rise 17 C: fine LDO (low dropout) parts, for example the MCP1700 or AP2112K (3.3 V, dropout 0.2 to 0.3 V, Iq tens of uA, 250 to 600 mA): allow a small input margin (a single Li-ion cell, 4.2 to 3.3 V, can feed a 3.3 V logic board through an LDO and keep regulating down to about 3.6 V). Pick a linear regulator when: the current is small (below about 100 to 200 mA), Vin is only a little above Vout, and quietness matters (an analogue sensor supply).

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).

Ideal buck: Vout = D x Vin (D = duty of the switch) Ideal boost: Vout = Vin / (1 - D) 7.4 V to 5 V: D = 5 / 7.4 = 0.676 3.7 V to 5 V (boost): D = 1 - 3.7/5 = 0.26 Inductor ripple (buck): dI(p-p) = (Vin - Vout) x D / (L x f) 22 uH at 500 kHz, 7.4 V to 5 V: 2.4 x 0.676 / (22e-6 x 500e3) = 0.147 A p-p (the choice of L sets how much ripple) Efficiency: eta = Pout / Pin = 85 to 95 % for a good converter at 20 to 100 % load; input current Iin = Pout / (eta x Vin) (it falls as Vin rises) 5 V, 0.35 A from 7.4 V at 90 %: Pin = 1.75 / 0.9 = 1.94 W, Iin = 0.263 A, loss 0.19 W (a linear regulator: Iin = 0.35 A, loss 0.84 W) Noise: the switching edges put ripple (tens of mV) at the switching frequency on the output, and radiated spikes with fast edges. For an ADC, an analogue sensor, or a radio: follow the converter with an LC filter or a linear (LDO) regulator ("post-regulation"), keep the converter away from sensor wiring, and decouple (section 11).
Linear (7805, AMS1117, LDO)Switching (buck, boost, buck-boost module)
EfficiencyVout / Vin: poor if the drop is large85 to 95 %, nearly independent of the voltage drop
Heat(Vin − Vout) × I; needs a heatsink above about 1 WSmall; the module stays cool
NoiseVery quiet, fast response, no external inductorRipple and spikes at the switching frequency; layout matters
Step up?NoBoost and buck-boost types can
PartsOne chip and two capacitorsA ready-made module; or a chip with an inductor, diode and capacitors
Typical robot use3.3 V for sensors from a 5 V rail; small loads5 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.

Budget for the small robot of Build B: 5 V rail (from a buck converter), mA typical peak Arduino Uno board (microcontroller, regulator, LEDs) 45 45 TB6612FNG logic supply 5 5 3 distance sensors (15 mA each) 45 45 1 micro servo (moving: about 250 mA; stalled: about 650 mA) 250 650 5 V rail total 345 745 x 1.3 margin = 450 and 970 mA Motor rail (battery directly), mA two M1 motors (0.25 A each running, 1.2 A each stalled) 500 2400 Battery current (2S LiPo 7.4 V): motors 0.50 A + buck input 5 V x 0.345 A / 0.9 / 7.4 V = 0.26 A = 0.76 A typical peak: 2.40 A + 5 V x 0.745 A / 0.9 / 7.4 V = 0.56 A = 2.96 A Choose: a buck converter rated 1 A or more (970 mA with margin); a fuse at 5 A (above the 2.96 A peak, under the 7 A of 22 AWG wire); a battery that gives 2.96 A (1.4C of 2.2 Ah: any LiPo). Runtime: 2.2 Ah x 0.8 / 0.76 A = 2.3 hours.

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.

Scenario: an ESP32 robot, the board on a 3.3 V AMS1117 (dropout about 1.1 V) from the same battery as two M1 motors (R = 5 ohm, 1.2 A stalled at 6 V). Battery: four half-used alkaline AAs: open circuit 5.2 V; internal resistance 4 x 0.3 = 1.2 ohm; wiring and connectors 0.1 ohm = 1.3 ohm in all. ESP32 with Wi-Fi: 0.24 A. At the moment both motors start (E = 0): the two 5 ohm windings in parallel are 2.5 ohm. Let V be the voltage at the motors and the regulator: V = 5.2 - 1.3 x (V / 2.5 + 0.24) V x (1 + 0.52) = 5.2 - 0.312 = 4.888 V = 3.22 V (the motors take 1.29 A, the total is 1.53 A) The regulator's output cannot exceed V - dropout = 3.22 - 1.1 = 2.1 V. That is below the ESP32 brown-out level (about 2.4 V): the board resets. The motors then stop, the voltage recovers, the board boots, starts the motors again: a repeating reset loop that looks like a software bug. The cures, in order of effectiveness: 1. Separate supplies (or a separate regulator input): the logic on its own cell or its own regulated rail, joined to the motors' battery only by the ground. 2. A buck-boost or a boost regulator for the logic (it can keep 3.3 V while its input falls to 2 V), instead of an LDO that needs 1.1 V headroom. 3. Limit the start-up current (a soft start): ramp the PWM from 0 to 100 % over 200 ms, or limit the duty to what the motor needs. With the motors held to 0.4 A in total, V = 5.2 - 1.3 x (0.4 + 0.24) = 4.37 V, the regulator gives about 3.27 V: no reset. (As the motors speed up, E rises and their current falls on its own.) 4. A bulk capacitor and a Schottky isolation diode: the diode (anode on the battery, cathode on the logic) stops the dip draining the logic's capacitor. The capacitor supplies the logic for t = C x dV / I: 1000 uF, 1.0 V of allowed droop, 0.24 A: 1000e-6 x 1.0 / 0.24 = 4.2 ms. That is only a few milliseconds: it bridges a spike, not a stalled motor that stays on for a second. (A diode costs 0.3 V too, which makes the headroom problem worse with an LDO.)

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.

Star ground: every load returns to the battery minus by its OWN wire (short and thick for the motors); only the wires that carry signal share a reference at the star point. +--------- motor driver GND (thick wire, carries all motor current) | battery - o-----------*star----- regulator GND (the logic rail's return) | +--------- servo supply GND (a separate wire; its 470 uF capacitor next to the servos) | +--------- sensor / ADC reference GND (a thin wire, nothing else on it) battery + o----[ FUSE ]----+----------> motor driver VM (with its bulk capacitor) | +----------> regulator input ---> logic rail (decoupled) Wrong way: battery - ---> motor driver GND ---> (the same wire continues) ---> microcontroller GND ---> sensor GND. The motor current's voltage drop along the wire shifts the microcontroller's and the sensor's "0 V" by tens of millivolts at each PWM edge. An ADC reading that wanders with motor speed is almost always this.

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.

P-channel MOSFET reverse-polarity protection (in the positive supply line): battery + o---------+ drain the MOSFET is placed so that its BODY DIODE (anode on the drain, cathode on the source) | points TOWARDS the load: the right way round, the diode conducts at power-up and the channel then turns on. [ P-FET ] gate ----+ | | source +--------------+--------> to the load (+) gate to 0 V: through a 100 k resistor to the battery minus, | (and, if the battery is above the gate limit of the part, usually 20 V, a Zener battery - o---------+---------------------------------> load (-) from gate to source: ask the datasheet for Vgs max) Right way round: the body diode conducts, the source rises to the battery voltage, Vgs = 0 - Vbat = -Vbat: the channel turns on (the drop falls to I x Rds(on)). Reversed: the load's ground rail is now joined to the battery's + terminal and the drain to its - terminal, so the drain is at -Vbat relative to the rail: the body diode (drain to source) is reverse biased, the source stays near 0 V, Vgs = 0: the MOSFET is off and nothing reaches the load. Loss: a P-channel part with Rds(on) = 30 mohm at 3 A: V = 0.09 V, P = 3^2 x 0.03 = 0.27 W against the Schottky diode's 1.35 W.

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 wiringDrop for 2 A over 1 m out and 1 m backTypical use
26 (breadboard jumper)about 133, plus 50 to 100 mΩ per contactUnder 1 A0.53 V and more (the contacts add)Signals only
2484.23.5 A0.34 VSensors, small servos
2253.07 A0.21 VSignal and light power wiring, small motors
2033.311 A0.13 VMotors and battery leads up to a few amps
1820.916 A0.084 VMain battery wiring
1613.222 A0.053 VHeavy motors, fast robots
Drop V = I x R(wire) x 2 x length; heating P = I^2 x R(wire) x 2 x length Example: 1.5 m each way of 22 AWG carrying 3 A: R = 0.053 x 3 m = 0.159 ohm V = 3 x 0.159 = 0.48 V (6.4 % of a 7.4 V pack, which lowers the motors' voltage and torque) P = 9 x 0.159 = 1.43 W lost in the wire. The same run in 18 AWG: 0.0209 x 3 = 0.063 ohm, V = 0.19 V, P = 0.56 W. For a short robot the heating limit rarely matters (the table's currents are for long runs); the voltage drop and the contact resistance of connectors do. Use soldered joints or screw terminals for motor current, crimped connectors (XT30, XT60, JST-XH) for batteries, and never push motor current through a breadboard's thin contacts above about 1 A: the contacts heat up, become unreliable and are a fire risk. Twisting each pair (+ and -) together reduces the noise radiated by switching currents.

13. Which board for motors and power?

JobBest boardWhy
One or two DC motors with speed and direction (this chapter’s Build A), 5 V driver parts, first projectArduino Uno or Nano5 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 PicoEight 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 walkerESP3216 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 servosRaspberry Pi with a PCA9685 servo board or a motor HATSoftware 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 axesPico (PIO), or any board plus A4988/DRV8825A 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.

Safety and order of connection
Use a separate battery pack for the motor (four AA cells, 6 V) and power the Uno from USB or its own supply; join only the grounds. Connect the motor pack last and disconnect it before any wiring change. Do not hold the motor shaft for more than a couple of seconds: stalled, it draws 1.1 A and the driver and the motor warm up. Use only alkaline or NiMH AA cells for Build A. Build B’s bench supply should have its current limit set to 1 A; if you use a LiPo pack, fit a 5 A fuse and follow section 8. Never connect a lithium pack to anything you have not checked with the meter.

Parts list

PartValueUsed in
BoardArduino Uno (or Nano) and a USB cableA
U1TB6612FNG breakout module (pins named VM, VCC, GND, STBY, AIN1, AIN2, PWMA, BIN1, BIN2, PWMB, AO1, AO2, BO1, BO2)A
M1Small 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
P110 kΩ linear potentiometerA
SW1Push button (tactile switch)A
C1100 µF electrolytic, 16 V (motor supply bulk capacitor; + leg to the motor +)A
C2, C3100 nF ceramic: C2 from VCC to GND at the module, C3 across the motor terminalsA
U27805 regulator (TO-220), with 0.33 µF ceramic on the input and 0.1 µF on the outputB
U3Adjustable buck converter module (LM2596 or MP1584 type), with input and output capacitors on the moduleB
RL15 Ω, 5 W (or two 33 Ω 2 W in parallel, about 16.5 Ω) load resistor: 5 V / 15 Ω = 0.333 A, 1.67 WB
Supply7.4 V: a bench supply (current limit 1 A) or a 2S Li-ion/LiPo pack with a protection board and a fuseB
Breadboard, jumpers, multimeterThe meter must have a 10 A (or at least 400 mA) current range; a second meter, or an oscilloscope, is usefulA, B

Build A: motor speed and direction with a TB6612FNG

Motor battery pack (4 x AA, 6 V) TB6612FNG module Arduino Uno + o--------+---------------------> VM D9 o---------> PWMA (speed; PWM) | D7 o---------> AIN1 [ C1 100 uF ] D8 o---------> AIN2 | VCC <-------------------------------- 5 V - o--------+---------+-----------> GND (motor side) GND o---------+----> GND (module) (and the - rail) | A0 o---------< P1 wiper +-------------------------------------------------- GND (the ONLY link between pack and Uno) STBY ----------------------------> 5 V (always enabled) BIN1, BIN2, PWMB ---------------> GND (unused bridge held off) AO1 ----+ +--( M1 )--+ (C3 100 nF across the motor terminals, soldered if you can) AO2 ----+ P1: one end to 5 V, the other end to GND, the wiper to A0 SW1: one side to D2, the other to GND (D2 uses INPUT_PULLUP: pressed = LOW) Current paths (forward, AIN1 = H, AIN2 = L, PWM on): battery + -> VM -> high-side FET (A) -> AO1 -> M1 -> AO2 -> low-side FET (A) -> GND -> battery - (reverse is the same through the other pair, with M1 supplied the other way round). In the PWM off part the current continues AO2 -> low side -> ... -> AO1 through the freewheel path inside the chip (section 3).
// Arduino Uno: pot sets the speed, the button reverses the motor (TB6612FNG on bridge A) const int PWMA = 9, AIN1 = 7, AIN2 = 8, POT = A0, BTN = 2; bool forward = true; bool lastBtn = HIGH; void setup() { pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMA, OUTPUT); pinMode(BTN, INPUT_PULLUP); TCCR1B = (TCCR1B & 0xF8) | 0x01; // Timer 1 prescaler 1: pins 9 and 10 now run at about 31.4 kHz (inaudible) } void drive(bool fwd, int duty) { digitalWrite(AIN1, fwd); // forward: AIN1 high, AIN2 low; reverse: the opposite digitalWrite(AIN2, !fwd); analogWrite(PWMA, duty); // 0 to 255: the duty cycle } void loop() { int duty = analogRead(POT) / 4; // 0 to 1023 becomes 0 to 255 bool btn = digitalRead(BTN); if (lastBtn == HIGH && btn == LOW) { // the button has just been pressed analogWrite(PWMA, 0); delay(300); // stop the motor first: reversing at speed asks the driver for a current spike forward = !forward; } lastBtn = btn; drive(forward, duty); delay(20); // also debounces the button }

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.

Grounds: jumper Uno GND -> top - rail; jumper top - rail -> bottom - rail (the one link between the two supplies) Potentiometer P1 (legs in row e): end A = column 5 row e, wiper = column 6 row e, end B = column 7 row e jumper column 5 row a -> top + rail; jumper column 7 row a -> top - rail; jumper column 6 row a -> Uno A0 Button SW1 (across the centre gap, legs in rows e and f): column 12 rows e/f and column 14 rows e/f (the two legs in one column are joined inside the switch) jumper column 12 row a -> top - rail; jumper column 14 row a -> Uno D2 Module power: VCC -> top + rail; GND -> top - rail; VM -> bottom + rail; STBY -> top + rail; BIN1, BIN2, PWMB -> top - rail Module signals: PWMA -> Uno D9; AIN1 -> Uno D7; AIN2 -> Uno D8 C2 (100 nF): column 20 row c -> column 22 row c; jumper column 20 row a -> top + rail, jumper column 22 row a -> top - rail (as close to the module as the board allows) C1 (100 uF): + leg column 30 row c, - leg column 32 row c; jumper column 30 row a -> bottom + rail, jumper column 32 row a -> bottom - rail Motor: AO1 -> one motor wire, AO2 -> the other (C3 across the two motor terminals) Connect the 5 V (USB) first, check that the module's power LED is lit (if it has one), connect the motor pack LAST. Measure: pin D9 average = red probe at the Uno D9 jumper; motor volts = red on AO1, black on AO2 (DC volts: the meter reads the average at 31 kHz); battery current = meter in series with the pack + lead (10 A range); all other voltages: black probe on the - rail.
  1. Pot fully towards end B (GND), duty 0: the motor does not move; D9 reads 0 V.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

Supply 7.4 V o----[ meter A (10 A range) ]----+----> regulator input (7805 pin 1, or the buck module IN+) | [ C 0.33 uF ] (for the 7805) | Supply 0 V o-------------------------------+-----------------------> regulator GND (7805 pin 2, or module IN- and OUT-) 7805 (TO-220, front facing you, pins left to right: 1 INPUT, 2 GROUND, 3 OUTPUT): OUT (pin 3) -> [ R_L 15 ohm ] -> GND; [ C 0.1 uF ] from pin 3 to pin 2 Buck module: first connect the input only, set the output to 5.00 V with the trimmer using the meter, no load, THEN connect R_L. OUT+ -> [ R_L 15 ohm ] -> OUT- Measure for each: Vin (at the regulator), Iin (the meter in series), Vout (across R_L), and the temperature (finger on the case for a moment, or an infrared thermometer). Compute: Pin = Vin x Iin Pout = Vout^2 / R_L efficiency = Pout / Pin loss = Pin - Pout.

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

MeasurementCalculatedReal (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 frequency31.4 kHz with the Timer 1 line; 490 Hz without31 kHz (a frequency meter or a scope)Whatever the simulator models
A: motor average volts at the same positions0 / 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 ofabout 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, reverse5 V / 0 V, 0 V / 5 V4.9 to 5.0 V / 0.00 V5 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 V0.9 to 1.2 AModel 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 WVout 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 WIin 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 buck0.338 − 0.256 = 0.082 A (24 % less)0.06 to 0.10 A—
B3: node N with the motors off / on5.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 switchFalls 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

Challenge 1: The motor, PWM and the bridge
(a) A 12 V motor has a winding resistance of 3.0 Ω, a no-load current of 0.15 A and a no-load speed of 6000 rpm at 12 V. Find Ke (in V·s/rad and V per krpm), Kt in mN·m/A, the stall current and the stall torque. At a load current of 0.8 A find the speed, the torque, the output power and the efficiency. Find the current and speed at maximum power and the maximum efficiency. Then fit a 30:1 gearbox of efficiency 0.65: find the output speed and torque at 0.8 A, and the output stall torque. (b) The winding inductance is 2.5 mH: find the electrical time constant, the peak-to-peak current ripple at D = 0.5 at 1 kHz and at 20 kHz (compare with 0.15 A), and the average voltage and the no-load speed at D = 0.7. (c) In a 12 V H-bridge built from four MOSFETs of 20 mΩ each, find the shoot-through current if one leg conducts at the same time, the conduction loss at 6 A in the bridge, and the dead time (twice the turn-off time) for Rg = 47 Ω, Ciss = 3.3 nF, Vdrive = 5 V, Vth = 2 V as a percentage of a 50 µs PWM period.
View solution
Challenge 2: Drivers, servos, steppers and relays
(a) A 6 V motor draws 1 A on a 6 V supply. For an L298N (2.2 V lost at 1 A), a TB6612FNG (0.5 V) and a DRV8833 (0.36 V), find the motor voltage, the power in the driver and the share of the supply energy that reaches the motor, and say which suits a battery robot. (b) A servo takes 1.0 to 2.0 ms for 0 to 180° at 50 Hz. Find the pulse and the duty for 30° and for 120°, and the angle for a 1.72 ms pulse. Three servos move at 0.25 A each and stall at 0.65 A each: find the supply current in each case and say why the supply must be separate. (c) A NEMA 17 motor is rated 1.2 A per phase. Find the Vref for an A4988 (Rs = 0.068 Ω) and for a DRV8825 (Rs = 0.100 Ω); find the steps per revolution at 1/8 microstepping and the step frequency at 120 rpm; find the angle per step for a 0.9° motor at 1/16. (d) A 12 V relay has a 400 Ω coil: find the coil current and power; and compare the loss in a mechanical relay (contact resistance 0.05 Ω) and in a triac SSR (1.2 V drop) switching an 8 A load.
View solution
Challenge 3: Power, brown-outs and wiring
(a) A robot runs from a 2S LiPo (7.4 V, 1.8 Ah). Its motors draw 0.8 A at the battery, and a 5 V rail supplies 0.5 A. Compare a linear regulator (5 mA quiescent) and a buck converter at 88 %: find the input current, the power lost, the total battery current and the runtime at 80 % usable capacity; and find the temperature rise of the linear part at 65 °C/W and of an AMS1117 at 100 °C/W (and say whether it is acceptable). (b) The logic shares a battery of open-circuit 5.4 V, total resistance 1.15 Ω, with motors that draw a total of 3 A at start-up. Find the voltage at the logic, say whether a processor with a 2.7 V brown-out threshold resets, and find the voltage if the soft start limits the motor current to 1 A. Find the capacitance that holds the logic for 10 ms at 70 mA if the voltage may fall by 1.1 V, and say why that is not a cure for a stalled motor. (c) A 3 A load is 1.5 m from the battery. Find the drop and the loss in 22 AWG and in 18 AWG wire (out and back), and choose a fuse for the 3.8 A peak of the robot (a 22 AWG wire is rated 7 A). State the C-rate required of the 1.8 Ah pack for 3 A.
View solution

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.