The Standard Low-Side BJT Relay Topology
When integrating a relay in circuit with a 3.3V microcontroller like the ESP32 or Raspberry Pi, you cannot drive the relay coil directly from a GPIO pin. A typical 5V relay coil draws 70mA to 90mA, which far exceeds the safe continuous current limit of a 3.3V GPIO pin (typically 20mA, with an absolute maximum of 40mA before silicon damage occurs). The most robust, cost-effective, and predictable solution is a low-side NPN Bipolar Junction Transistor (BJT) switch.
This topology places the relay coil between the positive supply and the transistor's collector. The transistor acts as a switch to ground, controlled by the microcontroller. Here is the exact node map for the control side of the circuit:
- Node 1 (VCC): 5V DC power supply positive rail.
- Node 2 (Coil+): Relay coil positive terminal, tied to VCC.
- Node 3 (Coil- / Collector): Relay coil negative terminal, tied to the BJT Collector and the Flyback Diode Cathode.
- Node 4 (Flyback Anode): Flyback diode anode, tied to the BJT Collector.
- Node 5 (Base): BJT Base, tied to the Base Resistor.
- Node 6 (GPIO): Microcontroller 3.3V output pin, tied to the other end of the Base Resistor.
- Node 7 (Emitter / GND): BJT Emitter, Flyback Diode Cathode (wait, anode to collector, cathode to VCC - correction below), and system Ground.
Circuit Behavior Matrix
| Element Changed / State | Electrical Result | Physical Outcome |
|---|---|---|
| GPIO goes HIGH (3.3V) | Base current flows, BJT saturates. V_CE drops to ~0.2V. 5V appears across the coil. | Relay energizes. Audible click. NO (Normally Open) connects to COM. |
| GPIO goes LOW (0V) | Base current stops. BJT cuts off. Coil magnetic field collapses, flyback diode clamps spike. | Relay de-energizes. Audible click. NC (Normally Closed) connects to COM. |
| GPIO left Floating | Base voltage is undefined. BJT may enter the linear (active) region due to leakage or noise. | Relay chatters or fails to pull in. BJT dissipates massive heat and may thermally fail. |
| Base Resistor increased to 10kΩ | Base current starved (0.26mA). BJT cannot saturate. V_CE rises to 2V-3V. | Coil receives insufficient voltage. Relay fails to latch. BJT overheats. |
Design Walkthrough: Sizing Components for 3.3V Logic
Let's design this circuit using real bench components: an ESP32-WROOM-32 (3.3V logic), a Songle SRD-05VDC-SL-C (5V relay, 70Ω coil), and a 2N3904 NPN BJT.
Step 1: Determine Coil Current (Ic)
The relay coil resistance is 70Ω. Using Ohm's Law: I = V / R = 5V / 70Ω = 71.4mA. This is the Collector current (Ic) the BJT must handle.
Step 2: Calculate Required Base Current (Ib)
To ensure the BJT acts as a closed switch (saturation) rather than an amplifier, we force a low Beta (hFE). While the 2N3904 datasheet lists an hFE of ~100 in the active region, saturation requires a forced Beta of 10 to 20. Let's use 15 for a safe margin.
Ib = Ic / Forced Beta = 71.4mA / 15 = 4.76mA.
Step 3: Size the Base Resistor (R_base)
The ESP32 GPIO outputs 3.3V. The Base-Emitter junction (V_BE) drops about 0.7V when conducting.
Voltage across resistor = 3.3V - 0.7V = 2.6V.
R_base = V / Ib = 2.6V / 0.00476A = 546Ω.
The closest standard E12 resistor value below this is 470Ω. Using 470Ω yields an Ib of 5.5mA, which is perfectly safe for the ESP32 (well under the 20mA continuous recommendation) and guarantees hard saturation.
Step 4: Select the Flyback Diode
When the BJT turns off, the coil's inductance generates a high-voltage reverse spike (V = L * di/dt). A standard 1N4007 rectifier diode is rated for 1A and 1000V PIV, easily handling the 71mA decay current and the transient spike. For faster switching applications, a 1N4148 signal diode works, but the 1N4007 is the ubiquitous, bulletproof choice for 5Hz to 10Hz relay switching.
Failure Modes: What Breaks at the Extremes?
Understanding failure modes separates a working prototype from a reliable product. Here is what happens when individual elements fail open or short.
- Base Resistor Shorts (0Ω): The 3.3V GPIO pin is connected directly to the BJT Base. The Base-Emitter junction acts like a forward-biased diode, clamping at 0.7V. The GPIO pin attempts to source (3.3V - 0.7V) / 0Ω = infinite current. Result: The ESP32 GPIO pin burns out instantly, potentially destroying the microcontroller's internal silicon.
- Base Resistor Opens: No base current can flow. Result: The BJT remains permanently off. The relay never energizes. The circuit fails safe.
- Flyback Diode Installed Backwards (Cathode to Collector, Anode to 5V): The diode is now forward-biased across the 5V power supply. Result: The moment power is applied, 5V shorts directly to ground through the diode. The 5V power supply will either current-limit, blow its fuse, or the diode will violently overheat and fail open.
- Flyback Diode Fails Open (or is omitted): When the BJT turns off, the inductive kickback has no recirculation path. The Collector voltage spikes to 50V or more. Result: The spike exceeds the 2N3904's 40V V_CEO rating, causing avalanche breakdown. The BJT is destroyed, and the high-voltage spike can backfeed through the Base-Collector junction into the ESP32 GPIO, frying the microcontroller.
- Relay Coil Opens (Internal wire break): No current flows. Result: The BJT saturates safely (0mA collector current), but the relay contacts never move. The load remains unpowered.
Decision Tree: Which Driver Should You Actually Use?
While the BJT topology is excellent for standard 5V relays, different constraints demand different drivers. Use this decision matrix to select your switching element.
| Application Constraint | Recommended Component | Why This Wins |
|---|---|---|
| Single 5V relay (<100mA coil), 3.3V logic | 2N3904 BJT + 470Ω Resistor | Cheap ($0.05), fully saturates at 3.3V, simple biasing. |
| High-current DC load (e.g., 12V 5A motor), 3.3V logic | IRLZ44N Logic-Level MOSFET | Handles high current with near-zero steady-state heat; Vgs(th) is low enough for 3.3V drive. |
| Switching 8+ relays simultaneously from one MCU | ULN2803 Darlington Array IC | Integrates 8 drivers and flyback diodes in one DIP package; saves massive PCB space. |
| Switching 120V/240V AC Mains at high frequency (>1Hz) | Omron G3NA-210B Solid State Relay (SSR) | Zero-crossing switching prevents EMI and contact arcing; no mechanical wear-out. |
The Concrete Pick: If you are building a standard home-automation node or bench project switching a single 5V mechanical relay from an ESP32, use the 2N3904 with a 470Ω base resistor and a 1N4007 flyback diode. It is the most forgiving, easily sourced, and electrically robust configuration for this exact use case.
Why This Topology Over a Direct MOSFET Drive?
A common mistake among hobbyists is reaching for a MOSFET (like the IRF520) because 'MOSFETs are better switches.' For 3.3V logic driving a low-current relay coil, a BJT is actually superior. Here is why:
- The Vgs(th) Trap: Standard MOSFETs like the IRF520 have a Gate-Source threshold voltage (Vgs(th)) of 2V to 4V. At 3.3V, they barely turn on, operating in the linear region with high Rds(on), leading to voltage drops and heat. You must specifically hunt for 'Logic-Level' MOSFETs (like the IRLZ44N), which are more expensive and often overkill for a 70mA coil.
- Gate Capacitance and Oscillation: MOSFET gates act like capacitors. Driving a gate directly from a GPIO pin without a series gate resistor can cause high-frequency ringing and oscillation due to trace inductance, potentially frying the GPIO pin. BJTs are current-controlled and do not suffer from this specific capacitive ringing issue in low-frequency relay applications.
- Pull-Down Requirements: MOSFET gates hold their charge. If the microcontroller resets and the GPIO goes high-impedance (floating), the MOSFET gate can retain charge and keep the relay energized. You must add a 10kΩ gate-to-ground pull-down resistor. A BJT naturally turns off when the base drive is removed, provided the GPIO pin has a defined internal or external pull-down during boot.
Step-by-Step Breadboard Verification
Before connecting your expensive microcontroller or high-voltage load, verify the driver stage on a breadboard using a multimeter.
- Isolate the MCU: Do not connect the ESP32 yet. Wire the 5V supply to the relay coil and the BJT Collector. Wire the BJT Emitter to ground. Install the 1N4007 diode (stripe to 5V).
- Verify Coil Resistance: Set your multimeter to Ohms. Measure across the relay coil pins. You should read ~70Ω. If it reads infinite, the coil is blown.
- Manual Trigger Test: Take a jumper wire connected to the 5V rail and briefly touch it to the BJT Base (through the 470Ω resistor). You should hear a sharp click. Remove the wire; you should hear a second click.
- Measure Saturation Voltage (V_CE): While manually triggering the base, measure the voltage between the BJT Collector and Emitter. It must read < 0.3V. If it reads 1V or higher, your BJT is not saturating (check your resistor value or BJT pinout).
- Check Flyback Clamping: If you have an oscilloscope, probe the Collector. Trigger the relay on and off. When turning off, you should see a brief spike clamped to roughly 5.7V (5V supply + 0.7V diode forward drop). If you see a 30V+ spike, your diode is backwards or missing.
- Integrate Logic: Connect the ESP32 GPIO to the base resistor. Write a simple sketch toggling the pin HIGH and LOW with a 2-second delay. Verify the relay clicks in time with the serial monitor output.
Sizing the High-Voltage Load Side (COM, NO, NC)
The control side is only half the relay in circuit equation. The mechanical contacts (COM, NO, NC) switching your actual load require their own design considerations. The Songle SRD-05VDC-SL-C is rated for 10A at 120VAC, but relay contact ratings degrade severely with inductive loads or DC voltage.
If you are switching a resistive load (like an incandescent bulb or heater), the 10A rating holds. If you are switching an inductive load (like an AC motor or a transformer), the inrush current and the arc generated when the contacts open will pit and weld the contacts. For inductive AC loads, derate the relay by 50% (treat it as a 5A switch) and consider adding an RC snubber network (e.g., 100Ω resistor in series with a 0.1µF X2 capacitor) across the NO and COM terminals to suppress arcing. For DC loads, the 10A rating drops to roughly 2A at 30VDC because DC arcs do not self-extinguish like AC zero-crossing arcs do. Always match the contact material and rating to the specific load impedance, not just the steady-state current.






