The standard low-side switching circuit of relay for 5V logic requires an NPN transistor (like a 2N3904), a 1kΩ base resistor, and a 1N4007 flyback diode across the coil. This topology protects your microcontroller from the relay coil's inductive voltage spike while providing the ~71mA current the coil needs, which a GPIO pin cannot supply directly. By calculating the exact forced beta and placing the flyback diode correctly, you prevent brownouts and fried silicon when switching heavy loads.
The Standard Low-Side Relay Topology
To build a reliable circuit of relay driven by a 3.3V microcontroller (like an ESP32 or Raspberry Pi) switching a 5V relay coil, we use a low-side NPN BJT configuration. We will design around the ubiquitous Songle SRD-05VDC-SL-C, which has a coil resistance of roughly 70Ω and requires about 71mA to pull in the contacts.
Node Labels and Topology Description
- Node A (GPIO): 3.3V logic output from the microcontroller.
- Node B (Base Resistor): 1kΩ resistor connecting Node A to Node C.
- Node C (Transistor Base): Base pin of the 2N3904 NPN transistor.
- Node D (Transistor Collector): Collector pin, connected to the relay coil's ground-side terminal.
- Node E (Relay Coil / VCC): The positive terminal of the relay coil, tied to the 5V supply rail.
- Node F (Flyback Diode): 1N4007 diode placed in parallel with the coil. Cathode (stripe) to Node E (5V), Anode to Node D (Collector).
- Node G (Emitter / GND): Transistor emitter tied to the common system ground.
Design Walkthrough: Picking Real Component Values
First, we calculate the coil current. According to the 5V relay datasheet, the 70Ω coil at 5V draws 71.4mA. A microcontroller GPIO pin is typically limited to 20mA continuous (40mA absolute max for ESP32), so direct driving will destroy the pin.
We need a transistor to act as a switch. The Texas Instruments 2N3904 datasheet shows it can handle up to 200mA collector current, making 71.4mA well within its safe operating area. To ensure the transistor acts as a closed switch (hard saturation), we need a forced beta ($h_{FE}$) of roughly 10 to 20.
Let's calculate the base resistor ($R_B$):
V_GPIO = 3.3V
V_BE (Base-Emitter drop) = 0.7V
Voltage across R_B = 3.3V - 0.7V = 2.6V
If we choose a standard 1kΩ resistor, the base current ($I_B$) is 2.6V / 1000Ω = 2.6mA.
Our forced beta is 71.4mA / 2.6mA = 27.4. Since the 2N3904 has a typical $h_{FE}$ of over 100 at this current level, a forced beta of 27 guarantees the transistor will fully saturate, dropping less than 0.2V across the Collector-Emitter junction. This leaves 4.8V for the relay coil, which is more than enough to reliably pull in the 5V armature.
Behavior Matrix: Component Changes and Failure Modes
Understanding what breaks at the extremes is critical for debugging. Here is the failure-mode contrast for the primary components in this circuit of relay.
| Component | Normal State Behavior | If OPEN (Broken/Disconnected) | If SHORTED (Bypassed/Failed) |
|---|---|---|---|
| Base Resistor (1kΩ) | Limits base current to 2.6mA; protects GPIO. | Relay never pulls in. GPIO reads floating or low. | GPIO sources >30mA directly into base. High risk of frying the MCU pin. |
| Flyback Diode (1N4007) | Clamps inductive spike to 5V rail when coil de-energizes. | Inductive spike (100V+) punches through transistor C-E junction or resets MCU. | 5V rail shorts to GND when transistor turns ON. Fries transistor and power supply. |
| NPN Transistor (2N3904) | Sinks 71mA to GND when GPIO is HIGH. | Relay never pulls in. Collector node floats to 5V via coil. | Relay is permanently stuck ON, regardless of GPIO state. |
| Relay Coil (70Ω) | Generates magnetic field to pull NO contact closed. | No current flows. Transistor stays cool. Load never switches. | Dead short across 5V and Collector. Transistor instantly overheats and fails. |
Why Low-Side NPN Beats High-Side PNP (and Direct GPIO)
When designing a circuit of relay, you might wonder why we don't just use a PNP transistor on the high side (between 5V and the coil) or an N-channel MOSFET. Here is the practical decision framework.
Low-Side NPN vs. High-Side PNP
A high-side PNP topology requires the base voltage to be roughly 0.7V below the emitter to turn on. If your emitter is at 5V, you must pull the base down to 4.3V. To turn it off, you must push the base back up to 5V. A 3.3V ESP32 GPIO cannot output 5V to turn the PNP off, meaning the relay will either stay permanently on or leak current. You would need an additional NPN level-shifter stage, doubling your part count. The low-side NPN references the common ground, allowing a 3.3V logic HIGH to easily turn on the 5V load.
BJT vs. Logic-Level MOSFET
You can absolutely use a logic-level N-channel MOSFET (like the IRLZ44N or 2N7000) instead of a 2N3904. MOSFETs draw virtually zero steady-state gate current. However, for a simple 71mA relay coil, the 2N3904 is cheaper, requires no gate-pulldown resistor to prevent floating-gate turn-on during MCU boot, and is less susceptible to static discharge damage on the breadboard. Save the MOSFETs for high-current loads like solenoid valves or DC motors drawing >500mA.
Step-by-Step Breadboard Testing & Verification
Do not connect your expensive microcontroller until you have verified the power stage. Follow this exact sequence to breadboard-test the circuit of relay.
- Verify the Coil: Before applying power, set your multimeter to resistance mode. Probe the relay coil pins. You should read between 65Ω and 75Ω. If it reads infinite (open), the internal thermal fuse is blown; discard the relay.
- Place the Flyback Diode: Insert the 1N4007 across the coil pins. The silver stripe (cathode) must face the positive 5V rail. If you reverse this, you will short the power supply the moment the transistor turns on.
- Wire the Transistor Stage: Connect the 2N3904 emitter to ground, the collector to the ground-side of the relay coil, and the 1kΩ resistor to the base. Leave the base resistor disconnected from the MCU for now.
- Manual Trigger Test: Power the 5V rail. Briefly touch the free end of the 1kΩ base resistor to the 3.3V or 5V rail. You should hear a distinct 'click'. Measure the voltage between Collector and Emitter with your multimeter; it should read < 0.2V (saturation).
- Verify Flyback Clamping: If you have an oscilloscope, probe the collector node. Trigger on the falling edge when you disconnect the base resistor. You should see the voltage spike clamp cleanly at roughly 5.7V (5V rail + 0.7V diode forward drop). If it spikes to 30V+, your diode is backward or open.
- Connect MCU: Finally, connect the base resistor to your ESP32/Arduino GPIO. Set the pin HIGH in code and verify the switching action.
Circuit of Relay FAQ
Can I use a MOSFET instead of a BJT in this circuit of relay?
Yes, you can substitute the 2N3904 with a logic-level N-channel MOSFET like the 2N7000 or IRLZ44N. Connect the gate to the GPIO via a 100Ω series resistor (to dampen high-frequency ringing from gate capacitance), the source to ground, and the drain to the relay coil. Crucially, you must add a 10kΩ pulldown resistor between the gate and ground. Without it, the gate acts as an antenna during MCU boot-up, potentially turning the relay on and off erratically before the GPIO initializes.
Why does my ESP32 reset when the circuit of relay switches off?
This is almost always caused by one of two issues. First, the flyback diode is missing, installed backward, or uses a slow-recovery type that isn't clamping the inductive spike fast enough, causing a ground-bounce that resets the MCU's brownout detector (BOD). Second, the 5V relay coil and the ESP32 are sharing a weak USB power supply. When the 71mA coil de-energizes, the sudden change in current draw can cause a voltage sag on a poor-quality USB cable. Power the relay coil from a separate 5V buck converter, tying only the grounds together.
Do I need an optocoupler in my circuit of relay for mains switching?
For basic hobbyist setups, the transistor and flyback diode provide sufficient protection for the MCU against the coil's inductive kickback. However, if you are switching noisy inductive mains loads (like large compressors or fluorescent ballasts), electromagnetic interference (EMI) can couple back through the relay's parasitic coil-to-contact capacitance. In these environments, inserting an optocoupler (like the PC817) between the MCU GPIO and the transistor base provides galvanic isolation, completely breaking the electrical path and protecting your low-voltage logic from high-voltage transients. As noted in application guides on relay isolation, this is a best practice for industrial or high-reliability deployments.






