The most reliable and universally applicable relay circuit for switching a 5V DC coil from a 3.3V or 5V microcontroller is a low-side NPN BJT switch with a reverse-biased flyback diode. For a standard 70mA 5V relay driven by an ESP32 or Arduino, you need a 2N2222A NPN transistor, a 330Ω base resistor, and a 1N4148 flyback diode. This topology guarantees hard saturation, protects your microcontroller from inductive voltage spikes, and keeps the logic referencing simple.
The Standard Low-Side Relay Circuit Topology
When designing a relay driver, you have two primary choices: high-side switching (using a PNP transistor or P-channel MOSFET) or low-side switching (using an NPN transistor or N-channel MOSFET). The low-side NPN topology wins for 95% of microcontroller applications.
A high-side PNP switch requires you to pull the base voltage below the emitter voltage to turn it on. If your relay VCC is 5V and your MCU GPIO outputs 3.3V, the GPIO can never pull the base low enough to fully turn off the PNP, leading to a partially conducting, overheating transistor. A low-side NPN switch is referenced to ground. A 3.3V logic HIGH easily forward-biases the base-emitter junction, and a 0V logic LOW reliably cuts it off.
Topology Node Labels
- VCC_5V: 5V power rail connected to Relay Coil Pin 1 (Node A).
- Node B (Coil-): Relay Coil Pin 2, connected to the transistor Collector and the diode Cathode (stripe end).
- Node C (Collector): NPN Transistor Collector pin.
- Node D (Base): NPN Transistor Base pin, connected to the base resistor.
- Node E (Emitter): NPN Transistor Emitter pin, tied directly to GND.
- GPIO_3V3: Microcontroller output pin, connected to the other side of the base resistor.
- GND: Common ground shared by the 5V power supply, the MCU, and the transistor Emitter.
Design Walkthrough: Sizing Real Component Values
Let's design this circuit using real-world bench components. We will drive a bare Songle SRD-05VDC-SL-C relay using an ESP32 DevKit v1 (3.3V logic).
1. Calculate the Coil Current
The SRD-05VDC-SL-C has a coil resistance of approximately 70Ω. Using Ohm's Law:
I_coil = V / R = 5V / 70Ω = 71.4mA
This is the collector current (Ic) our transistor must handle when saturated.
2. Select the Transistor and Forced Beta
We'll use a standard 2N2222A NPN BJT. While its datasheet lists a maximum hFE (DC current gain) of up to 300, we must design for hard saturation to minimize the voltage drop across the transistor (VCE_sat). We use a "forced beta" of 10 to guarantee saturation.
I_base (required) = Ic / Forced_Beta = 71.4mA / 10 = 7.14mA
3. Size the Base Resistor
The ESP32 GPIO outputs 3.3V. The base-emitter junction of a silicon BJT drops about 0.7V when forward-biased (up to 0.8V in deep saturation).
R_base = (V_GPIO - V_BE) / I_base = (3.3V - 0.7V) / 7.14mA = 2.6V / 0.00714A = 364Ω
The closest standard E12 resistor value is 330Ω. Using 330Ω yields a base current of ~7.8mA, which safely drives the transistor into saturation and is well within the ESP32's recommended continuous GPIO current limit of 20mA.
4. Select the Flyback Diode
When the transistor turns off, the relay coil's collapsing magnetic field generates a massive reverse voltage spike (inductive kickback) that will instantly destroy your ESP32. A flyback diode provides a safe recirculation path for this current. While the 1N4007 is a common hobbyist default, its slow reverse recovery time makes the 1N4148 small-signal switching diode a superior choice here. It handles the 71mA spike easily and clamps the voltage much faster.
Decision Tree: Which Driver Element to Choose
While the discrete BJT is the default, your specific project constraints might demand a different switching element. Use this decision matrix to finalize your component pick.
| Driver Topology | Best Used When... | Pros | Cons | Concrete Part Pick |
|---|---|---|---|---|
| Discrete NPN BJT | Driving 1 or 2 bare relays from 3.3V/5V logic. | Cheap ($0.05), fast switching, simple math. | Requires 3 components per relay; draws base current from GPIO. | 2N2222A or BC337-40 |
| Logic-Level N-MOSFET | Driving high-current relays (12V/24V coils >200mA) or PWM switching. | Zero steady-state gate current; very low RDS(on) heat. | Gate capacitance requires care; higher component cost. | 2N7000 or IRLZ44N |
| Darlington Array IC | Driving 3 to 8 relays simultaneously from a single MCU. | Compact DIP/SOIC package; built-in clamp diodes. | High VCE_sat (~1.0V) wastes power and reduces coil voltage. | ULN2003A or ULN2803A |
| Optocoupler + BJT | Mains-voltage loads where galvanic isolation is legally or practically required. | Complete electrical isolation between MCU and relay coil. | Complex; requires two separate power domains and CTR calculations. | PC817 + 2N2222A |
Behavior & Failure Mode Analysis
Understanding what happens when a component fails is critical for debugging and safety. Here is the behavior table detailing circuit states at the extremes.
| Component | Failure Mode | Circuit Behavior & Consequence |
|---|---|---|
| Base Resistor (330Ω) | Opens | Base current drops to 0. Transistor remains in cutoff. Relay never engages. MCU GPIO is safe. |
| Base Resistor (330Ω) | Shorts | GPIO connects directly to Base. Base current spikes to (3.3V-0.7V)/~0Ω. MCU GPIO pin burns out instantly. |
| Flyback Diode (1N4148) | Opens | Inductive kickback has no path. Voltage at Node C spikes to 50V+. Transistor breaks down (VCEO exceeded) and MCU may latch up or fry. |
| Flyback Diode (1N4148) | Shorts (or installed backwards) | 5V VCC shorts directly to GND through the transistor when it turns on. Transistor vaporizes, 5V power supply trips or catches fire. |
| Transistor (2N2222A) | Collector-Emitter Short | Relay coil is permanently energized. Relay contacts remain closed regardless of MCU logic state. |
| Relay Coil | Opens | No current flows. Transistor switches normally, but relay never clicks. Flyback diode does nothing. |
For a deeper look at how inductive spikes destroy semiconductors, refer to the SparkFun guide on relay coil surges and standard semiconductor protection literature.
Step-by-Step Breadboard Testing Procedure
Never connect a newly wired relay circuit directly to your microcontroller. A single reversed diode will short your power supply and potentially back-feed 5V into your 3.3V ESP32, bricking it. Follow this verification sequence.
- Wire the Power and Ground: Connect the 5V power supply to the breadboard rails. Connect the common ground. Do not connect the MCU yet.
- Place the Transistor and Resistor: Insert the 2N2222A (flat side facing you: Emitter=Left, Base=Middle, Collector=Right). Connect the Emitter to GND. Connect the 330Ω resistor to the Base pin. Leave the other end of the resistor floating for now.
- Install the Flyback Diode: Place the 1N4148 across the relay coil pins. Critical: The black stripe (cathode) must point toward the 5V VCC rail. The unmarked anode must point toward the transistor Collector.
- Wire the Relay Coil: Connect Coil Pin 1 to 5V VCC. Connect Coil Pin 2 to the transistor Collector and the diode Anode.
- The 'Manual Base' Test: Power on the 5V supply. Take a jumper wire and briefly touch one end to the 5V rail and the other end to the floating leg of the 330Ω base resistor.
- Expected Result: You should hear a distinct 'click' from the relay. The transistor should remain cool to the touch.
- If it gets hot instantly: Power off immediately. Your diode is likely installed backward, creating a dead short.
- Verify the Clamp: If you have an oscilloscope, probe the Collector pin while toggling the manual base connection. You should see the voltage spike cleanly clamped to about 5.7V (5V VCC + 0.7V diode drop). If it spikes to 30V+, your diode is dead or disconnected.
- Integrate the MCU: Power down. Connect the ESP32 GND to the breadboard GND. Connect the ESP32 GPIO pin to the floating leg of the 330Ω resistor. Power up and run a simple 1Hz blink sketch.
By treating the relay circuit as a predictable system of nodes and failure modes rather than a black-box module, you ensure your hardware survives the inevitable inductive kickback and logic-level mismatches inherent in bench prototyping.






