The Direct Answer: Sizing and Selecting Your BJT Switch
If you are building a smart home automation project and need to use a microcontroller (like an ESP32 or Arduino) to control a 120V or 240V home electrical load, you cannot wire the GPIO pin directly to a relay coil. You need a bipolar junction transistor switch to act as the bridge.
For 95% of home automation relay builds—specifically those using standard 5V DC signal relays like the Songle SRD-05VDC-SL-C (which draws about 70mA)—the undisputed default part is the 2N2222A (NPN). It is cheap, comes in a TO-92 package, and easily handles the coil current. If you are driving a heavy-duty industrial contactor coil that pulls over 500mA, step up to a TIP120 (Darlington NPN) in a TO-220 package.
BJT Pinout, Symbol, and Operation Regions
Before wiring, you must identify the pins. For the standard TO-92 packaged 2N2222A, hold the transistor with the flat side facing you and the pins pointing down. From left to right, the pins are Emitter (E), Base (B), and Collector (C). In schematic symbols, the Emitter is the leg with the arrow pointing outward (for NPN), the Base is the vertical line, and the Collector is the remaining angled leg.
A BJT operates in three distinct regions, but when used as a switch, we only care about two: Cutoff (fully OFF) and Saturation (fully ON). The Active region is for amplifiers, not switches.
| Region | Base-Emitter Voltage (Vbe) | Collector Current (Ic) | Collector-Emitter Voltage (Vce) | Switch State |
|---|---|---|---|---|
| Cutoff | < 0.6V | 0 mA (Leakage only) | Equals Supply Voltage | OFF (Open Circuit) |
| Active | ~0.7V | Ic = β × Ib | Variable (1V to Vcc) | Partial (Avoid in switching) |
| Saturation | > 0.7V (Typ 0.8V) | Max limited by load | < 0.3V (Vce-sat) | ON (Closed Circuit) |
To guarantee saturation, you must overdrive the base. A common engineering rule is to assume a forced beta (β) of 10. If your relay coil needs 70mA, you must supply at least 7mA to the base, regardless of the transistor's datasheet hFE (which might claim a gain of 100 or more).
Designing the Circuit: Interfacing 3.3V GPIO to a Home Electrical Relay
Let's build the exact control circuit for an ESP32 (3.3V logic) switching a 5V relay module that controls a 120V home lighting branch.
Component Values and Wiring Steps
- Calculate the Base Resistor: The ESP32 GPIO outputs 3.3V. The BJT Base-Emitter junction drops ~0.7V. This leaves 2.6V across the base resistor. To get our required 7mA of base current (for a 70mA relay coil), R = V / I = 2.6V / 0.007A = 371Ω. The closest standard E12 resistor value is 330Ω. This provides ~7.8mA, ensuring hard saturation.
- Wire the Base: Connect the ESP32 GPIO pin to one leg of the 330Ω resistor. Connect the other leg to the Base (B) of the 2N2222A.
- Wire the Emitter: Connect the Emitter (E) directly to the common Ground (GND) shared by the ESP32 and the 5V relay power supply.
- Wire the Collector and Load: Connect the Collector (C) to the negative (-) control pin of the relay coil. Connect the positive (+) pin of the relay coil to the 5V supply.
- Install the Flyback Diode: This is non-negotiable. Relay coils are inductors; when the BJT turns off, the collapsing magnetic field generates a massive reverse voltage spike that will instantly destroy your transistor. Place a 1N4148 or 1N4007 diode in reverse parallel across the relay coil pins (cathode stripe pointing toward the 5V positive side).
Decision Tree: Which BJT Part Number Should You Buy?
Do not guess your transistor based on whatever is lying in your junk bin. Use this decision matrix to select the right part for your specific relay or contactor coil current.
| Coil Load Current | Default Part Number | Package | Vce(sat) Drop | Verdict & Use Case |
|---|---|---|---|---|
| < 100 mA | 2N2222A or 2N3904 | TO-92 | ~0.3V | WINNER for standard 5V PCB signal relays (Songle, Omron G5V). |
| 100 mA - 1 A | TIP31C or BD139 | TO-220 | ~1.2V | Use for heavy-duty 12V/24V contactor coils. Requires a small heatsink. |
| 1 A - 5 A | TIP120 (Darlington) | TO-220 | ~2.0V | High gain, but wastes power as heat due to high Vce drop. Needs heatsink. |
| > 5 A | IRLZ44N (MOSFET) | TO-220 | N/A (Rds-on) | STOP. Do not use a BJT. Switch to a logic-level N-channel MOSFET. |
The Concrete Pick: For home automation projects involving ESP32s and standard 5V relay modules, buy a bulk pack of 2N2222A transistors and 330Ω resistors. They are the safest, most reliable default for this exact scenario.
Failure Modes and Multimeter Testing on the Bench
Transistors rarely fail from age; they fail from thermal runaway or inductive voltage spikes. When a BJT fails, it almost always fails as a dead short between the Collector and Emitter. This means the relay stays energized even when the microcontroller GPIO is LOW.
How to Test a BJT with a Digital Multimeter
You do not need a specialized transistor tester. Set your multimeter to Diode Test mode (the symbol with an arrow and a line). Remove the transistor from the circuit to avoid parallel resistance skewing your readings.
- Base to Emitter (Forward): Red probe on Base, Black on Emitter. Expect a reading of 0.600V to 0.750V.
- Base to Collector (Forward): Red probe on Base, Black on Collector. Expect 0.600V to 0.750V.
- Reverse Bias Checks: Swap the probes (Black on Base, Red on Emitter/Collector). The meter should read OL (Over Limit).
- Collector to Emitter: Probes on C and E in either direction. Must read OL. If you read 0.00V or a low resistance here, the junction is blown and the part is trash.
For deeper validation on the bench, wire the circuit and measure the voltage directly across the Collector and Emitter while the GPIO is HIGH. If your Vce measures higher than 0.4V, your transistor is not fully saturated. You need to lower your base resistor value to push more current into the base.
Safety Caveats for Mains-Side Home Wiring
While the BJT handles the low-voltage DC side, the relay contacts are switching your home's 120V AC branch circuit. This is where electrical code and safety practices become critical. The NFPA 70 (National Electrical Code) strictly governs how these conductors must be handled.
Wire Sizing and Color Codes
When wiring the relay output to a standard 120V receptacle or hardwired light fixture, you must match the branch circuit breaker size.
- 15A Breaker: Use minimum 14 AWG THHN in conduit, or 14/2 NM-B (Romex) cable.
- 20A Breaker: Use minimum 12 AWG THHN or 12/2 NM-B cable.
Follow standard US color codes strictly: The Black (Hot) wire is the only conductor that gets broken by the relay. Wire the 120V AC Black wire into the relay's Common (COM) and Normally Open (NO) terminals. The White (Neutral) wire must never be switched; it passes straight through, wire-nutted directly to the load. The Bare or Green (Ground) wire must be continuously bonded to the metal junction box and the load chassis.
NEC Switching Rules and Isolation
Per semiconductor switching principles and NEC Article 404.2(b), you are forbidden from switching the grounded (neutral) conductor. The relay must interrupt the ungrounded (hot) conductor. Furthermore, never run your low-voltage DC ESP32 wires in the same conduit or junction box as your 120V AC mains wires unless a physical, grounded metal barrier separates them (NEC 300.3). Keep the 5V DC control wiring entirely isolated from the AC load box to prevent lethal fault conditions if a wire nut vibrates loose.
By pairing a properly biased 2N2222A bipolar junction transistor switch with a correctly sized flyback diode and code-compliant mains wiring, you create a home automation interface that is both electronically robust and legally safe.






