The Standard Switching Relay Circuit Topology

Microcontrollers like the ESP32 and Raspberry Pi Pico operate at 3.3V logic and can safely source only 12mA to 20mA per GPIO pin. A standard 5V electromechanical relay requires 70mA to 100mA to pull in its contacts. You cannot drive the coil directly from the silicon; doing so will instantly destroy the GPIO bank. The solution is a low-side switching relay circuit using an NPN bipolar junction transistor (BJT) as a current amplifier, protected by a flyback diode.

Here is the exact node-by-node topology for a low-side NPN driver:

  • VCC_5V: 5V power supply rail. Connects to Relay Coil Pin 1 and the Flyback Diode Cathode (stripe end).
  • NODE_COIL_LO: Relay Coil Pin 2. Connects to the NPN Collector and the Flyback Diode Anode.
  • NODE_BASE: NPN Base. Connects to the Base Resistor (R_B).
  • MCU_GPIO: Microcontroller output pin (3.3V HIGH). Connects to the other side of R_B.
  • GND: Common ground. Connects to the NPN Emitter, the 5V power supply ground, and the microcontroller ground.

When the MCU drives HIGH, current flows through R_B into the base, forward-biasing the base-emitter junction. This allows a much larger current to flow from VCC_5V, through the relay coil, into the collector, and out the emitter to GND. The magnetic field pulls the relay armature. When the MCU drives LOW, the BJT cuts off, the magnetic field collapses, and the flyback diode provides a recirculation path for the inductive spike.

Component Selection & Design Walkthrough

Let us pick real component values for a 3.3V ESP32 driving a standard 5V relay. We will use the Songle SRD-05VDC-SL-C, one of the most common relays on the hobbyist market.

1. Calculate Coil Current

The SRD-05VDC-SL-C datasheet specifies a coil resistance of 71.4Ω. Using Ohm's Law:

I_coil = V / R = 5V / 71.4Ω = 70mA

The transistor must handle at least 70mA continuously. The 2N2222A (TO-92 package) has a maximum collector current (Ic) of 800mA, giving us massive headroom.

2. Calculate Base Resistor (R_B)

To ensure the 2N2222 acts as a closed switch (saturation) rather than a linear amplifier, we must force a specific base current. In saturation, the DC current gain (hFE) drops. We use a 'forced beta' of 20 to guarantee hard saturation.

I_base = I_coil / forced_beta = 70mA / 20 = 3.5mA

The ESP32 outputs 3.3V. The base-emitter voltage drop (Vbe) of a silicon BJT in saturation is roughly 0.7V. The voltage across the base resistor is therefore 3.3V - 0.7V = 2.6V.

R_B = V_Rb / I_base = 2.6V / 3.5mA = 742Ω

The closest standard E12 resistor value is 680Ω (yielding 3.8mA) or 1kΩ (yielding 2.6mA). While 680Ω perfectly hits our forced beta of 20, a 1kΩ resistor is the superior practical choice. At 1kΩ, the base current is 2.6mA, resulting in a forced beta of 27. The 2N2222 will still saturate fully (Vce drop < 0.3V), but we reduce the current draw on the ESP32 GPIO pin from 3.8mA to 2.6mA, extending the lifespan of the microcontroller's internal bond wires.

3. The Flyback Diode

A relay coil is an inductor. When the BJT switches off, the current attempts to continue flowing. Without a path, the voltage at NODE_COIL_LO will spike to 50V or more, instantly exceeding the 2N2222's 40V Vceo rating. A standard 1N4007 rectifier diode placed in reverse bias across the coil clamps this spike to roughly -0.7V, safely dissipating the stored energy.

Behavior & Failure Modes at the Extremes

Understanding what breaks when a single element fails is critical for debugging. Here is the behavior matrix for this topology:

Element AlteredConditionPhysical ResultCircuit Consequence
Flyback DiodeRemoved / OpenInductive kickback (V = -L di/dt) spikes to >50V at NODE_COIL_LO.Avalanche breakdown of 2N2222. Collector-Emitters shorts. 5V backfeeds through the base-collector junction into the 3.3V MCU_GPIO, frying the silicon.
Base Resistor (R_B)Shorted (0Ω)3.3V applied directly across the Base-Emitter diode.The base-emitter junction acts as a dead short. The ESP32 GPIO attempts to source >100mA, exceeding its absolute maximum rating. The pin's internal bond wire melts, permanently killing the GPIO.
Relay CoilOpen CircuitNo current flows through the collector.Transistor switches perfectly (Vce rises to 5V), but no magnetic field is generated. The relay remains in its default state (NO open, NC closed).
Flyback DiodeInstalled BackwardsDiode is forward-biased when VCC is applied.Direct short from 5V VCC through the diode to the BJT collector. When the BJT turns on, it shorts 5V to GND, drawing massive current until the BJT melts or the 5V supply trips its overcurrent protection.

Decision Tree: BJT vs. MOSFET vs. Darlington Array

While the 2N2222 BJT is the workhorse of relay driving, it is not the only option. Use this decision path to select the right driver for your specific constraints.

Question 1: How many relays are you switching simultaneously?

  • 4 or more relays: Stop using discrete transistors. Use a ULN2003A Darlington transistor array. It packs 7 drivers into a single 16-pin DIP, includes internal base resistors (compatible with 5V logic, requires level shifting or pull-ups for 3.3V), and has built-in flyback diodes tied to a common COM pin. (TI ULN2003A Datasheet).
  • 1 to 3 relays: Proceed to Question 2.

Question 2: What is the coil current and switching frequency?

  • Current > 150mA OR switching via PWM (>10Hz): Use a logic-level N-Channel MOSFET like the 2N7000 (for <200mA) or IRLZ44N (for high current). MOSFETs are voltage-controlled, drawing virtually zero steady-state current from the GPIO, and they switch faster without the charge-storage delays inherent to BJTs.
  • Current < 150mA AND switching slowly (<5Hz, standard on/off control): Use an NPN BJT.

The Default Concrete Pick: For 90% of hobbyist projects involving a single 5V Songle relay on an ESP32 or Arduino, the 2N2222A + 1kΩ base resistor + 1N4007 flyback diode is the optimal choice. It is cheap ($0.05 per assembly), tolerant of minor wiring errors, and the 1kΩ resistor perfectly protects 3.3V logic limits while providing sufficient base drive.

Step-by-Step Breadboard Verification

Never plug a newly wired relay circuit directly into your microcontroller and upload code. A single misplaced diode stripe will kill your $6 ESP32 dev board. Follow this bench verification sequence using a multimeter and a jumper wire.

Safety & Verification Rule: Always test the power stage independently of the logic stage first. According to Espressif GPIO specifications, continuous current above 40mA causes irreversible damage. We will simulate the logic HIGH manually.

  1. Wire the Power Stage Only: Connect VCC_5V and GND to the breadboard power rails. Wire the relay coil, the 2N2222 (verify the flat-side pinout: Emitter, Base, Collector), the 1kΩ resistor, and the 1N4007 diode. Do not connect the MCU_GPIO node to your microcontroller yet.
  2. Verify the Flyback Orientation: Use your multimeter in continuity/diode mode. Place the red probe on NODE_COIL_LO and the black probe on VCC_5V. You should read a diode drop (~0.6V). If you read a short (0.0V), your diode is backwards. Fix it before proceeding.
  3. Measure Coil Resistance: With the 5V power turned OFF, place your DMM probes across the relay coil pins. Verify you read approximately 71.4Ω. If you read infinite (OL), the internal coil is severed.
  4. Simulate Logic HIGH: Turn the 5V power ON. Take a jumper wire and briefly touch one end to the 3.3V rail (or the 5V rail, the 1k resistor limits current safely either way) and the other end to the MCU_GPIO node (the free leg of the 1kΩ resistor). You should hear a distinct mechanical 'click' from the relay.
  5. Measure Saturation Voltage: While holding the jumper wire HIGH, place your DMM (DC Volts) across the Collector and Emitter of the 2N2222. You should read less than 0.3V. If you read 2V or higher, the transistor is in the linear region, not saturated. Drop your base resistor to 680Ω or 470Ω and re-test.
  6. Verify Dropout: Remove the jumper wire. The relay should audibly drop out with a second 'click'. The DMM across the coil should now read the full 5V (since the BJT is open and no current is flowing, there is no voltage drop across the coil's resistance).

Once these six steps pass, your power stage is proven safe. You can now connect the MCU_GPIO node to your ESP32 or Arduino pin, confident that a software bug toggling the pin at 100kHz will not result in a magic smoke event. For deeper theory on inductive load management, the Electronics Tutorials relay switching guide provides excellent oscilloscope captures of the flyback clamping action.