Here’s a clear, reliable way to switch a relay with a transistor (works for microcontrollers, sensors, or logic outputs).
.jpg?x-oss-process=image/auto-orient,1/quality,q_70/format,webp)
The go-to topology (low-side switch)
-
Q1: NPN BJT (e.g., 2N2222, BC337) or logic-level N-MOSFET (e.g., AO3400, IRLZ44N).
-
D1 (flyback diode): across coil, cathode to +V, anode to transistor side.
-
R_G/B: gate/base resistor from controller output to Q1 input.
-
(Optional) R_pull-down from MOSFET gate to ground (100 kΩ) to keep it off at reset.
-
(Optional) LED + resistor in parallel with coil (or on the logic side) for “relay on” indication.
Why the diode?
When you turn the relay off, the coil’s magnetic field collapses and makes a high-voltage spike. D1 gives that current a safe path, protecting Q1 and your MCU. Place D1 close to the relay.
Picking parts & resistor values
Option A — N-MOSFET (recommended)
-
Choose a logic-level MOSFET with:
-
V_DS ≥ 2× supply (e.g., ≥30 V for 12 V relays)
-
R_DS(on) low at your gate voltage (2.5–3.3–5 V), and
-
I_D ≥ 3× coil current for margin.
-
-
Gate resistor (R_G): 100–330 Ω tames ringing and limits gate inrush.
-
Gate pull-down: 47–220 kΩ to keep it off at boot.
-
No steady gate current → great for MCUs with tiny pin drive.
Example (3.3 V MCU, 5 V relay, 70 mA coil):
AO3400A (logic-level, low R_DS(on)), R_G = 150 Ω, R_Gpull-down = 100 kΩ, D1 = 1N4148 (ok at 70 mA coil) or 1N5819 (Schottky).
Option B — NPN BJT (works, but check drive)
-
Pick I_C rating > 3× coil current.
-
Drive the base into saturation with forced β (h_FE) of ~10.
-
Compute base current: IB=IC/10.
-
Base resistor: RB=(Vdrive−0.7 V)/IB.
Example (5 V logic, 12 V relay, 30 mA coil):
Target IB=30 mA/10=3 mA.
RB≈(5−0.7)/0.003=4.3/0.003≈1433 Ω → use 1.5 kΩ.
MCU pins often source only 4–8 mA; this is fine here.
If your coil is 70 mA, IB≈7 mA → many MCU pins can’t supply that; prefer a MOSFET or a Darlington (e.g., TIP122/ULN2003A).
Supply & layout tips
-
Power the relay from a rail that can deliver the coil current plus margin. Decouple with 100 nF ceramic close to the transistor and a 10–100 µF bulk cap near the relay supply.
-
Keep short, thick traces for the coil current loop (V+, coil, transistor, ground).
-
Route logic ground back to the MCU ground star-style to avoid injecting coil current noise into logic ground.
Faster release options (if timing matters)
A simple diode slows the relay release slightly. To speed it up:
-
Use a TVS instead of a plain diode (clamps higher → faster decay).
-
Add a diode + Zener in series across the coil (e.g., Zener 12–24 V for a 12 V relay).
-
Or a RC snubber across contacts for contact arcing (not the coil).
Additions you might want
-
Status LED: in parallel with the coil (with its own series resistor), or on the logic side.
-
Optocoupler isolation: for noisy environments; pair with a transistor/MOSFET on the relay side.
-
Prebuilt arrays: ULN2003A/ULN2803A (7/8 channels of Darlington + built-in diodes) are great for multiple relays (5 V/12 V coils).
Quick reference BOM (single relay, 5 V coil @ 70 mA, 3.3 V MCU)
-
Q1: AO3400A (SOT-23 logic-level NMOS)
-
D1: 1N5819 (Schottky flyback)
-
R_G: 150 Ω
-
R_pull-down: 100 kΩ
-
C_decouple: 100 nF near Q1, 47 µF near relay supply
-
Optional LED: 2 mA target → R≈(5−2)/0.002=1.5 kΩ
