Driving a BJT from a microcontroller

Using a BJT as a switch starts with one resistor: the base resistor. It must be small enough to saturate the worst-case part, and large enough not to overload the GPIO. Then come PNP high-side switches, Darlingtons and the common mistakes.

DrivingChapter 05

The goal: saturation, not the active region

As a switch, a BJT must run in saturation. There VCE falls to 0.05–0.3 V and the transistor barely warms up.

With too little base current it stays in the active region. IC is capped at β·IB and VCE rises. The transistor dissipates VCE·IC, and the load does not get its full voltage.

Saturation condition:   βmin · IB  ≫  IC(load)   ⇒   IB = IC / βforced,   βforced ≈ 10…20 βforced is chosen far below the datasheet’s minimum hFE. The ratio between them is the “overdrive” margin against temperature, current and part spread.

Base resistor: step by step

  1. Know the load current. IC = Vload / Rload, or the datasheet current. Include inrush if there is any.
  2. Read hFE(min) at that current. Gain falls at high current: a 2N3904 has ≥ 100 at 10 mA but ≥ 30 at 100 mA. Use the table’s min value.
  3. Pick βforced: 10–20. Rule of thumb: IB = IC/10. βforced should be at most half of hFE(min).
  4. Compute RB. The resistor sees VGPIO − VBE(sat); VBE(sat) ≈ 0.7–0.9 V. If there is an RBE, add the current it steals.
  5. Round down to a standard value. A smaller RB means deeper saturation.
  6. Check the GPIO current. On most MCUs 8–12 mA is comfortable; ~20 mA is near the absolute limit.
  7. Check the dissipation. P ≈ VCE(sat)·IC + VBE·IB. In a TO-92 (≈ 200 °C/W), above 0.2–0.3 W is hot.
RB = (VGPIO − VBE(sat)) / (IB + VBE/RBE)    overdrive = βmin·IB / IC Without RBE the second term is zero. Overdrive ≥ 2 is good, 1–2 is marginal, < 1 means no saturation.
Lab

Low-side NPN switch calculator

Model: VBE(sat) = 0.75 V, resistive load, β = hFE(min) (worst-case part, 25 °C). VCE(sat) is a rough estimate; read the real value from the datasheet. GPIO VOH sag is ignored. Plot: IC, VCE and transistor loss (relative to V·I/4) as RB grows.
Too large an RB = a silent failure The load still works “somehow”, but the transistor slides into the active region, VCE climbs to volts and the package heats. A circuit that works on the bench fails on a cold morning or with a low-hFE batch. Try it in the lab with “choose RB manually”.

The base pull-down resistor (RBE)

A 10–100 kΩ resistor from base to emitter does two jobs:

  • Floating pin: during reset and boot the MCU leaves its pins as Hi-Z inputs. A floating base can half-turn-on from noise; RBE holds it at 0 V.
  • Leakage: ICBO grows when hot and an open base multiplies it by β. RBE drains it to the emitter and speeds up turn-off.

The price: with the GPIO high, VBE/RBE flows into RBE instead of the base. Rule: RBE ≥ 5–10 × RB.

IB = (VGPIO − VBE)/RB − VBE/RBE    when off: VBE = Ileak·RBE ≪ 0,5 V
VBE0.75 VICBO (hot)1 µA
  1. Current stolen by 10 kΩI = 0.75 V / 10 kΩ75 µAnegligible at a few mA of IB, serious at 200 µA
  2. VBE when off, 47 kΩV = 1 µA · 47 kΩ47 mVfar below 0.5 V
RBE keeps the transistor off and steals little base current.

PNP high-side: the trap and the fix

A PNP sits above the load with its emitter on the positive rail. It turns on when the base is pulled ~0.7 V below the emitter. To turn it off, the base must be pulled up to the emitter voltage.

If the load rail equals the GPIO voltage (3.3 V), the GPIO can do this directly: HIGH → off, LOW → on.

On a 12 V rail the PNP never turns off. The GPIO lifts the base to 3.3 V at most, leaving VEB = 8.7 V. Worse, base current flows through the pin’s ESD diode into the MCU’s 3.3 V rail.

The fix is an NPN level shifter. The GPIO drives the NPN, the NPN pulls the PNP base down. A pull-up resistor turns the PNP off.

Animation

12 V PNP high-side: direct GPIO vs NPN level shifter

The GPIO toggles every 2 seconds. On the left the PNP conducts in both states; in HIGH the red dots are current flowing into the pin. On the right the 10 kΩ pull-up turns the PNP off, the 1 kΩ sets the PNP base current (~11 mA), the 4.7 kΩ the NPN base current (~0.55 mA). The right-hand logic is inverted: GPIO HIGH = load ON.

Sizing the right-hand circuit

V+12 VLoad200 mAVGPIO3.3 Vβforced10
  1. PNP base currentIB = 200 mA / 1020 mA
  2. R2 (PNP base)R2 = (12 − 0.8 − 0.2) V / 20 mA≈ 560 ΩVEB(sat) 0.8 V, NPN VCE(sat) 0.2 V
  3. NPN base currentIB = 20 mA / 102 mA
  4. R1 (NPN base)R1 = (3.3 − 0.75) V / 2 mA≈ 1.2 kΩ
  5. Current stolen by the pull-upI = 0.8 V / 10 kΩ80 µAnegligible
R1 = 1.2 kΩ, R2 = 560 Ω, pull-up 10 kΩ. The GPIO supplies only 2 mA.

Darlingtons and the ULN2003/ULN2803

A Darlington is two BJTs in cascade; the gain is β1·β2 (1000+). A GPIO can switch hundreds of mA with very little current.

The price: the output transistor cannot saturate. VCE(sat) becomes ≈ 0.9–1.6 V; the loss is 5–10 times that of a single BJT.

The ULN2003 (7 channels) and ULN2803 (8 channels) are Darlington arrays. Each input has a 2.7 kΩ base resistor, each output a flyback diode to COM. Always tie COM to the load supply.

OptionIC per channelVCE(sat) / VDSDriveNote
Single NPN (BC337)≤ 800 mA0.1–0.7 VIC/10GPIO can’t supply enough at high current
Darlington (TIP120)≤ 5 A≈ 2 V @ 3 Atiny (hFE ≥ 1000)Runs hot; replace with a MOSFET
ULN2003A / ULN2803A≤ 500 mA0.9 V @ 100 mA, 1.3 V @ 350 mAdirect GPIO, internal 2.7 kΩBuilt-in flyback diodes; package loss limited to ~1 W
Logic-level MOSFET (AO3400A)≤ 5 AI·RDS(on) ≈ 0.04 Ω·Ivoltage, ~0 currentBest choice for most new designs
MOSFET array (TPL7407L)≤ 600 mA≈ 0.1–0.3 Vdirect GPIOULN2003 pin-compatible, much lower loss
Lab

Loss comparison: BJT, Darlington, MOSFET

Approximate models: single BJT 0.05 V + 0.4 Ω·I (βforced = 10), ULN2003 0.8 V + 1.4 Ω·I (datasheet typical points), MOSFET 40 mΩ (3.3 V gate). Package limit: ULN2003 DIP-16, RθJA ≈ 67 °C/W, practical ~1 W at 25 °C.

Common mistake: using an emitter follower as a switch

An NPN above the load is not a switch but an emitter follower. The emitter follows ~0.7 V below the base. With a 3.3 V GPIO the load sees at most ~2.6 V; the rest of the 12 V turns into heat in the transistor.

The layout is tempting because the load stays grounded. An NPN high-side only works if the base can be driven above the load rail.

Lab

Low-side switch vs emitter follower side by side

Both circuits use the same GPIO, load and NPN (β = 100, VBE = 0.7 V; RB on the left is small enough to saturate). In the follower Vload = VGPIO − 0.7 V; the rest stays across the transistor as VCE.

Digital (pre-biased) transistors

Parts like DTC114E, PDTC114E or MUN2211 are “digital transistors”: small NPNs with a built-in R1 (base) and R2 (base–emitter). PNP versions are DTA…. One part saves space, and you can’t forget the resistors.

  • Output current is small: typically 50–100 mA. Fine for relay coils, LEDs and small buzzers.
  • The R1 = 10 kΩ (“114”) versions need ~3 V; marginal at 3.3 V. Pick the 4.7 kΩ (“143”) or 2.2 kΩ (“123”) version and check VI(on).
  • The internal R2 is already the pull-down; no external one needed.

Worked examples

Example 1: How does an ESP32 (3.3 V) drive a 12 V, 80 mA relay coil?

2N3904 low-side switch, βforced = 10, 10 kΩ base pull-down.

VGPIO3.3 VCoil12 V, 150 ΩTransistor2N3904RBE10 kΩ
  1. Load currentIC = 12 V / 150 Ω80 mA
  2. hFE(min) @ 80 mA60 at 50 mA, 30 at 100 mA≈ 40
  3. Base currentIB = 80 mA / 108 mA
  4. Overdrive40 · 8 mA / 80 mA4≥ 2 → good
  5. RB(3.3 − 0.8) V / (8 + 0.08) mA309 Ωnext lower E24 value: 300 Ω
  6. GPIO current(3.3 − 0.8) V / 300 Ω8.3 mAcomfortable for an ESP32
  7. Dissipation0.3 V · 80 mA + 0.8 V · 8 mA≈ 30 mWVCE(sat) ≈ 0.3 V
RB = 300 Ω, RBE = 10 kΩ. Always put a flyback diode (1N4148) across the coil; see chapter 08.

Why the popular “1 kΩ” choice is marginal:

  1. Base currentIB = (3.3 − 0.8) V / 1 kΩ2.5 mA
  2. Overdrive40 · 2.5 mA / 80 mA1.25
1 kΩ is marginal. It works on the bench, but a bad batch drops out of saturation.

A BC547B also works, but its IC(max) is 100 mA, so 80 mA is close to the limit.

Example 2: Can a BC337 switch a 500 mA load from a GPIO?

The problem is not the transistor but the base current the GPIO can supply.

Load500 mATransistorBC337-25hFE(min) @ 500 mA≈ 40VCE(sat)≈ 0.5 V (typ.)
  1. Base current, βforced = 10IB = 500 mA / 1050 mAno GPIO can supply this
  2. Base current, βforced = 25IB = 500 mA / 2520 mAthe pin is at its absolute limit
  3. Overdrive40 · 20 mA / 500 mA1.6marginal
  4. DissipationP = 0.5 V · 0.5 A0.25 W
  5. Temperature rise (TO-92)ΔT = 0.25 W · 200 °C/W+50 °C
Do not drive a single BC337 from a GPIO. Use a logic-level MOSFET: an AO3400A drops 20 mV at 0.5 A, dissipates 10 mW and draws ≈ 0 GPIO current.

Other options: two stages (a small NPN feeds the BC337 base from V+) or a Darlington (over 1 V drop, ~0.6 W loss).