To safely drive a relay in electronics circuit designs from a 3.3V or 5V microcontroller, use a low-side N-channel logic-level MOSFET topology paired with a flyback diode. This configuration isolates the high-current inductive coil from your fragile GPIO pins, handles the inductive kickback safely, and provides a fail-safe default state when the microcontroller resets.
Below is the complete design walkthrough, from node mapping and component sizing to extreme failure modes and breadboard verification.
The Standard Low-Side Topology: Node Mapping and Design Rationale
When integrating a relay in electronics circuit projects, the physical switching element (the relay coil) requires significantly more current (typically 50mA to 150mA) than a microcontroller GPIO can safely source (usually limited to 12mA–20mA continuous). You need an intermediary switch.
Node Labels and Connections
- VCC (Coil Power): 5V or 12V supply, connected to one side of the relay coil.
- Drain (MOSFET): Connected to the other side of the relay coil and the anode of the flyback diode.
- Source (MOSFET): Connected to the circuit GND and the cathode (stripe) of the flyback diode.
- Gate (MOSFET): Connected to the microcontroller GPIO via a series resistor, with a pull-down resistor to GND.
- COM / NO / NC: The dry contacts on the relay output side, completely isolated from the low-voltage control nodes.
Why Low-Side N-FET Over the Alternatives?
You might wonder why we don't use a high-side P-channel MOSFET or a standard NPN BJT (like the 2N2222).
- vs. High-Side P-FET: A P-FET requires the gate to be pulled below the source voltage to turn on. If your relay runs on 12V, you would need to pull the gate down to ~7V to turn it on, which a 3.3V ESP32 GPIO cannot do directly without an additional level-shifting N-FET. Low-side N-FETs are ground-referenced, meaning a 3.3V logic HIGH directly exceeds the threshold voltage ($V_{GS(th)}$) to turn the switch on.
- vs. NPN BJT: Bipolar transistors are current-controlled. To switch 70mA through the collector, a 2N2222 with an $h_{FE}$ of 100 requires ~0.7mA of base current, but to guarantee saturation, you typically drive it with 5mA to 10mA. That wastes precious GPIO current budget and generates heat in the base resistor. A MOSFET is voltage-controlled; once the gate capacitance is charged, steady-state gate current is effectively zero.
Component Sizing: Picking Real Values for a 5V Relay Driver
Let's design a concrete circuit using a standard 5V relay module or bare component. The table below details the exact component values, part numbers, and the engineering rationale for each selection in this relay in electronics circuit topology.
| Component | Part / Value | Key Specification | Design Rationale |
|---|---|---|---|
| Relay | Songle SRD-05VDC-SL-C | Coil: 71.4Ω, 70mA @ 5V | Standard 5V SPDT relay. 70mA is well within the limits of our chosen MOSFET. |
| Switch (MOSFET) | IRLZ44N | $R_{DS(on)}$ = 22mΩ @ $V_{GS}$=5V | Logic-level N-FET. 22mΩ means it dissipates only ~0.1mW at 70mA. Runs completely cold. |
| Flyback Diode | 1N4007 | 1000V PIV, 1A $I_F$, 30A $I_{FSM}$ | Clamps the inductive spike ($V = L \frac{di}{dt}$) when the MOSFET opens. 1N4148 works, but 1N4007 handles higher surge currents safely. |
| Gate Series Resistor | 100Ω (1/4W) | Limits $di/dt$ into gate capacitance | Prevents high-frequency ringing on long gate traces and limits inrush current from the GPIO pin charging the MOSFET gate. |
| Gate Pull-Down | 10kΩ (1/4W) | Bleeds gate charge to GND | Critical for ESP32/Arduino. Keeps the relay OFF during MCU boot when GPIO pins are high-impedance (floating). |
For a deeper look at the physics of inductive kickback and why the flyback diode is non-negotiable, refer to the All About Circuits guide on microcontroller relay driving. Omitting the diode will result in the MOSFET's internal parasitic BJT breaking down, permanently shorting the Drain to the Source.
Behavior Matrix and Extreme Failure Modes
Understanding how the circuit behaves under normal logic states is only half the battle. As a designer, you must know what happens when components fail or are wired incorrectly.
Normal Operation Behavior Table
| GPIO State | $V_{GS}$ (Gate-Source) | MOSFET State | Coil Current | Relay Contacts |
|---|---|---|---|---|
| LOW (0V) | 0V | OFF (High $R_{DS}$) | 0 mA | COM connected to NC |
| HIGH (3.3V/5V) | > $V_{GS(th)}$ | ON (~22mΩ) | ~70 mA | COM connected to NO |
| Transition HIGH→LOW | Drops to 0V | Turns OFF rapidly | Collapses; Diode conducts | Returns to NC (with slight delay due to diode decay time) |
What Breaks at the Extremes? (Failure Mode Contrast)
- Open Coil (Relay internally broken): The MOSFET will switch the Drain node to GND perfectly, but no current flows. You will hear no "click." The flyback diode does nothing because there is no magnetic field collapsing. Diagnostic: Measure voltage at the Drain pin. If it swings from VCC to ~0V but the relay doesn't click, the coil is open.
- Shorted Flyback Diode (or wired backwards): If the 1N4007 is installed with the stripe (cathode) facing the Drain instead of VCC, it acts as a dead short across the power supply the moment the MOSFET turns on. Result: The MOSFET will likely survive due to its low $R_{DS(on)}$, but your VCC trace will melt, or your bench power supply will trip its overcurrent protection instantly.
- Open Pull-Down Resistor: During the first 500ms of an ESP32 boot sequence, GPIO pins float. Without the 10kΩ pull-down, ambient noise or internal MCU multiplexing can charge the MOSFET gate capacitance, causing the relay to chatter wildly or latch ON unexpectedly during boot.
- Shorted MOSFET (Drain-to-Source): Usually caused by omitting the flyback diode, allowing a 50V+ inductive spike to punch through the silicon. Result: The relay is permanently energized, regardless of the GPIO state. The microcontroller is usually protected by the 100Ω gate resistor, which stops the high-voltage spike from traveling backward into the GPIO pin.
Step-by-Step Breadboard Verification Sequence
Do not just plug in power and hope for the best. Inductive loads demand a methodical verification sequence. Follow these steps to breadboard and test your relay in electronics circuit safely.
- Power Down and Isolate: Ensure both the logic supply (3.3V/5V) and the coil supply (VCC) are disconnected. If using a single 5V rail for both, unplug the USB or barrel jack.
- Seat the Active Components: Insert the IRLZ44N MOSFET into the breadboard. Ensure the pins (Gate, Drain, Source) are on independent rows. Insert the Songle relay and the 1N4007 diode.
- Wire the Low-Side Switch: Connect the MOSFET Source pin to the breadboard GND rail. Connect the Gate pin to GND via the 10kΩ pull-down resistor.
- Wire the Coil and Clamping Diode: Connect one side of the relay coil to the VCC rail. Connect the other side of the coil to the MOSFET Drain. Critical Step: Place the 1N4007 diode in parallel with the coil. The cathode (silver stripe) must connect to VCC, and the anode must connect to the Drain.
- Wire the Logic Input: Connect the 100Ω series resistor between your ESP32/Arduino GPIO pin and the MOSFET Gate.
- Pre-Flight Multimeter Checks:
- Set your multimeter to continuity mode. Probe VCC to GND. It should read open (OL). If it beeps, your diode is backwards or you have a short.
- Probe the Drain to GND. It should read open (the MOSFET is off, and the diode is reverse-biased).
- Set the meter to resistance. Probe across the relay coil pins. You should read approximately 71.4Ω.
- Logic Test (Dry Run): Power up the microcontroller and the 5V rail. Write a simple sketch to toggle the GPIO HIGH for 2 seconds, then LOW for 2 seconds. You should hear a distinct mechanical "click" from the relay on both transitions.
- Verify the Flyback Clamp: If you have an oscilloscope, place the probe on the Drain pin. When the GPIO goes LOW, you should see the voltage spike up to exactly VCC + 0.7V (the forward voltage of the 1N4007), then decay smoothly. If you see a massive high-frequency ringing spike exceeding 20V, your diode is missing or installed backwards.
- Measure Contact Resistance: With the relay energized (GPIO HIGH), use your multimeter to measure resistance between the COM and NO terminals on the output side. It should read less than 0.1Ω. If it reads higher, the internal contacts are pitted or oxidized, and the relay should be replaced before switching high-current loads.
By adhering to this low-side topology and respecting the extreme failure modes of inductive components, your relay driver will operate reliably for millions of cycles without risking your microcontroller or your power supply. For further reading on contact degradation and relay lifecycle limits, consult the SparkFun Relay Tutorial.






