Sooner or later, your ESP32 project will need to switch something bigger than an LED — a lamp, a fan, a heater, a pump. The ESP32's GPIO pins can only source about $40\,\text{mA}$ at $3.3\,\text{V}$, so you need an external switch. The two most common options are relays and MOSFETs, and they solve the problem in very different ways.
🔗Quick Recommendations
- Switching mains AC (lamps, appliances, outlets): Relay module — provides galvanic isolation between your ESP32 and dangerous voltages.
- PWM-controlling a DC load (LED strips, heaters, fans): MOSFET — switches fast enough for smooth dimming and speed control.
- Simple on/off for DC loads: Either works — relay for isolation, MOSFET for silence and speed.
🔗Comparison Table
| Feature | Relay Module | Logic-Level MOSFET |
|---|---|---|
| Switching type | Mechanical contacts | Solid-state (semiconductor) |
| AC loads | Yes — designed for this | No — DC only (for AC, use a TRIAC or SSR) |
| DC loads | Yes | Yes |
| Max voltage | $250\,\text{V AC}$ / $30\,\text{V DC}$ (typical) | Depends on MOSFET ($55\,\text{V}$ for IRLZ44N) |
| Max current | $10\,\text{A}$ (typical) | $47\,\text{A}$ (IRLZ44N rated) — but PCB traces and cooling matter |
| PWM control | No — too slow, wears contacts | Yes — switches in nanoseconds |
| Switching speed | $\sim 5\text{--}10\,\text{ms}$ | $< 1\,\mu\text{s}$ |
| Isolation | Yes — galvanic isolation (optocoupler) | No — load and logic share a common ground |
| Audible click | Yes (mechanical) | Silent |
| Power draw | $\sim 70\text{--}80\,\text{mA}$ (coil) | Negligible (gate charge only) |
| Heat generation | Minimal at low current | Minimal at low current; increases with high current ($I^2 \times R_{DS(on)}$) |
| Lifespan | $\sim 100{,}000$ cycles (mechanical wear) | Essentially unlimited (no moving parts) |
| GPIO pins needed | 1 (digital) | 1 (digital or PWM) |
| Price | Very cheap | Very cheap |
🔗Relay — Mechanical Isolation
A relay is an electrically controlled mechanical switch. When the ESP32 energizes the relay coil, an electromagnet pulls the contacts closed. When the coil is de-energized, a spring returns the contacts to their default position.
Best for:
- Mains AC switching — lights, fans, appliances, outlets. The relay provides galvanic isolation, meaning there is no electrical connection between the ESP32 circuit and the mains side.
- Safety-critical applications — the physical air gap between open contacts provides a level of safety that semiconductors cannot match.
- Simple on/off — when you just need "turn it on" and "turn it off" with no dimming or speed control.
Limitations:
- No PWM — mechanical contacts cannot switch fast enough. Using PWM on a relay would burn out the contacts quickly.
- Audible click — every switch makes a click sound. In a bedroom or quiet environment, this is noticeable.
- Slower — $5\text{--}10\,\text{ms}$ switching time (fine for human-scale events, too slow for high-frequency control).
- Coil power — the relay coil draws $\sim 70\text{--}80\,\text{mA}$, which is significant for battery-powered projects.
- Contact wear — mechanical contacts degrade over time. Rated for around $100{,}000$ cycles, which sounds like a lot but is consumed quickly if switching frequently.
Active LOW: Most relay modules are active LOW — pulling the signal pin LOW energizes the relay. This is counterintuitive but important to know. The modules usually have an optocoupler that inverts the logic.
🔗Solid-State Relays (SSR)
A solid-state relay uses a semiconductor (usually a TRIAC) instead of mechanical contacts. SSRs switch silently, handle PWM (at low frequencies like $1\,\text{Hz}$ for proportional control), and last longer. They are more expensive and can generate heat under heavy loads. Consider an SSR if you need relay-like isolation but want to avoid mechanical wear.
🔗MOSFET — Fast Semiconductor Switch
A logic-level MOSFET (like the IRLZ44N) acts as a voltage-controlled switch. Apply $3.3\,\text{V}$ to the gate and it conducts; remove the voltage and it stops. There are no moving parts, no click, and switching happens in nanoseconds.
Best for:
- PWM-controlled DC loads — dimming LED strips, controlling heater power, adjusting fan speed. The fast switching is invisible to the load.
- High-frequency switching — anything that needs rapid on/off cycling.
- Battery-powered projects — the gate draws essentially zero continuous current.
- Silent operation — no mechanical noise.
Limitations:
- DC only — a standard MOSFET cannot switch AC loads. For AC, you need a TRIAC or solid-state relay.
- No isolation — the load and the ESP32 share a common ground. If the load is noisy (motors, solenoids), it can introduce electrical noise into your logic circuits.
- Heat at high current — the MOSFET's on-resistance ($R_{DS(on)}$) dissipates power as heat: $P = I^2 \times R_{DS(on)}$. At $10\,\text{A}$ through an IRLZ44N ($0.022\,\Omega$), that is $2.2\,\text{W}$ — warm, but manageable with a small heatsink.
- Gate protection — a $10\,\text{k}\Omega$ pull-down resistor on the gate is essential to prevent the MOSFET from floating on during boot (when the ESP32's GPIO is high-impedance). A $100\,\Omega$ series resistor on the gate limits inrush current.
Logic-level MOSFETs: Not all MOSFETs work with $3.3\,\text{V}$ logic. You need a logic-level MOSFET with a gate threshold voltage ($V_{GS(th)}$) well below $3.3\,\text{V}$. The IRLZ44N ($V_{GS(th)} = 1.0\text{--}2.0\,\text{V}$) and IRL540N are safe choices. A standard IRF540N ($V_{GS(th)} = 2.0\text{--}4.0\,\text{V}$) may not fully turn on at $3.3\,\text{V}$.
🔗Flyback Diode
If your load is inductive (motors, solenoids, relay coils), you must add a flyback diode (like a 1N4007) across the load, cathode toward the positive rail. When an inductive load is switched off, it generates a voltage spike that can destroy the MOSFET. The diode absorbs this spike.
🔗Which Should You Pick?
| Your Situation | Recommended | Why |
|---|---|---|
| Switching a mains lamp or appliance | Relay | Galvanic isolation for safety |
| Dimming an LED strip (12V/24V DC) | MOSFET (PWM) | Smooth, silent dimming |
| Controlling a heater (DC) | MOSFET (PWM) | Proportional power control |
| Controlling a heater (AC) | SSR or relay | Relay for on/off, SSR for proportional |
| Fan speed control (DC) | MOSFET (PWM) | Variable speed, silent |
| Fan on/off (AC) | Relay | Simple, isolated |
| Battery-powered device | MOSFET | Near-zero quiescent current |
| Noisy motor / solenoid | Relay | Isolation protects ESP32 from noise |
| Silent switching (bedroom, quiet space) | MOSFET or SSR | No mechanical click |
| Rapid switching (>1 Hz) | MOSFET | No contact wear, instant switching |
🔗Next Steps
- Read our individual guides: Relay Module, Controlling Heating Elements (covers MOSFET wiring in detail)
- Build a smart thermostat using MOSFET-controlled heating
- Add relay control to Home Assistant for smart home switching
- Combine a MOSFET with a temperature sensor for automatic fan control