---
title: Driving loads
heading: Transistor switches, inductive kicks and switching losses
eyebrow: C12 · Lesson 22 · Driving loads
xp: 40
---

::: hook
A pin supplies 3.3 V and about 8 mA: enough for an LED, nowhere near enough for a relay, a motor, a solenoid or a strip of lights. Between the pin and the load goes a transistor, and choosing it is where firmware meets power electronics. The pin must turn the transistor fully on, the transistor must not overheat, and when an inductive load turns off, the current it was carrying has to go somewhere, or the voltage spike destroys the transistor within a few switchings.
:::

::: play
- A GPIO pin (0 to 3.3 V) drives a logic-level MOSFET switching a 12 V relay coil (100 mH, 100 Ω). The scope shows the drain and the gate.
- RD is in series with the flyback diode. At 1 Ω, the diode catches the coil's current when the switch opens, and the drain only rises to about 12.9 V: the supply plus the diode's drop.
- Raise RD to 1 MΩ, as if the diode were missing: the coil drives the drain up until the MOSFET avalanches at 60 V, every cycle.
:::

```sim circuit
VS vs 0 12                    @ 0,0 down 6
L1 vs x 100m                  @ 3,0 down
RW x d 100                    @ 3,2 down
D1 d dd                       @ 5,4 up
RD dd vs 1                    @ 5,2 up
DZ 0 d BV=60                  @ 7,6 up 2
RLEAK d 0 100k                @ 10,4 down 2
M1 d g 0 nmos VTO=1.5 KP=1    @ 2,5
VG g 0 square 0 3.3 100 Hz    @ 2,5 down 2
slider: RD 1..1M log
scope: v(d), v(g)
time: 0..20m s
meter: v(d)
```

::: predict
## A 12 V, 100 Ω relay coil is switched by an NPN transistor with a minimum β of 100. Can a GPIO pin's 8 mA drive it into saturation reliably?

- [x] Yes: it needs a few milliamps of base current with a forced β of 20
- [ ] No: the coil needs 120 mA and the pin only gives 8
- [ ] Only with a MOSFET
- [ ] Only if β is exactly 100

::: reveal
The coil takes 120 mA. For a hard, reliable saturation the base gets $I_C/20$ = 6 mA, well below the β limit, through $(3.3 - 0.7)/6\text{ mA} = 430$ Ω. A logic-level MOSFET needs no steady gate current at all, which is why it's the usual choice above a few hundred milliamps.
:::
:::

::: nameit
## Low-side switches

The load goes from the supply to the transistor, and the transistor to ground, so the pin drives it relative to ground.
- **BJT:** saturate it with a **forced β** of 10 to 20: $I_B = I_C/\beta_\text{forced}$, $R_B = (V_\text{pin} - 0.7)/I_B$. It drops $V_{CE(\text{sat})}$, about 0.2 V.
- **MOSFET:** choose a **logic-level** part whose $R_{DS(\text{on})}$ is specified at $V_{GS}$ = 2.5 or 3.3 V; a part rated at 10 V may barely turn on from 3.3. It drops $I R_{DS(\text{on})}$.

## Losses

**Conduction:** $P = I^2 R_{DS(\text{on})} D$ for duty $D$. **Switching:** each transition passes through full voltage and full current at once for $t_s$, so
$$P_\text{sw} \approx \tfrac12 V I (t_r + t_f) f$$
The transition takes about $t = Q_g/I_g$, the gate charge over the gate current. A pin's few milliamps make microsecond transitions; a gate-driver chip's amperes make nanosecond ones.

## Inductive loads

A coil's current can't stop instantly. With nowhere to go, $L\,di/dt$ drives the switch's voltage up until something breaks down. A **flyback diode** across the coil carries the current around it, and it decays with $\tau = L/R$. That's slow: a relay releases late. A **Zener** in series with the diode lets the coil's voltage reach $V_Z$, so the current falls faster:
$$t = \frac{L}{R}\ln\frac{I_0R + V_Z}{I_1R + V_Z}$$
to fall from $I_0$ to $I_1$, at the price of the switch seeing $V + V_Z$.

## H-bridges

Four switches let a motor run both ways. If both switches on one side conduct together, they short the supply: **shoot-through**. Drivers add **dead time** between turning one off and the other on.
:::

::: example
## Size the base resistor for a 12 V, 100 Ω relay coil switched by an NPN from a 3.3 V pin with an 8 mA limit.

::: solution
1. **Collector current.**
   $$\begin{aligned} I_C &= \frac{12\ \text{V}}{100\ \Omega} \\ &= 120\ \text{mA} \end{aligned}$$
2. **Base current.** A forced β of 20 needs 6 mA, under the pin's limit.
3. **Base resistor.** It drops 2.6 V:
   $$\begin{aligned} R_B &= \frac{2.6\ \text{V}}{6\ \text{mA}} \\ &= 433\ \Omega \end{aligned}$$
   so 430 Ω.
4. **Why not drive it harder.** A forced β of 10 would need 12 mA, more than the pin can give.

**Answer:** 430 Ω, for a forced β of 20.
:::
:::

::: example
## A MOSFET with 2 µs of total transition time switches 2 A at 12 V and 20 kHz. Compare its switching loss with its conduction loss at 50 mΩ and 50% duty.

::: solution
1. **Switching loss.**
   $$\begin{aligned} P_\text{sw} &= \tfrac12 \times 12 \times 2 \\ &\quad \times 2\ \mu\text{s} \times 20\ \text{kHz} \\ &= 0.48\ \text{W} \end{aligned}$$
2. **Conduction loss.**
   $$\begin{aligned} P_\text{cond} &= 2^2 \times 0.05 \times 0.5\ \text{W} \\ &= 0.1\ \text{W} \end{aligned}$$
3. **Compare.** Driven straight from a pin, the switching loss dominates, five times the conduction loss.
4. **With a gate driver.** A driver making 10 ns transitions, 20 ns in all, cuts the switching loss to 4.8 mW.

**Answer:** 0.48 W of switching loss against 0.1 W of conduction loss, so switching dominates, about five times larger.
:::
:::

::: example
## A relay (100 mH, 100 Ω, 120 mA) releases when its current falls to 30%. How long does release take with a plain flyback diode, and with a 24 V Zener in series?

::: solution
1. **Time constant.**
   $$\begin{aligned} \tau &= \frac{L}{R} \\ &= \frac{100\ \text{mH}}{100\ \Omega} \\ &= 1\ \text{ms} \end{aligned}$$
2. **Diode only.**
   $$\begin{aligned} t &= \tau \ln\frac{1}{0.3} \\ &= 1.20\ \text{ms} \end{aligned}$$
3. **With the Zener.** Here 3.6 V is 30% of the coil's 12 V:
   $$\begin{aligned} t &= 1\ \text{ms} \times \ln\frac{12 + 24}{3.6 + 24} \\ &= 0.27\ \text{ms} \end{aligned}$$
   four and a half times faster.
4. **The cost.** The drain sees 36 V instead of about 13.

**Answer:** 1.20 ms with a plain diode, and 0.27 ms with the 24 V Zener, which puts 36 V on the drain.
:::
:::

::: check
kind: parts
given: V = 5 | 12 | 24; R = 50 | 100 | 400; b = 10 | 20

## An NPN switches a {V} V, {R} Ω coil from a 3.3 V pin, with a forced β of {b}.

::: part
answer: V/R*1000
unit: mA
tolerance: 0.5%

### What collector current flows (ignore $V_{CE(\text{sat})}$)?

::: misses
- [any] $V/R$.
:::
:::

::: part
answer: (3.3 - 0.7)/(V/R/b)
unit: Ω
tolerance: 0.5%

### What base resistor?

::: misses
- [3.3/(V/R/b)] Subtract the 0.7 V base-emitter drop.
- [any] $(3.3 - 0.7)/(I_C/\beta_\text{forced})$.
:::
:::
:::

::: check
kind: parts
given: I = 0.5 | 2 | 5; r = 20 | 50 | 120; D = 30 | 75; t = 40 | 60

## A MOSFET with $R_{DS(\text{on})}$ = {r} mΩ carries {I} A at {D}% duty; its package has {t} °C/W to ambient.

::: part
answer: I^2*r/1000*D/100
unit: W
tolerance: 0.5%

### What is its conduction loss?

::: misses
- [I^2*r/1000] Scale by the duty cycle.
- [any] $I^2 R D$.
:::
:::

::: part
answer: I^2*r/1000*D/100*t
unit: °C
tolerance: 0.5%

### How far above ambient does it run?

::: misses
- [any] Power × thermal resistance.
:::
:::
:::

::: check
kind: parts
given: V = 12 | 24 | 48; I = 1 | 3; t = 0.1 | 1 | 2; f = 1 | 20 | 100

## A switch turns {I} A at {V} V on and off at {f} kHz, with {t} µs per transition.

::: part
answer: 0.5*V*I*2*t*1e-6*f*1000
unit: W
tolerance: 0.5%

### What is its switching loss?

::: misses
- [0.5*V*I*t*1e-6*f*1000] Two transitions per cycle.
- [any] $\tfrac12 VI(t_r + t_f)f$.
:::
:::

::: part
answer: 0.5*V*I*2*t*1e-6*f*1000/(V*I)*100
unit: %
tolerance: 0.5%

### What fraction of the power it switches is that?

::: misses
- [any] Over $VI$.
:::
:::
:::

::: check
kind: parts
given: Q = 5 | 15 | 40; I = 8 | 100 | 1000

## A MOSFET's gate needs {Q} nC and is driven with {I} mA.

::: part
answer: Q/I*1000
unit: ns
tolerance: 0.5%

### About how long is each transition?

::: misses
- [any] $Q_g/I_g$.
:::
:::

::: part
answer: Q*1e-9*3.3*1e6*20e3/1e6*1000
unit: µW
tolerance: 0.5%

### What gate-drive power does 20 kHz switching at 3.3 V take?

::: misses
- [any] $Q_g V f$.
:::
:::
:::

::: check
kind: parts
given: L = 20 | 100 | 500; R = 50 | 100 | 400; z = 12 | 24 | 48

## A coil of {L} mH and {R} Ω carries its full 12 V current when the switch opens; it releases at 30% of that.

::: part
answer: L/R*ln(1/0.3)
unit: ms
tolerance: 0.5%

### How long does release take with a plain flyback diode?

::: misses
- [L/R] The current must fall to 30%, not to 1/e.
- [any] $(L/R)\ln(1/0.3)$.
:::
:::

::: part
answer: L/R*ln((12 + z)/(3.6 + z))
unit: ms
tolerance: 0.5%

### And with a {z} V Zener in series with the diode?

::: misses
- [any] $(L/R)\ln((I_0R + V_Z)/(I_1R + V_Z))$.
:::
:::

::: part
answer: 12 + z
unit: V
tolerance: 0.5%

### What peak voltage does the switch see then?

::: misses
- [any] Supply plus Zener (ignoring the diode's drop).
:::
:::
:::

::: check
kind: parts
given: I = 0.1 | 0.5 | 2; r = 30 | 300

## A pin-driven MOSFET has $R_{DS(\text{on})}$ = {r} mΩ at 3.3 V gate drive and carries {I} A continuously.

::: part
answer: I*r
unit: mV
tolerance: 0.5%

### What voltage does it drop?

::: misses
- [any] $IR$.
:::
:::

::: part
answer: I^2*r
unit: mW
tolerance: 0.5%

### What does it dissipate?

::: misses
- [any] $I^2R$.
:::
:::
:::

::: check
kind: choice

## An H-bridge's firmware switches the high side off and the low side on in the same instruction. What goes wrong?

- [ ] Nothing: one instruction is instantaneous
- [x] The outgoing switch turns off more slowly than the incoming one turns on, so both conduct briefly and short the supply
- [ ] The motor reverses
- [ ] The gate charge doubles

::: misses
- [1] The transistors take nanoseconds to microseconds to change; the instruction's timing doesn't make them simultaneous.
- [3] Reversal is intended; the problem is the overlap.
- [4] Gate charge is a property of the part.
:::
:::

::: check
kind: python

## Write `switch_losses(v, i, rds, qg, ig, f, d)`: a MOSFET switching current `i` at voltage `v`, on-resistance `rds`, gate charge `qg` driven by `ig` amperes (each transition takes `qg/ig`), at `f` Hz and duty `d`. Return (conduction, switching, total) in watts.

```python starter
def switch_losses(v, i, rds, qg, ig, f, d):
    return (0.0, 0.0, 0.0)
```

```python solution
def switch_losses(v, i, rds, qg, ig, f, d):
    cond = i * i * rds * d
    t = qg / ig
    sw = 0.5 * v * i * 2 * t * f
    return (cond, sw, cond + sw)
```

```python tests
c, s, t = switch_losses(12, 2, 0.05, 8e-9, 8e-3, 20e3, 0.5)
assert abs(c - 0.1) < 1e-9, "Conduction: 4 × 0.05 × 0.5 = 0.1 W."
assert abs(s - 0.48) < 1e-9, "Switching: two 1 µs transitions at 20 kHz, 0.48 W."
assert abs(t - 0.58) < 1e-9, "Total 0.58 W."
c, s, t = switch_losses(12, 2, 0.05, 8e-9, 1.0, 20e3, 0.5)
assert abs(s - 0.00384) < 1e-9, "With a 1 A driver the switching loss nearly vanishes."
```

::: right
Right. The two terms pull opposite ways: faster switching frequencies make smaller inductors but more switching loss.
:::
:::

::: explain
## Why does turning off a relay coil without a flyback diode damage the transistor that switches it?

::: model
The coil stores energy in its magnetic field, ½LI², and its current can't change instantly: $V = L\,di/dt$. When the transistor opens, the current tries to drop to zero in nanoseconds, so the coil generates whatever voltage it takes to keep it flowing. That voltage climbs past the supply until the transistor's junction breaks down and the coil's energy is dumped into it as an avalanche, every time it switches off. A diode across the coil gives the current a path around the coil instead, so the voltage stays at the supply plus a diode drop while the energy decays harmlessly in the coil's own resistance.
:::
:::

::: practice
::: check
given: I = 50 | 200; b = 10 | 20
answer: 2.6/(I/1000/b)
unit: Ω
tolerance: 0.5%

## Base resistor for {I} mA at forced β {b} from 3.3 V?

::: misses
- [any] $2.6\text{ V}/I_B$.
:::
:::

::: check
given: V = 12 | 48; I = 2 | 10; t = 0.05 | 0.5; f = 50 | 200
answer: V*I*t*1e-6*f*1000
unit: W
tolerance: 0.5%

## Switching loss: {V} V, {I} A, {t} µs per transition, {f} kHz?

::: misses
- [any] $VI t_s f$ with two transitions.
:::
:::

::: check
given: L = 50 | 300; R = 60 | 240
answer: L/R
unit: ms
tolerance: 0.5%

## Decay time constant of a {L} mH, {R} Ω coil with a flyback diode?

::: misses
- [any] $L/R$.
:::
:::
:::
