Electronics Engineering DISCUSSION

How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

Started by lumatetedavid BJT switchbase resistor calculationtransistor saturationrelay driverflyback diode
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

lumatetedavid Electronics Engineering Forum
#1

I want to switch a 5 V relay (70 Ω coil, so about 70 mA) from a 3.3 V microcontroller pin using a general-purpose NPN transistor as a low-side switch. The datasheet gives hFE as 100 to 300.

Using the typical gain I calculated a base current of well under 1 mA and a base resistor of several kilohms, but tutorials for the same circuit use 1 kΩ or less. Which calculation is right, and what else does this circuit need?

Community replies 5

Re: How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

#2

For a switch you do not use hFE at all, you deliberately overdrive the base so the transistor saturates. The usual rule is a forced gain of 10 to 20: choose I_B = I_C / 20 at least. For 70 mA that is 3.5 mA.

The resistor drops the GPIO voltage minus V_BE: R_B = (3.3 V - 0.7 V) / 3.5 mA = 743 Ω. Pick the next standard value down, 680 Ω, which gives 2.6 V / 680 Ω = 3.8 mA. Check that against the pin's rated output current in the microcontroller datasheet; a few milliamps is within what most pins can supply.

Re: How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

#3

The reason the hFE figure misleads you: it is measured in the active region, with several volts across the transistor, and it varies by three to one between parts and with temperature. A resistor sized with a gain of 200 (about 7.4 kΩ for 0.35 mA) only just turns the transistor on. If your part has a gain of 100, the collector current stops at 35 mA, the rest of the supply voltage sits across the transistor, the relay may not pull in, and the transistor heats up.

Saturated, V_CE is around 0.1 to 0.2 V, so the coil sees about 4.8 V and the transistor dissipates roughly 0.2 V × 70 mA = 14 mW. Datasheets typically specify V_CE(sat) at I_C / I_B = 10 for this reason.

Re: How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

#4

The circuit needs a flyback diode across the coil, cathode to the +5 V side. When the transistor turns off, the coil inductance keeps the current flowing and the collector voltage rises until something breaks down, which is normally the transistor. With the diode the current circulates through the coil and decays, and the collector is clamped to about 5.7 V. A 1N4148 is adequate for a small signal relay; a 1N400x works too.

Also add a resistor of 10 kΩ to 100 kΩ from the GPIO side of the base resistor to ground. During reset and boot the pin is a floating input, and without the pull-down the relay can chatter or click at power-up.

Re: How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

#5

A logic-level N-channel MOSFET is the usual alternative, since the gate takes no steady current. The parameter to check is R_DS(on) specified at a gate voltage you can actually provide, 2.5 V or 3.3 V. The threshold voltage V_GS(th) is not a drive level: it is the point where the device conducts a few hundred microamps. A part with a threshold of 2 to 4 V will not switch properly from 3.3 V.

Keep the flyback diode and the pull-down (100 kΩ from gate to ground is typical), and a small series gate resistor of about 100 Ω limits the current spike into the gate capacitance.

Re: How do I calculate the base resistor for an NPN transistor switching a relay from a 3.3 V GPIO?

#6

To verify the finished circuit, measure with the relay energised: V_CE should be below about 0.3 V, V_BE about 0.7 V, and the GPIO pin should still be close to 3.3 V. If the pin sags noticeably, the output is being loaded near its limit and the base resistor should go up slightly.

Power the coil from the 5 V rail directly, not from the 3.3 V regulator, and make sure the 5 V supply and the microcontroller share a ground. If the microcontroller resets when the relay switches, add a bulk capacitor on the 5 V rail near the relay.

TEP COMMUNITY