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.
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.
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.
Ib = (VGPIO − VBE) / R1 = (5 V − 0.7 V) / 470 Ω ≈ 9.1 mAFrom 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.
Ic ≤ βforced × Ib = 10 × 9.1 mA ≈ 90 mAA 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.
Icoil ≈ (5 V − VCE(sat)) / Rcoil = (5 V − 0.2 V) / 70 Ω ≈ 69 mAThe relay's datasheet gives the coil resistance. It must stay below the 90 mA the switch can carry.
| Ref | Part | Value | What it does |
|---|---|---|---|
| D1 | Diode | 1N4007 | 1N4007 flyback diode, cathode to +5 V. |
| K1 | Relay coil | 5 V relay | 5 V relay coil. Its contacts (not drawn) switch the load. |
| Q1 | NPN transistor | 2N2222 | 2N2222 NPN transistor, the low-side switch. |
| R1 | Resistor | 470 Ω | 470 Ω base resistor. Sets the base drive to about 9 mA and keeps the GPIO pin within its rating. |
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.
| Net | Connects |
|---|---|
| GPIO | R1.1 |
| N1 | R1.2, Q1.base |
| N2 | Q1.collector, K1.1, D1.A |
| GND | Q1.emitter |
| +5V | K1.2, D1.K |
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.
Reverse-biased in normal operation: cathode (the striped end) to the positive supply, anode to the transistor's collector.
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.
Yes. A logic-level N-channel MOSFET such as the AO3400 in the same position needs no steady drive current. Keep the flyback diode.
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.
An Arduino Uno reads a pot on A0 and dims an LED with PWM on D9.
A 2N3904 with divider bias, an emitter bypass and coupling capacitors.
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.
Free to start. No card required.