Switching Things: Driving LEDs, Motors, and Relays
A GPIO pin can source a few milliamps and a great deal of optimism.
🎯 By the end you'll understand
What a microcontroller pin can genuinely drive, why the "40 mA" in the absolute-maximum table is not a design target, and why sinking and sourcing are not always symmetric. How to size an LED resistor, why the same calculation that works perfectly for a red LED on 5 V falls apart for a green one on 3.3 V, and what PWM dimming really controls. When to reach for an N-channel low-side switch, a P-channel high-side switch, or a bipolar transistor, and how to size the gate or base drive so the device is actually saturated. Why an inductive load needs a return path for its current at turn-off, and what the choice of flyback element costs in release time. And the specific reason Beacon's addressable LED sits behind a high-side switch: one milliamp of idle current that turns a three-year battery into a forty-day one.
KEY INSIGHTA pin is a signal source that happens to deliver a little power
The question is never "can this pin drive that load?" It is "what does the load demand, at what voltage, with what transient, for how long, and what is that pin's job when it is not driving?" Most driver bugs are a load whose demands were never written down being connected to a pin whose limits were never read.
A working prototype: an ESP32-C3 dev board with a green LED wired straight from a GPIO to ground, no resistor. It lights. The engineer knows this is wrong in principle, measures 6 mA, decides it is fine, and ships twenty-five boards. Four of them have visibly dimmer LEDs. Two are so dim they look broken. Nothing is broken: the LEDs came from a different bin with a forward voltage 0.15 V higher, and because the entire current was being set by the 0.2 V of headroom between a 3.3 V rail and a 3.1 V forward voltage, a 5 % change in one number produced a 7× change in the other. The circuit was never a current source. It was a subtraction of two nearly equal numbers, divided by 30 Ω, which is a definition of an unstable design.
Three questions size every driver. How much current does the load need, at its worst? — including inrush, stall, and part-to-part spread, not the typical figure. What voltage does the load need, and is it the pin's rail? — a 5 V relay does not care that your logic is 3.3 V. What happens at turn-off? — capacitive loads surge on, inductive loads fight off, and both need somewhere for their energy to go. Answer those three and the component choice is nearly automatic.
CORE CONCEPTWhat a GPIO can actually drive
A CMOS output pin is a pair of transistors: a P-channel from the rail that sources current for a logic high, and an N-channel to ground that sinks it for a low. Neither is a switch with zero resistance; each has an on-resistance of a few tens of ohms, which means the output voltage sags as you draw more current. That sag is the whole story.
The absolute maximum is a survival number, not a design number
The ESP32-C3's datasheet lists an absolute-maximum output current per pin of 40 mA. "Absolute maximum" means the value beyond which damage may occur — operating there continuously is not a design, it is a dare. Espressif's drive-strength API (gpio_set_drive_capability()) selects nominal levels of roughly 5, 10, 20 and 40 mA, with the middle setting as the default. Treat the top of that range as a short-burst capability and design your steady loads well below it.
There is a per-chip limit as well as a per-pin one
Eight pins each at their per-pin maximum is not eight times a legal design; the current all returns through the same bond wires and the same die metal. Datasheets give a total, and exceeding it heats the die and shifts every other pin's thresholds. If you are driving several loads, add them up.
Sinking and sourcing may not be symmetric
On many microcontroller families the N-channel pull-down is stronger than the P-channel pull-up, which is why the classic LED wiring puts the anode on the rail and the cathode on the pin — the pin sinks. On the ESP32 family they are close to symmetric, but "close to" is not "identical", and the datasheet lists IOL and IOH separately for a reason. Read both rather than assuming.
Every milliamp out of a pin comes from the rail, through the decoupling capacitor
A 20 mA LED turning on is a 20 mA load step at the module's supply pin, with an edge as fast as the pin can switch. Lesson 2.5 priced that: it is exactly the event that makes an RF module reset when its decoupling is 15 mm away. Driver design and power integrity are the same problem seen from two sides.
If a load draws more than about 10 mA, or needs a voltage the MCU does not have, or is inductive, or must stay on while the MCU sleeps, it gets a transistor. The transistor costs a few hundred rupiah and one 0402's worth of board area, and it converts a power problem into a signal problem — which is the only kind a GPIO is good at.
EXAMPLELEDs: forward voltage, bins, and why 3.3 V is awkward
An LED is a diode with a steep exponential I–V curve, which is a polite way of saying it is a voltage-controlled disaster. A small change in the voltage across it produces a large change in current, so you never drive one from a voltage — you drive it through a resistor from a voltage, and the resistor is what actually sets the current:
R = (Vsupply − Vf) / Itarget, and the resistor dissipates I²R. The sensitivity that matters is how much the current moves when Vf moves — because Vf varies by chemistry, by bin, by temperature (roughly −2 mV/°C) and by current itself:
ΔI / I = −ΔVf / (Vsupply − Vf). The whole design rests on that denominator. Plenty of headroom means a stable current; little headroom means the LED's own tolerance sets your brightness.
| LED | Typical Vf at 5 mA | On 5 V: R and current spread for ±0.15 V of Vf | On 3.3 V: R and current spread |
|---|---|---|---|
| Red (AlInGaP) | ≈ 2.0 V | R = 600 Ω; 4.75–5.25 mA — about ±5 % | R = 260 Ω; 4.4–5.6 mA — about ±12 % |
| Amber / yellow | ≈ 2.1 V | R = 580 Ω; ±5 % | R = 240 Ω; ±13 % |
| Green (InGaN) | ≈ 3.1 V | R = 380 Ω; 4.6–5.4 mA — about ±8 % | R = 40 Ω; 1.25–8.75 mA — about ±75 % |
| Blue / white (InGaN) | ≈ 3.2 V | R = 360 Ω; ±8 % | R = 20 Ω; the LED may not light at all on a low-Vf rail |
Read the last column. Red on 3.3 V has 1.3 V of headroom and behaves; green on 3.3 V has 0.2 V of headroom and does not. This is not a subtlety, it is a design rule: a green, blue or white LED cannot be current-set by a series resistor from a 3.3 V rail. The three real options are to run it from 5 V, to accept the variation and design the product so nobody notices (a status indicator, not a light source), or to use a constant-current LED driver, which is what every product with matched-looking LEDs actually does. A fourth option — the one in the opening story — is to use no resistor and let the pin's output impedance do it, which combines the worst variation with an undefined and datasheet-violating current.
Pulse-width modulation switches the LED fully on and fully off, so the instantaneous current is always the resistor's design value and only the average changes. That is why PWM keeps the colour constant while analog dimming shifts it — an LED's spectrum moves with drive current. Two practical notes. First, the eye's response is roughly a power law, so a linear duty ramp looks wrong: apply a gamma of about 2.2 (duty ∝ level2.2) if you want a smooth-looking fade. Second, on the ESP32-C3 the LEDC peripheral divides an 80 MHz clock, so resolution and frequency trade directly: usable duty bits ≈ log₂(80 MHz / fPWM), giving about 13 bits at 5 kHz and 10 bits at 78 kHz. Above roughly 200 Hz nobody sees flicker directly, but a camera will, which matters more often than you expect.
HOW IT WORKSMOSFETs and transistors as switches
Three topologies cover nearly everything an embedded board needs to switch.
| N-channel low-side | P-channel high-side | NPN bipolar low-side | |
|---|---|---|---|
| Where it sits | Between the load and ground | Between the supply and the load | Between the load and ground |
| Drive | Gate straight from the GPIO, if the part is logic-level | Gate must be pulled to the load's supply to turn off — needs a level shift if that supply is above the GPIO's rail | Base through a resistor sized for the current |
| Gate/base current | Essentially zero once switched; only the gate charge on each edge | Same, plus the standing current in the gate pull-up while it is on | Continuous: IB ≈ IC/10 for saturation, so 100 mA of load costs 10 mA from the pin |
| Voltage drop | I × RDS(on) — millivolts | I × RDS(on), usually 2–3× worse than an equivalent N-channel | VCE(sat) ≈ 0.1–0.3 V regardless of current |
| Catch | The load's ground is switched, so the load "floats" when off — bad for anything that shares a ground reference | The one that gates a supply rail off, which is why it is the sleep-power tool | Simple, cheap, tolerant, and it costs pin current forever |
The classic trap is a 2N7002 or an IRF540 driven from 3.3 V. Both have a gate threshold voltage VGS(th) specified as a maximum of around 2.5–4 V — and VGS(th) is the voltage at which the device conducts a few hundred microamps, not the voltage at which it is on. Worse, their RDS(on) is specified at VGS = 10 V. At 3.3 V they are partially on: they conduct, the circuit appears to work, and the FET dissipates watts and dies slowly. The correct check is to find RDS(on) specified at VGS = 2.5 V or 3.0 V in the datasheet's table. If that row does not exist, the part is not logic-level, whatever the marketing says. Parts that do have it — an AO3400 or a DMN2075U class of device, around 30 mΩ at VGS = 4.5 V and specified down to 2.5 V — cost the same and simply work.
Sizing a bipolar's base resistor is the one calculation people skip. To saturate a transistor you drive it far harder than its gain requires: assume a forced beta of 10, so IB = IC/10. For a 100 mA load from a 3.3 V GPIO, with VBE ≈ 0.7 V: IB = 10 mA, so RB = (3.3 − 0.7)/0.010 = 260 Ω, and the pin now supplies 10 mA continuously while the load is on. That is a quarter of the pin's absolute maximum spent on control rather than work, which is the honest reason MOSFETs displaced bipolars for switching: a FET's gate is a capacitor, so it costs current only during transitions.
Gate resistors and pull-ups are not optional decoration. A series gate resistor of 10–100 Ω limits the peak current the pin delivers while charging the gate capacitance — a 10 nF gate charged in 20 ns is a 1.5 A pulse without one — and damps ringing between the gate capacitance and the trace inductance. A pull-up (for a P-channel high-side) or pull-down (for an N-channel low-side) holds the gate in the off state during the seconds between power-on and the moment your firmware configures the pin, and holds it there again when the pin is put into a high-impedance state for sleep. Without it, the gate floats to wherever leakage takes it, the switch turns partially on, and you have built a heater. The next section's widget puts a number on exactly that mistake.
DEFINITIONInductive loads: the current that will not stop
A relay coil or a motor winding stores energy in a magnetic field: E = ½LI². An inductor's defining property is that its current cannot change instantly, so when the switch opens and tries to force the current to zero in nanoseconds, the inductor generates whatever voltage it takes to keep that current flowing — V = L·di/dt, with di/dt set by how fast you tried to stop it. In practice the voltage rises until something breaks down and conducts: the transistor's avalanche, the relay's contacts, or a piece of your circuit that you would rather keep.
Take a coil of 100 mH and 50 Ω, drawing 100 mA at 5 V, storing E = ½ × 0.1 H × (0.1 A)² = 500 µJ. That energy has to go somewhere at every turn-off, and it will go there several times a second if the relay is being modulated.
No flyback path: the voltage climbs until the switching FET avalanches — say at 60 V — and the FET absorbs the full 500 µJ each cycle. Some FETs are rated for repetitive avalanche and most are not; either way it is heat in a part that was not chosen for it, and 60 V transients are radiating into everything nearby.
Schottky diode across the coil: the current freewheels through the diode, so the FET sees only Vsupply + 0.4 V ≈ 5.4 V. Safe, cheap, and slow: the current decays with τ = L/R = 0.1 H / 50 Ω = 2 ms, which means the relay's armature releases lazily and its contacts arc for longer, shortening contact life.
Zener plus diode (say 24 V): the current is forced to decay against 24 V instead of 0.4 V, so the release takes t ≈ L·I/V = 0.1 × 0.1 / 24 = 0.42 ms — five times faster, at the price of exposing the FET to 25 V instead of 5.4 V. This is the standard trade in relay drive: release speed against voltage stress, and it is a real engineering choice rather than a rule.
A DC motor is the same problem plus three more: its stall current is several times its running current and occurs every time it starts or jams, its brushes generate broadband electrical noise that couples into everything (which is why motor terminals get small ceramic capacitors), and its back-EMF means the supply current is not a simple function of duty cycle. None of that changes the flyback requirement; it just makes the transistor bigger.
SEE ITThe driver designer: what the load costs, all the way to battery life
Pick a load, a rail, a drive method, how the load's supply behaves in sleep, and a flyback element. The left plot shows how the load current responds to the parameter that actually varies in production — an LED's forward-voltage bin, an addressable LED's brightness, a coil's supply. The right plot converts everything into the only number Beacon's requirements care about: battery life against R1's six-month minimum, with the canon's three-year theoretical figure marked for reference.
The sequence that teaches the most: green LED, 3.3 V, series resistor. The maths is textbook and the result is a 75 % production spread — then switch the rail to 5 V and watch the same circuit become well-behaved, because the headroom in the denominator went from 0.2 V to 1.9 V. Then the addressable LED, always powered: the load current looks trivial and the battery life collapses to forty days, because the part draws a milliamp doing nothing. Then gate it with the pull-up and three years comes back. Then remove the pull-up: life lands at 175 days against R1's 182 — a failure by about a week that no bench test lasting less than half a year would ever reveal, caused by a missing 100 kΩ resistor.
BEACONBeacon's LED, and the milliamp that eats a battery
Beacon carries one WS2812B-2020: a 2 × 2 mm addressable RGB LED with a controller die inside it, driven by a single data line. It is a genuinely good choice for a device that needs to say several different things with one part — charging, low battery, pairing, self-test failed — using colour instead of blink patterns. It also comes with two traps, and canon fixes both on the schematic.
The timing is tight, and it is not a job for a loop
The protocol is single-wire and self-clocked: a bit is a 1.25 µs cell, with a high time of roughly 0.4 µs for a zero and 0.8 µs for a one, tolerances measured in ±150 ns, and a reset gap of low longer than 50 µs (newer variants of the part want considerably more — read the datasheet for the exact revision you bought). Twenty-four bits per LED at 800 kbit/s is 30 µs of transmission. Bit-banging that on top of FreeRTOS is a lottery: any interrupt longer than 150 ns during the frame corrupts a colour. On the ESP32-C3 the right peripheral is RMT, which clocks the pattern out of hardware — a Lesson 3.3 topic, made necessary by a Stage 2 component choice.
The logic threshold scales with its supply
The part expects a data high of roughly 0.7 × VDD. Run it from 5 V and it wants 3.5 V, which a 3.3 V GPIO cannot deliver — a marginality that works on the bench and fails on a cold morning, and the reason so many forum posts recommend a level shifter or a sacrificial diode in the 5 V rail. Beacon sidesteps it entirely by running the LED from the same 3.3 V rail as everything else, where 0.7 × 3.3 = 2.31 V and a 3.3 V output has healthy margin. This is a case where the cheap fix is a rail choice, not a component.
The controller die never sleeps
This is the one that matters. A WS2812B draws roughly a milliamp with all three channels commanded off, because its internal oscillator and logic keep running to watch the data line. Against Beacon's canon sleep floor of 8 µA, that is 125× the entire budget.
So it sits behind a high-side P-MOSFET, with a gate pull-up
The FET's source connects to the 3.3 V rail, its drain feeds the LED, and its gate is pulled up to 3.3 V through 100 kΩ so the switch is off by default — during the seconds between power-on and firmware configuration, during any reset, and throughout deep sleep. Because the source sits on the 3.3 V rail, a 3.3 V output can drive the gate directly with no level shift: GPIO10 pulls it to ground to turn the LED on, costing 3.3 V / 100 kΩ = 33 µA for as long as it is on, which averaged over a wake of tens of milliseconds once a minute is negligible.
Canon's budget: 8 µA in deep sleep, plus a 45 ms active window at 40 mA once every 60 s, giving 0.045 × 40 mA / 60 = 30 µA of duty-cycled active current and 38 µA average — about 26,300 hours, or three years on a 1000 mAh cell, comfortably inside R1's six-month requirement even with heavy derating.
Leave the LED on the always-on rail and the average becomes 8 + 1000 + 30 = 1038 µA. Life: 1,000,000 µAh / 1038 µA ≈ 963 h ≈ 40 days. R1 fails by a factor of five, from a part that is switched off.
Fit the P-MOSFET but forget the gate pull-up, and in deep sleep the GPIO goes high-impedance and the gate floats. A floating gate on a P-channel device drifts to somewhere between the rail and ground, partially enhancing the channel; a couple of hundred microamps is an entirely plausible result. Average: 8 + 200 + 30 = 238 µA. Life: about 4200 h ≈ 175 days ≈ 5.8 months. R1 demands 182 days, so the design misses it by about a week — and a 72-hour bench soak, which is exactly what the Stage 4 milestone calls for, would show a current draw of a quarter of a milliamp that looks like nothing much and passes.
And even when it is gated, the LED is not free. One 20 ms flash at 16 mA per report is 16 × 0.020 / 60 = 5.3 µA averaged — 14 % of the entire 38 µA budget for a single blink a minute. Beacon's firmware therefore uses the LED for bring-up, for button feedback, and for fault indication, and never as a heartbeat. That is a firmware rule created by a Stage 2 arithmetic result, and it goes on the schematic as a note beside the part.
Schematic v0 gains: the WS2812B-2020 on the 3.3 V rail behind a logic-level P-channel MOSFET, its gate on GPIO10 and its data line on GPIO5 (which is RMT-capable); 100 kΩ from that gate to 3.3 V so the default state is off; a series resistor footprint (100–330 Ω) on the data line to damp reflections and limit fault current, populated as 0 Ω if the trace turns out to be short enough; and 100 nF of decoupling on the switched LED rail — which, being downstream of the switch, is discharged in sleep and produces a small inrush at every turn-on, handled by the FET's own gate ramp. The design note on the sheet reads: "LED rail gated; gate pull-up mandatory; sleep current with LED rail off must measure < 10 µA at bring-up." That last sentence is a test, written at schematic time, that Lesson 6.1 will actually run.
WEAKNESSWhere this bites
Every one of the failures above is invisible unless you measure sleep current properly, and the usual measurement is wrong in two ways at once. A USB cable supplies the board, so the battery path — and everything downstream of the reverse-polarity FET and the gating switch — is not in the measurement at all. And an attached debugger or an open serial monitor keeps peripherals awake that would otherwise be off. The number to trust is a current measurement in series with the battery, on a board with nothing else connected, with the firmware in its real sleep state, averaged over at least one full report cycle so the wake burst is included. That instrument is introduced in Lesson 1.7 and used in anger in Lesson 4.3; the point here is that a driver's sleep cost is a measurement, not a calculation, and the calculation exists to tell you what the measurement should say.
A FET that is partially enhanced conducts, so the LED lights, the motor spins, and the circuit passes a functional test. What it does not do is saturate, so instead of a few tens of milliohms it presents an ohm or more, and the power it dissipates is I²R with a resistance nobody measured. At 300 mA that is 90 mW in an SOT-23 — survivable; at 1 A it is a warm part with a shortened life and a thermal coefficient that makes RDS(on) worse as it heats, which is a self-reinforcing loop. Always find the RDS(on) row specified at or below your actual gate drive. If the datasheet only characterises the part at VGS = 10 V, it is telling you something.
Gating a peripheral's supply rail while leaving its data or control lines driven high from a still-powered GPIO is a classic and subtle leak: current flows out of your pin, through the unpowered device's input protection diode, into its dead supply net, and away. The device is now half-powered through its own I/O pin — which is both a phantom current draw and a reliability hazard, since parts are not specified for being energised through their protection structures. Whenever you switch a rail off, every signal into that device must go low or high-impedance in the same operation. Lesson 2.1 made this point about I2C pull-ups; it is the same failure with a different part, and on Beacon it is the reason the LED's data line is driven low before the gate is released.
KEY INSIGHTHow this connects
- Diodes, Transistors, and MOSFETs as Switches (1.5) — RDS(on) versus VGS, the body diode, and saturation were introduced there as device physics. Here they became the reason a part that "works" at 3.3 V may be dissipating twenty times what you budgeted.
- Logic Levels, Pull-ups, and Open-Drain (2.1) — a floating gate is a floating input with power behind it. The rule that every pin must be driven, pulled, or configured is the same rule that makes the LED gate's 100 kΩ pull-up mandatory rather than optional.
- Timing, Clocks, and Crystals (2.8) — next: the last of Beacon's front-end decisions. The addressable LED needed 150 ns of timing accuracy from a peripheral; the sleep timer needs parts-per-million accuracy over thirty days, and the two problems have completely different answers.
Forward, the RMT peripheral that drives the WS2812B is set up in Lesson 3.3, and the rule that the LED must not blink on every report becomes a state-machine constraint in 3.7. The 5.3 µA-per-blink figure feeds directly into the measured power budget of 4.3, where every microamp gets a line. And in Stage 6 the "sleep current with the LED rail off must measure under 10 µA" note becomes an actual step in the bring-up procedure — the moment when this lesson's arithmetic either survives contact with a real board or does not.
GLOSSARYKey terms introduced
- Sourcing and sinking
- A pin sources current when it supplies it from the rail through its P-channel transistor, and sinks it when it accepts current to ground through its N-channel one. The two capabilities (IOH and IOL) are listed separately in a datasheet and are not always equal.
- Forward voltage, Vf
- The voltage across a conducting LED, set by its chemistry: about 2.0 V for red, 3.1–3.2 V for green, blue and white. It varies by bin and falls roughly 2 mV per °C, which is why the supply headroom (Vsupply − Vf) determines how stable the current is.
- Low-side / high-side switch
- A switch between the load and ground (low-side, usually N-channel, gate driven directly) or between the supply and the load (high-side, usually P-channel, gate referenced to the load's supply). Only a high-side switch can remove a rail from a sleeping device.
- Logic-level MOSFET
- A device whose RDS(on) is specified at a gate drive your microcontroller can actually produce. Verified by finding an RDS(on) figure at VGS = 2.5 or 3.0 V in the datasheet — not by the threshold voltage, and not by the marketing name.
- Forced beta
- The ratio IC/IB you deliberately choose when saturating a bipolar transistor, typically 10 — far below the device's actual gain, to guarantee saturation across temperature and part spread.
- Flyback (freewheel) path
- The route provided for an inductor's current at turn-off, usually a diode across the coil. Without one, the inductor generates whatever voltage is needed to keep its current flowing, and the switching device absorbs ½LI² by avalanching.
- Quiescent current
- What a part draws while doing nothing. For an addressable LED it is around a milliamp — the number that decides whether it may share an always-on rail with a battery-powered device.
- Gate pull-up / pull-down
- The resistor that holds a switch in its off state whenever the controlling pin is not actively driving it: at power-on, during reset, and in deep sleep. Its absence is a leak that appears only in the state hardest to test.
ACTIVE RECALLCheck your understanding
Answer in your own words first, then reveal.
1Why does a 5 mA green LED behave predictably on a 5 V rail and unpredictably on 3.3 V, when both use a correctly calculated resistor?›
Because the sensitivity of the current to forward-voltage variation is ΔI/I = −ΔVf/(Vsupply − Vf), and the denominator is the supply headroom. A green LED's Vf is around 3.1 V. On 5 V the headroom is 1.9 V, so a ±0.15 V bin spread moves the current by about ±8 %: invisible. On 3.3 V the headroom is 0.2 V, so the same ±0.15 V moves the current from roughly 1.3 mA to 9.0 mA — a factor of seven, plus a further shift with temperature at −2 mV/°C. The resistor calculation was correct in both cases; the circuit topology stopped being a current source in the second one.
Three real options on 3.3 V: run the LED from a higher rail, accept the variation because it is a status indicator and not a light source, or use a constant-current driver. A fourth non-option is to drop the resistor and let the pin's output impedance set the current, which combines the worst variation with an undefined current and a datasheet violation.
2Beacon's WS2812B is fitted behind a high-side P-MOSFET but the 100 kΩ gate pull-up was left off the BOM. Compute the consequence and explain why testing would not find it.›
In deep sleep the GPIO goes high-impedance, so the gate floats and drifts to a voltage that partially enhances the channel — a couple of hundred microamps of leakage through the LED's controller is a realistic outcome. Beacon's average becomes 8 µA sleep + 200 µA leak + 30 µA duty-cycled active = 238 µA, giving 1,000,000 µAh / 238 µA ≈ 4200 h ≈ 175 days, about 5.8 months. R1 demands six months, or 182 days. The design misses it by about a week.
Testing misses it because the failure is a small number in a state that is expensive to observe. A functional test passes: the LED works. A current measurement with USB attached misses it: the battery path is not in circuit. Even the canon's 72-hour bench soak passes, because 238 µA over three days consumes 17 mAh out of 1000 — under 2 %, well inside the noise of a bench measurement. The failure is only visible either by measuring sleep current directly in series with the battery, or by waiting five months. That is precisely why the number is calculated at schematic time and written on the sheet as a bring-up test with a threshold.
3A 5 V relay coil (100 mH, 100 mA) is switched by an N-MOSFET from a 3.3 V GPIO. What must be checked about the FET, and what happens at turn-off with no flyback diode, a Schottky, and a 24 V Zener?›
The FET check: find RDS(on) specified at VGS = 2.5 or 3.0 V. The threshold voltage VGS(th) is not the answer — it is the gate voltage at which a few hundred microamps flow. A part characterised only at VGS = 10 V will conduct at 3.3 V, appear to work, and dissipate far more than budgeted. Also add a gate pull-down so the relay is off before firmware runs, and a gate series resistor of 10–100 Ω to limit the pin's peak charging current.
At turn-off the coil holds E = ½LI² = ½ × 0.1 × 0.01 = 500 µJ. With no diode the voltage rises until the FET avalanches — perhaps 60 V — and the FET absorbs all 500 µJ every cycle, while a 60 V transient radiates into the board. With a Schottky the current freewheels and the FET sees only about 5.4 V, but the current decays with τ = L/R = 2 ms, so the armature releases slowly and the contacts arc longer. With a 24 V Zener plus a series diode the current is forced to decay against 24 V and release takes about 0.42 ms — five times faster — at the price of about 30 V across the FET — the supply plus the clamp, not the clamp alone. That is the actual engineering trade: relay contact life against transistor voltage rating, decided by how often the relay switches and what it switches.
4A colleague gates a sensor's 3.3 V supply off in sleep with a high-side switch and measures a sleep current 400 µA higher than expected. The gate pull-up is fitted and correct. What is the most likely cause?›
Signal lines into the unpowered device are still being driven. Current flows out of a still-powered GPIO (or through an I2C pull-up on the always-on rail), forward through the sleeping device's input ESD protection diode, into its now-dead VDD net, and out through whatever else that net connects to. The device is being back-powered through its own I/O pins. Symptoms fit: hundreds of microamps, present only in sleep, and the sensor may even partly function, which makes it confusing.
The fix is to make rail gating an atomic operation that covers signals as well as power: before releasing the switch, drive every line into that device low or set it to high-impedance, and make sure no pull-up on the always-on rail terminates on a line that reaches it. This is exactly why Beacon does not gate its sensor rail — the SHT40 and LIS2DH12 both have proper sleep modes counted inside the canon's 8 µA, so switching their supply would create this hazard to solve a problem that does not exist. The LED is gated because its controller has no sleep mode at all, and its single data line is driven low as part of the same sequence.