A relay is the simplest way to switch a 12 V load — a pump, a solenoid lock, a mains lamp through its contacts — from a 5 V microcontroller pin. But the pin cannot drive the relay coil itself: the coil needs far more current than a pin can supply, and when the coil switches off it throws a voltage spike back at whatever is driving it. This guide sizes the one-transistor driver that sits between them — a 2N2222A switching a 12 V relay from a 5 V Arduino pin — with every resistor value worked out, including the flyback diode that keeps the transistor alive.
Figure 1: (a) The complete driver: RB sets the base current, the 2N2222A switches the coil current to ground, the 1N4007 clamps the turn-off spike, and the LED shows when the relay is energised. (b) Coil current against time — it rises with the coil time constant Ï„ = L/R = 0.75 ms, crosses the ~21 mA pull-in level in about 0.9 ms, and decays in ~3 ms after switch-off with the diode fitted. Without the diode the collector spikes to several hundred volts, far past the transistor's 40 V rating. (Schematic, not to scale.)Why the pin cannot drive the relay directly
Two numbers rule the pin out. First, the coil current: a typical small 12 V relay has a coil resistance of 400 Ω, so the coil draws I = 12 V / 400 Ω = 30 mA — and an Arduino pin is rated for 20 mA, with 40 mA as an absolute maximum it must never be asked to deliver. Second, the coil is an inductor: it stores energy, and when the current is interrupted the coil fights the change by driving its terminal voltage wherever it has to go to keep the current flowing. That is the spike that kills output pins and transistors. So the pin only has to supply a small base current, and the transistor does the heavy switching from the 12 V rail.
The coil current — 30 mA and 0.36 W
The coil is a plain resistor as far as the steady current is concerned: I = V / R = 12 / 400 = 30 mA, and the power it turns into heat and magnetic pull is P = V × I = 12 × 0.03 = 0.36 W. The transistor sits between the coil and ground, so it carries the same 30 mA. When it is on hard — saturated — its collector-emitter voltage is small: the 2N2222A datasheet guarantees VCE(sat) of 0.3 V maximum at 150 mA, and at our 30 mA a typical value is about 0.2 V. So the transistor burns only P = 0.2 × 0.03 = 6 mW, and the coil sees 12 − 0.2 = 11.8 V — 29.5 mA in practice, which the relay cannot tell from 30 mA. A saturated switch is efficient precisely because almost all the voltage appears across the load, not the switch. One detail matters: use the 2N2222A, not the plain 2N2222. The A version is rated VCEO 40 V and VCE(sat) 0.3 V maximum; the plain 2N2222 is only 30 V and 0.4 V. The common TO-92 part sold as PN2222A is the A version, so this is what you almost certainly have — but check the suffix, because the 40 V rating is what the flyback section below relies on.
Sizing the base resistor — forced beta 20
The datasheet hFE of 100–300 is measured in the active region, at VCE = 10 V. A switch does not work there: to saturate the transistor you deliberately overdrive the base, using a forced beta of about 20 instead of the datasheet gain. So the base current must be IB = IC / 20 = 30 mA / 20 = 1.5 mA. The base resistor drops the difference between the pin voltage and the base-emitter voltage: RB = (5 − 0.7) / 0.0015 = 2,867 Ω. The nearest standard value is 2.7 kΩ, giving an actual base current of IB = 4.3 / 2,700 = 1.59 mA — a forced beta of 30 / 1.59 = 18.9, still hard saturation. Check the pin: an Arduino pin must source 1.59 mA against its 20 mA rating, a factor of 12.6 in hand — fine. The resistor itself burns P = 4.3 V × 1.59 mA = 6.8 mW, so an ordinary ¼ W resistor is barely warm. And the design is robust: even if VBE(sat) rose to 0.9 V, the base current would be (5 − 0.9) / 2,700 = 1.52 mA, still above the 1.5 mA target. Add the base power to the collector power and the whole transistor dissipates about 6 + 1.1 = 7.1 mW — no heatsink, not even warm to the touch.
The flyback diode — a 1N4007 that clamps the collector to 12.7 V
When the transistor switches off, the coil current of 30 mA cannot stop instantly. The coil inductance — about 0.3 H for a relay this size — holds stored energy of ½ × L × I² = 0.5 × 0.3 × 0.03² = 135 µJ, and the coil will drive its collector end to whatever voltage keeps that current flowing. Without a diode that means hundreds of volts, far past the 2N2222A's 40 V VCEO, and the transistor dies by avalanche — often on the first switch-off. The fix is one diode across the coil, cathode to +12 V, anode to the collector: normally reverse-biased and invisible, it conducts the moment the collector tries to rise above the rail and clamps it to 12 + 0.7 = 12.7 V (0.7 V is the diode's forward drop at this small current; the 1N4007 is rated 1,000 V and 1 A, so it is loafing). The price is a slower release: the coil current now decays through the coil resistance with Ï„ = L/R = 0.3 / 400 = 0.75 ms, falling to ~2% in 4Ï„ ≈ 3 ms, so the relay releases in ~3–5 ms instead of ~1 ms. For a relay that is slow anyway, that is rarely a problem; if it is, a resistor in series with the diode or a Zener clamp releases it faster. (An earlier guide on this blog worked through the flyback energy itself in detail; here the diode is only the protection element — the driver sizing is the subject.)
Second worked example — a 5 V relay from a 3.3 V ESP32 pin
The same steps work at 3.3 V logic. Take a 5 V relay with a 125 Ω coil: I = 5 / 125 = 40 mA, P = 5 × 0.04 = 0.20 W. Forced beta 20 gives IB = 40 / 20 = 2 mA, and RB = (3.3 − 0.7) / 0.002 = 1,300 Ω. The nearest standard value is 1.2 kΩ, giving IB = 2.6 / 1,200 = 2.17 mA — against the ESP32's recommended 12 mA per pin, a factor of 5.5 in hand. The resistor burns 2.6 × 2.17 mA = 5.6 mW. At the worst-case VCE(sat) of 0.3 V the coil sees 5 − 0.3 = 4.7 V and draws 4.7 / 125 = 37.6 mA — still comfortably above the pull-in level, since a relay typically pulls in at about 70% of its rated voltage, which is 3.5 V here. Note the trap this example avoids: never connect the coil between the pin and ground, and never trust a 3.3 V pin to switch a relay through a MOSFET whose threshold is close to 3 V — that is the next section.
MOSFET variant — a 2N7000 from a 5 V pin, and an LED indicator
A MOSFET driver is even simpler — no base current at all — but the part choice matters more than it looks. The 2N7000 is the usual jellybean: 60 V, 200 mA continuous, with Rds(on) of 1.8 Ω typical and 5.3 Ω maximum at VGS = 4.5 V. Driven from a 5 V pin, the drop at 30 mA is 5.3 × 0.03 = 0.16 V worst case (0.054 V typical) and the conduction loss is 0.03² × 5.3 = 4.8 mW — a perfectly good switch, with a 100 kΩ pull-down from gate to ground (50 µA when the pin is high) to hold it off while the pin floats at boot. But do not drive a 2N7000 from a 3.3 V ESP32 pin: its gate threshold is 0.8–3.0 V, so at 3.3 V it is barely on and its on-resistance is not even specified — the 5 Ω figure belongs to a 4.5 V gate, not a 3.3 V one. For 3.3 V logic use a true logic-level FET such as the AO3400, whose Rds(on) is specified at 2.5 V. Finally, an LED across the coil shows the relay state: with a 2 V LED at 10 mA, R = (12 − 2) / 0.01 = 1 kΩ, burning 10 × 0.01 = 0.1 W — use a ¼ W resistor, which has a factor of 2.5 in hand.
Key points to remember
- Worked example: 12 V relay, 400 Ω coil, 2N2222A from a 5 V Arduino pin. Coil current I = 12 / 400 = 30 mA, coil power 0.36 W. VCE(sat) ≈ 0.2 V, so the transistor burns only 6 mW (7.1 mW including the base).
- Base resistor: forced beta 20 (not the datasheet hFE) gives IB = 30 / 20 = 1.5 mA. RB = (5 − 0.7) / 1.5 mA = 2,867 Ω → 2.7 kΩ standard, actual IB 1.59 mA. The pin sources 1.59 mA against its 20 mA rating; RB burns 6.8 mW. Even at VBE = 0.9 V the base current is still 1.52 mA.
- Flyback diode: 1N4007 across the coil (cathode to +12 V, 1,000 V / 1 A rated) clamps the collector to 12.7 V. Without it the stored 135 µJ (L ≈ 0.3 H) drives the collector to hundreds of volts, past the 2N2222A's 40 V VCEO. With it the current decays with Ï„ = 0.75 ms and the relay releases in ~3–5 ms.
- Second example: 5 V relay, 125 Ω → 40 mA, from a 3.3 V ESP32 pin. IB = 2 mA; RB = (3.3 − 0.7) / 2 mA = 1.3 kΩ → 1.2 kΩ standard, IB 2.17 mA (pin rated ~12 mA). At worst-case VCE(sat) 0.3 V the coil sees 4.7 V and 37.6 mA — still above the ~3.5 V pull-in level.
- Use the 2N2222A, not the plain 2N2222 (30 V / 0.4 V). MOSFET variant: 2N7000 from a 5 V pin, Rds(on) 5.3 Ω max at 4.5 V gate → 0.16 V drop at 30 mA, 100 kΩ gate pull-down; do not drive it from 3.3 V (threshold up to 3.0 V) — use a logic-level FET instead. LED indicator: (12 − 2) / 10 mA = 1 kΩ, 0.1 W in the resistor.
