UNIT 07 · LESSON 5 OF 6

Driving Loads and Protecting Pins

What does a pin need around it to drive real loads safely, and to survive the outside world?

INTERACTIVESwitching off an inductive load
The voltage across a transistor switching off an inductive load, with or without a flyback diodevoltage across the switchswitch opens≈ 5.7–6 Vthe diode clamps the switch one diode drop above the supply: 5 V + VF ≈ 5.7–6 V atthese currentsthe coil current then decays with τ = L/R = 1.4 ms (R = 5 V / 70 mA = 71 Ω): the relayreleases a little later
The voltage across a transistor switching off an inductive load, with or without a flyback diodevoltage across the switchswitch opens≈ 5.7–6 Vthe diode clamps the switch one diode drop above thesupply: 5 V + VF ≈ 5.7–6 V at these currentsthe coil current then decays with τ = L/R = 1.4 ms(R = 5 V / 70 mA = 71 Ω): the relay releases alittle later

Try this

Coil inductance
Coil current
Transistor switch-off time
Remove the flyback diode to compare the unclamped switch-off spike.
Clamped at about 5.7–6 V; current decays with τ = 1.4 ms.

A relay coil or motor is an inductor: its current cannot stop instantly. When the transistor switches off, v = L·di/dt, so without a path for the current the voltage across the switch rises until something breaks down. A flyback diode across the coil gives the current a loop: the switch sees only the supply plus one diode drop, and the current decays with time constant L/R instead. Coil values are illustrative.

What you will be able to do
  • Check a set of loads against per-pin and total current budgets, and decide which need a driver transistor.
  • Explain the voltage spike when an inductive load is switched off and size a flyback diode.
  • Make a load safe during reset and programming, when the controlling pin is not yet configured.
  • Calculate the current injected into a protection diode by an over-voltage input and choose a series resistor.
  • List protective measures for pins that leave the board.
Before you start
  • Pin current limits, LEDs and the MOSFET low-side switch (unit 1, lesson 5).
  • Push-pull outputs (lesson 2) and pad reset state (lesson 1).
Steps in this lesson
  1. Budget the current, then use a driver
  2. Inductive loads kick back
  3. Loads during reset and programming
  4. Protecting inputs
  5. Worked example: a 5 V relay from a 3.3 V pin
  6. Common misconceptions

The puzzle

The relay clicks once, and a week later the transistor driving it is dead. A second board resets itself every time the motor stops. A third turns its heater on for half a second at every power-up, before the firmware has even started. None of these is a logic error. What does a pin need around it to drive real loads safely, and to survive the outside world?

STEP 1

Budget the current, then use a driver

A pin is specified for a modest current, and so is the whole chip: the RP2040 characterises its pins at 2 to 12 mA drive settings and allows at most 50 mA sourced (and, separately, 50 mA sunk) by all pads together; the ATmega328P characterises its outputs at 20 mA, lists 40 mA per pin as an absolute maximum (a damage limit, not an operating budget) and caps groups of pins at 100–150 mA (unit 1, lesson 5). Add up every load that can be on at the same time:

Itotal=∑pins onIpin≤Ichip limitI_{\text{total}} = \sum_{\text{pins on}} I_{\text{pin}} \le I_{\text{chip limit}}

Anything beyond a few milliamps per pin, or a total near the chip limit, goes through a driver: a MOSFET on the low side (unit 1, lesson 5), a transistor array for many LEDs, or a dedicated driver chip for motors.

STEP 2

Inductive loads kick back

Relay coils, solenoids and motors store energy in a magnetic field, and their current cannot stop instantly. When the transistor switches off, the coil keeps pushing current and raises the voltage until the current finds a path:

v=L didt≈L Itoffv = L\,\frac{di}{dt} \approx L\,\frac{I}{t_{\text{off}}}

↑ This step uses the figure at the top of the page.

A flyback diode, reverse-biased across the coil, gives the current a loop. The transistor then sees only the supply plus one diode drop, and the energy dissipates in the coil’s resistance with time constant L/R. The diode must handle the coil current (a 1N4148 is rated 200 mA continuous) and the supply voltage. The price is a slower release: the relay lets go a few milliseconds later. Where release speed matters, a Zener diode in series with the flyback diode clamps at a higher voltage and lets the current die faster.

STEP 3

Loads during reset and programming

Between power-up and the firmware configuring a pin, the pin is whatever its reset state is. On the RP2040 the pad resets with its pull-down on and no function selected, so a MOSFET gate on that pin is held low. On chips whose pins reset as floating inputs, the gate can drift high and switch the load on, and the same happens while a debugger holds the chip in reset. A debugger that merely halts the core leaves the outputs as they were, so a load that was on stays on while you sit at a breakpoint; design the load, and your debugging habits, for that. A resistor from gate to ground (tens to hundreds of kΩ) keeps the load off whenever nothing drives the pin; choose which state is safe and wire the pull to that state.

STEP 4

Protecting inputs

Most pins have protection diodes from the pin to VDD and to ground. A signal above VDD plus a diode drop forward-biases the upper diode and pushes current into the supply rail; a signal below ground pulls current out through the lower one.

INTERACTIVEA 5 V signal into a 3.3 V pin
The current forced into a pin’s protection diode by an over-voltage input5.0 V → 1 kΩ → pin (VDD = 3.3 V)I = (Vin − VDD − V_diode) / R = (5.0 − 3.3 − 0.5) / 1000 Ω = 1.2 mAAbove the 1 mA limit: use a larger resistor, a level shifter or a divider.
The current forced into a pin’s protection diode by an over-voltage input5.0 V → 1 kΩ → pin (VDD = 3.3 V)I = (Vin − VDD − V_diode) / R = (5.0 − 3.3 − 0.5) /1000 Ω = 1.2 mAAbove the 1 mA limit: use a larger resistor, a levelshifter or a divider.
Series resistor
Injection limit in your datasheet
1.2 mA into the clamp (limit 1 mA): too much.

Most MCU pins have protection diodes to their supply. A signal above VDD plus one diode drop forward-biases the upper diode and pushes current into the chip’s supply rail. A series resistor limits that current; the datasheet states how much injected current, if any, is allowed. Some pins are specified as 5 V tolerant and have no diode to VDD; check before relying on this. Diode drop 0.5 V and the limits offered are illustrative.

A series resistor limits that current: I=(Vin−VDD−VD)/RI = (V_{in} - V_{DD} - V_{D})/R. Check the datasheet for how much injected current, if any, is allowed; some parts forbid it entirely, and some specify certain pins as tolerant of 5 V with no diode to VDD. Injected current can also raise VDD if the rest of the board draws less than is being injected, powering the chip through its input. For signals that leave the board (cables, connectors), add a series resistor, keep the lines away from supply pins on the connector, and consider dedicated ESD protection.

STEP 5

Worked example: a 5 V relay from a 3.3 V pin

A relay coil is rated 5 V, 70 mA (coil resistance about 71 Ω).

  1. Driver. 70 mA is far beyond any pin: use an N-channel MOSFET specified with its on-resistance at 2.5 V or lower gate voltage (unit 1, lesson 5), pin to gate through about 100 Ω.
  2. Flyback diode. A 1N4148 across the coil, cathode to +5 V: 70 mA is within its 200 mA rating and 5 V far below its 100 V. Without it, a 100 mH coil switched off in 1 µs would try to produce v≈0.1×0.07/10−6=7000v \approx 0.1 \times 0.07 / 10^{-6} = 7000 V; the transistor avalanches long before that.
  3. Reset state. A 100 kΩ resistor from gate to ground keeps the relay off during reset and programming.
  4. Release time. With the diode, τ=L/R≈0.1/71≈1.4\tau = L/R \approx 0.1/71 \approx 1.4 ms (taking an illustrative 100 mH coil); the current falls to 10 % of its initial value in about 2.3τ ≈ 3.2 ms, and the relay releases once it drops below its drop-out current, somewhere in that interval.

MYTHS AND FACTS

Common misconceptions

If each pin is within its rating, the chip is fine

The sum over pins has its own, lower limit.

The flyback diode is optional for small relays

Even a small relay’s L·I/t_off reaches hundreds of volts or more, and it arrives at every switch-off.

Protection diodes make any input voltage safe

They survive only a limited current; without a series resistor they can be destroyed or power the chip through the input.

The pin is off until the firmware turns it on

It is in its reset state, which may be floating, and the load follows it.

Check yourself

Answer in your head, then open the card.

Eight LEDs at 8 mA each run from RP2040 pins at the same time. Is that within the budget?

No, if they are wired pin → LED → ground: 64 mA of sourced current exceeds the RP2040's 50 mA total, even though each pin runs at its 8 mA characterised point. The series resistors set the current, not the drive setting: size them for 4 mA each (32 mA), or use a transistor array.

A 12 V solenoid draws 300 mA. Can a 1N4148 be its flyback diode?

Not as a continuous rating: 300 mA exceeds its 200 mA IF, although the decay is brief (its repetitive peak rating is 450 mA). A 1 A rectifier diode is the safe choice.

A 5 V signal drives a 3.3 V input through 10 kΩ. How much current flows into the protection diode (take the diode drop as 0.5 V)?

(5 − 3.3 − 0.5) / 10 kΩ = 0.12 mA. Compare with the datasheet's injection limit.

A motor driver MOSFET's gate is connected directly to an STM32 pin that resets as a floating input. What can happen at power-up, and what fixes it?

The gate can drift high and turn the motor on until the firmware configures the pin. A pull-down resistor from gate to ground keeps it off whenever the pin is not driven.

Sources (4)
  1. Raspberry Pi Ltd, RP2040 Datasheet (build 2025-02-20), Table 625 and §5.5.3.5 “Interpreting GPIO output voltage specifications”, pp. 616–617 — drive-strength settings of 2, 4, 8 or 12 mA that are “not hard limits”; VOH ≥ 2.62 V and VOL ≤ 0.5 V at the selected current; total sourced and total sunk pad current each ≤ 50 mA (as cited in unit 1, lesson 5)
  2. Microchip, ATmega328P Data Sheet DS40002061B (Rev. B, 08/2020), §29.1 Absolute Maximum Ratings p. 308 and Table 30-1 pp. 322–323 — DC current per I/O pin 40 mA (absolute maximum); summed port currents capped at 100–150 mA (as cited in unit 1, lesson 5)
  3. NXP (now Nexperia), 1N4148; 1N4448 High-speed diodes datasheet (2004 Aug 10) — VRRM 100 V; IF 200 mA continuous; IFRM 450 mA; VF ≤ 1 V at 10 mA (as cited in unit 1, lesson 5)
  4. Raspberry Pi Ltd, pico-sdk 1.5.1, hardware_regs/pads_bank0.h — GPIO pads reset with the pull-down enabled (PDE_RESET 1) and the output not driven by software until a function is selected (IO_BANK0 FUNCSEL reset 0x1F)