NewAI Part GeneratorFree to get started
CIRCUIT DIAGRAM EXAMPLE

Relay Driver Circuit Diagram with NPN Transistor

A microcontroller pin can supply a few milliamps; a relay coil needs tens of milliamps. One NPN transistor bridges the gap, and a diode across the coil protects the transistor when the relay turns off.

NPN relay driverNPN relay driverGPIO → 470 Ω → 2N2222, 1N4007 flyback across the coilGPIO+5VR1470 ΩQ12N2222K15 V relayD11N4007Base currentIb = (5 V − 0.7 V) / 470 Ω ≈ 9.1 mACoil current it switches (saturated)Ic ≤ 10 × Ib (forced β = 10) ≈ 90 mATypical 5 V coil (70 Ω)Ic ≈ (5 V − 0.2 V) / 70 Ω ≈ 69 mAMade with ProtoFlow · protoflow.ai
About 9 mA of base current drives the transistor into saturation, so it switches the relay coil hard on. The diode catches the coil's voltage spike when the transistor turns off.

How this circuit works

When GPIO goes high, current flows through R1 into Q1's base. The base-emitter junction holds the base at about 0.7 V, so the base current is (5 V − 0.7 V) / 470 Ω ≈ 9.1 mA. That is well inside the 20 mA an Arduino Uno pin is recommended to supply, and it drives Q1 into saturation, where it acts like a closed switch from the bottom of the coil to ground.

A transistor used as a switch is sized with a forced β, the collector current divided by the base current, of about 10, far below its datasheet gain. Then it stays saturated, with only about 0.2 V across it, even for a low-gain part. With 9.1 mA of base current that allows about 90 mA of coil current. A typical 5 V relay coil of about 70 Ω draws (5 V − 0.2 V) / 70 Ω ≈ 69 mA, so the relay pulls in with margin.

A coil stores energy in its magnetic field and resists any sudden change in its current. When Q1 switches off, the coil would drive the collector tens or hundreds of volts above the supply, enough to break down the transistor. D1, reverse-biased across the coil, gives that current a path to circulate until it dies away and clamps the collector to about 0.7 V above 5 V.

The diode makes the relay release slightly more slowly. Where that matters, a Zener diode in series with D1 lets the coil current decay faster while still limiting the spike.

Formulas

Base current
Ib = (VGPIO − VBE) / R1 = (5 V − 0.7 V) / 470 Ω ≈ 9.1 mA

From a 3.3 V pin, use 330 Ω: (3.3 V − 0.7 V) / 330 Ω ≈ 7.9 mA, enough for about 79 mA of coil current.

Coil current the switch can carry
Ic ≤ βforced × Ib = 10 × 9.1 mA ≈ 90 mA

A forced β of 10 keeps the transistor saturated whatever its actual gain. Do not use the datasheet β here: β × Ib is how far the current could rise in the linear region, where the transistor would drop volts and run hot.

Coil current
Icoil ≈ (5 V − VCE(sat)) / Rcoil = (5 V − 0.2 V) / 70 Ω ≈ 69 mA

The relay's datasheet gives the coil resistance. It must stay below the 90 mA the switch can carry.

Parts list

RefPartValueWhat it does
D1Diode1N40071N4007 flyback diode, cathode to +5 V.
K1Relay coil5 V relay5 V relay coil. Its contacts (not drawn) switch the load.
Q1NPN transistor2N22222N2222 NPN transistor, the low-side switch.
R1Resistor470 Ω470 Ω base resistor. Sets the base drive to about 9 mA and keeps the GPIO pin within its rating.

Netlist

Recovered from the drawing itself: every pin on each net below is joined by a wire, a junction or a shared supply or ground symbol.

NetConnects
GPIOR1.1
N1R1.2, Q1.base
N2Q1.collector, K1.1, D1.A
GNDQ1.emitter
+5VK1.2, D1.K

Common mistakes

  • Leaving out the flyback diode. The circuit may work for a while, then the transistor or the microcontroller fails from the turn-off spike.
  • Fitting the diode the wrong way round. Forward-biased across the coil, it shorts the 5 V supply through Q1 the moment the transistor turns on.
  • Connecting the base straight to the GPIO pin with no resistor. The base-emitter junction is a diode, so the pin and the transistor both see excessive current.
  • Separate grounds. The relay supply and the microcontroller must share a ground, or the base current has no return path.

Build notes

  • The GPIO pin supplies the 9 mA base current, inside the Arduino Uno's 20 mA per-pin recommendation.
  • For a coil above about 90 mA, lower R1 or use a logic-level MOSFET.

Frequently asked questions

Why does a relay need a flyback diode?

The coil's inductance produces a large reverse voltage when its current is switched off. The diode gives that current a safe path and clamps the spike to about one diode drop above the supply.

Which way round does the flyback diode go?

Reverse-biased in normal operation: cathode (the striped end) to the positive supply, anode to the transistor's collector.

Can I drive a relay straight from an Arduino or ESP32 pin?

No. A small relay coil needs around 70 mA or more, well beyond what a GPIO pin should supply (about 20 mA on an Arduino Uno). Use a transistor like this, or a relay module that has one built in.

Can I use a MOSFET instead of the NPN transistor?

Yes. A logic-level N-channel MOSFET such as the AO3400 in the same position needs no steady drive current. Keep the flyback diode.

Draw your own version

Open this circuit in the AI circuit diagram maker and ask for a change in plain English, edit a value, switch to IEC symbols, or export it as SVG, PNG, PDF, a KiCad project or a SPICE netlist. Opening the example and exporting it are free.

Related circuit examples

All circuit diagram examples

From circuit diagram to PCB

Open the diagram in ProtoFlow, the free AI PCB design app for Windows, macOS and Linux: pick real parts, lay out and route the board, simulate with SPICE and export Gerbers. Imports and exports KiCad, Altium and EAGLE.

Get for Mac

Free to start. No card required.