TExcelTAD

The BJT as a Switch

The first transistor circuit most engineers build is not an amplifier but a switch: a microcontroller pin that can deliver a few milliamps at 3.3 V has to turn on a 12 V relay, a string of LEDs or a small fan. A transistor used as a switch lives in only two of its regions, cutoff (off) and saturation (on), and the design questions are different from an amplifier's: how much base current guarantees saturation for the worst transistor, how much power the switch dissipates, how fast it turns off, and what an inductive load does to the collector at the moment of turn-off. This lesson is the roadmap's laboratory project "design a transistor switch".

The low-side switch

+12 V L 400 mH RK 400 Ohm relay coil D1 flyback C Q1 G GPIO 0/3.3 V RB 1 kOhm RBE 10k IC = 29.5 mA (switch on)

The transistor's emitter is grounded and the load sits between the supply and the collector: a low-side switch. With the pin low the base sees 0 V0\ \text{V}, the transistor is cut off and only leakage (nanoamps) flows. With the pin high the base is driven hard enough that the load current, not the transistor, limits ICI_C. RBER_{BE} keeps the base firmly off while the microcontroller is still in reset and its pin floats.

Designing for saturation

In saturation VCE,satV_{CE,\text{sat}} is about 0.050.05 to 0.2 V0.2\ \text{V}, so the load current is set by the load:

IC=VCC−VCE,satRload,IB≥ICβforced,RB=Vdrive−VBE,satIBI_C = \frac{V_{CC} - V_{CE,\text{sat}}}{R_\text{load}}, \qquad I_B \ge \frac{I_C}{\beta_\text{forced}}, \qquad R_B = \frac{V_\text{drive} - V_{BE,\text{sat}}}{I_B}

The forced beta βforced=IC/IB\beta_\text{forced} = I_C/I_B is a design choice, deliberately far below the transistor's minimum active-region β\beta. A value of 10 to 20 is the usual rule: it saturates the worst-case transistor at low temperature with margin, and datasheets specify VCE,satV_{CE,\text{sat}} at IC/IB=10I_C/I_B = 10. VBE,satV_{BE,\text{sat}} is higher than the active-region 0.65 V0.65\ \text{V}, typically 0.750.75 to 0.9 V0.9\ \text{V} (onsemi's 2N3904: 0.650.65 to 0.85 V0.85\ \text{V} at 10 mA/1 mA10\ \text{mA}/1\ \text{mA}, at most 0.95 V0.95\ \text{V} at 50 mA/5 mA50\ \text{mA}/5\ \text{mA}; VCE,satV_{CE,\text{sat}} at most 0.20.2 and 0.3 V0.3\ \text{V} at the same points). Overdriving more than necessary costs base current and slows turn-off.

Dissipation. On, the transistor dissipates P≈VCE,satIC+VBEIBP \approx V_{CE,\text{sat}} I_C + V_{BE} I_B, a few milliwatts here. Off, P≈0P \approx 0. During the transition both voltage and current are large, so a switch that changes state thousands of times a second also has a switching loss; the power electronics course treats that in detail.

Inductive loads and the flyback diode

A relay coil, a motor or a solenoid is an inductor in series with a resistance. Its current cannot change instantly: vL=L di/dtv_L = L\, di/dt. When the transistor turns off in a few hundred nanoseconds, the coil tries to keep its 30 mA30\ \text{mA} flowing and drives the collector as high as needed to do it, far above the supply, until the transistor's collector junction breaks down. The flyback (freewheeling) diode across the coil gives the current somewhere to go: the collector rises to only one diode drop above VCCV_{CC}, and the stored energy 12LI2\frac12 L I^2 dies away in the coil's own resistance with the time constant τ=L/R\tau = L/R (RL Circuits). The price is a slower release of the relay; a Zener in series with the diode releases faster at the cost of a higher collector voltage.

The lab shows that decay: a 400 mH400\ \text{mH}, 400 Ω400\ \Omega coil carrying 30 mA30\ \text{mA} has τ=1.00 ms\tau = 1.00\ \text{ms}; after one τ\tau the current is 11.0 mA11.0\ \text{mA} (36.8 %36.8\ \%) and the stored energy 24.4 μJ24.4\ \mu\text{J}, down from 180 μJ180\ \mu\text{J}. The model is an ideal RL circuit, so here it stands for the coil current circulating through its own resistance and the diode, with the diode drop ignored.

+-R = 400 Ohmsource shorted at t = 0V = 12.0 V, L = 400 mHtau = L/R = 1.00 mst1.00 mst/tau1.00vL-4.41 Vi11.0 mAvR4.41 V-12.0-6.00.06.012.001 tau2 tau3 tau4 tau5 tau6 tautv (V)-30 mA0 A30 mAi36.8 %5 tau: 0.7 %vL(t), inductor voltagei(t)

Switching speed

A saturated transistor holds excess charge in its base. At turn-off that charge must be removed before the collector current can fall: the storage time tst_s, typically hundreds of nanoseconds for small-signal parts, more for power transistors. Ways to shorten it: a smaller forced overdrive, a base-emitter resistor that pulls charge out, a speed-up capacitor across RBR_B, or a Baker clamp (a Schottky diode from base to collector that stops the transistor saturating deeply). For loads switched a few times per second, as here, none of this matters.

The PNP high-side switch

To switch the positive side of a load (so the load stays grounded), use a PNP with its emitter on the supply. It turns on when its base is pulled about 0.7 V0.7\ \text{V} below the emitter. A 3.3 V pin cannot turn off a PNP whose emitter is at 12 V (the pin cannot reach 11.3 V), so a small NPN is added to pull the PNP's base down through a resistor, with a pull-up resistor from its base to its emitter to turn it off. The design steps are the same: load current, forced beta, base resistor. For currents above a few hundred milliamps a MOSFET is usually the better switch (MOSFET Biasing and the MOSFET Switch).

Worked example

Given: a 3.3 V GPIO pin (rated 8 mA8\ \text{mA} maximum), a 12 V relay with a 400 Ω400\ \Omega coil, an NPN with βmin=100\beta_\text{min} = 100, VCE,sat=0.2 VV_{CE,\text{sat}} = 0.2\ \text{V}, VBE,sat=0.75 VV_{BE,\text{sat}} = 0.75\ \text{V}.

Find: ICI_C, a base resistor for βforced≈10\beta_\text{forced} \approx 10, the actual forced beta, and the on-state dissipation.

Solve:

IC=12 V−0.2 V400 Ω=29.5 mA,IB≥2.95 mARB≤3.3 V−0.75 V2.95 mA=864 Ω  →  use 1 kΩ with IB=2.55 mAβforced=29.5/2.55=11.6P=0.2 V⋅29.5 mA+0.75 V⋅2.55 mA=5.9 mW+1.9 mW=7.8 mW\begin{aligned} I_C &= \frac{12\ \text{V} - 0.2\ \text{V}}{400\ \Omega} = 29.5\ \text{mA}, \qquad I_B \ge 2.95\ \text{mA} \\ R_B &\le \frac{3.3\ \text{V} - 0.75\ \text{V}}{2.95\ \text{mA}} = 864\ \Omega \;\to\; \text{use } 1\ \text{k}\Omega \text{ with } I_B = 2.55\ \text{mA} \\ \beta_\text{forced} &= 29.5/2.55 = 11.6 \\ P &= 0.2\ \text{V} \cdot 29.5\ \text{mA} + 0.75\ \text{V} \cdot 2.55\ \text{mA} = 5.9\ \text{mW} + 1.9\ \text{mW} = 7.8\ \text{mW} \end{aligned}

Check: βforced=11.6\beta_\text{forced} = 11.6 is far below βmin=100\beta_\text{min} = 100, so the transistor saturates. The pin supplies 2.55 mA2.55\ \text{mA}, well within its 8 mA8\ \text{mA}; 75 μA75\ \mu\text{A} of it flows into RBER_{BE}, leaving 2.48 mA2.48\ \text{mA} for the base (a forced beta of 11.911.9, still deep in saturation). 7.8 mW7.8\ \text{mW} is nothing for a TO-92 package.

python · try it yourself
VCC, R_load, VCEsat, VBEsat, Vdrive = 12.0, 400.0, 0.2, 0.75, 3.3
beta_min, L = 100, 0.4

IC = (VCC - VCEsat) / R_load
for RB in [470, 1000, 2200, 10000]:
    IB = (Vdrive - VBEsat) / RB
    bf = IC / IB
    state = "saturated" if bf < beta_min else "NOT saturated"
    print(f"RB = {RB:5d} Ohm: IB = {IB*1e3:5.2f} mA, forced beta = {bf:6.1f} -> {state}")
print(f"IC = {IC*1e3:.1f} mA, coil energy = {0.5*L*IC**2*1e6:.0f} uJ, tau = {L/R_load*1e3:.1f} ms")
RB =   470 Ohm: IB =  5.43 mA, forced beta =    5.4 -> saturated
RB =  1000 Ohm: IB =  2.55 mA, forced beta =   11.6 -> saturated
RB =  2200 Ohm: IB =  1.16 mA, forced beta =   25.5 -> saturated
RB = 10000 Ohm: IB =  0.25 mA, forced beta =  115.7 -> NOT saturated
IC = 29.5 mA, coil energy = 174 uJ, tau = 1.0 ms

The 2.2 kΩ2.2\ \text{k}\Omega choice would also saturate a β=100\beta = 100 part, but with less margin for low temperature (where β\beta falls) and for the part's lower β\beta near saturation.

Cross-check with SPICE: with and without the diode

The deck turns the switch on for 5 ms and then off. QN and DSIG are models written for this course; the coil is 400 mH400\ \text{mH} in series with 400 Ω400\ \Omega. The session runs the circuit twice, the second time with the flyback diode added.

bash · try it yourself
cat > relay.cir <<'EOF'
* 3.3 V GPIO drives a 12 V relay coil through a 1k base resistor
.model QN NPN(Is=1e-14 Bf=200 Vaf=100 Br=2 Rb=10 Cje=10p Cjc=4p Tf=0.4n)
.model DSIG D(Is=2.52n N=1.752 Rs=0.568 Cjo=4p M=0.4 Tt=20n BV=100 IBV=0.1u)
VCC vcc 0 12
VG g 0 PULSE(0 3.3 0 100n 100n 5m 20m)
RB g b 1k
Q1 c b 0 QN
LK vcc m 400m
RK m c 400
.control
tran 1u 10m uic
meas tran ic_on find i(LK) at=4.9m
meas tran vce_on find v(c) at=4.9m
meas tran vc_max max v(c)
.endc
.end
EOF
echo "without flyback diode:"
ngspice -b relay.cir 2>&1 | grep -E '^(ic_|vce|vc_)'
sed -i 's/^LK/D1 c vcc DSIG\nLK/' relay.cir
echo "with flyback diode:"
ngspice -b relay.cir 2>&1 | grep -E '^vc_'
without flyback diode:
ic_on               =  2.964598e-02
vce_on              =  5.338544e-02
vc_max              =  7.741663e+03 at=  5.003582e-03
with flyback diode:
vc_max              =  1.275475e+01 at=  5.000540e-03

At 4.9 ms the coil carries 29.6 mA29.6\ \text{mA} (it is still finishing its own L/RL/R rise) with VCE,sat=53 mVV_{CE,\text{sat}} = 53\ \text{mV}. Without the diode the model's collector flies to 7.7 kV7.7\ \text{kV}: the model has no breakdown, so only the junction capacitance stops it. A real transistor would avalanche at its VCEOV_{CEO} (40 V for a typical small-signal part) and may be destroyed. With the diode the collector peaks at 12.75 V12.75\ \text{V}, one diode drop above the supply.

Guided lab: a relay or LED switch

Hand calculation. As in the worked example; for an LED load instead use Rload=(VCC−VF−VCE,sat)/IFR_\text{load} = (V_{CC} - V_F - V_{CE,\text{sat}})/I_F.

Simulation. The Python table and the SPICE run.

What you would measure. Use a bench supply at 12 V12\ \text{V}, current limit 100 mA, a 12 V relay (or a 470 Ω470\ \Omega resistor plus LED for a first test), an NPN general-purpose transistor, RB=1 kΩR_B = 1\ \text{k}\Omega, RBE=10 kΩR_{BE} = 10\ \text{k}\Omega, a 1N4148-class diode across the coil, and a 3.3 V source (a microcontroller pin or a second supply channel). Expect VCE,satV_{CE,\text{sat}} of 5050 to 150 mV150\ \text{mV} on the meter (meter accuracy about ±1 mV\pm 1\ \text{mV} on the 200 mV range), VBEV_{BE} about 0.750.75 to 0.8 V0.8\ \text{V}, and coil current within the coil's resistance tolerance (often ±10 %\pm 10\ \%). On the scope (10× probe, ground at the emitter), the collector at turn-off should rise to about 12.7 V12.7\ \text{V} and stay there for a few milliseconds while the current decays. Do not remove the diode to "see the spike" on a real circuit: it can destroy the transistor, and the spike may reach far beyond the scope probe's rating.

Hazards and safe setup. Low voltage only. Fit the flyback diode before the first power-up, observe its polarity (cathode to +12 V+12\ \text{V}), keep the relay's switched contacts unconnected (never wire mains to them in this lab), and set the supply's current limit.

Pitfalls

  • Designing with the typical β\beta. Use the minimum and a forced beta of 10 to 20.
  • Omitting the flyback diode. The commonest way to kill a switch transistor and to reset a nearby microcontroller.
  • A diode the wrong way round. It shorts the supply through the transistor when it turns on; the current limit saves the parts.
  • Forgetting the GPIO's current rating. Check the pin's maximum and the total per port in the microcontroller's datasheet.
  • No base pull-down. A floating base during reset can half-turn the transistor on.

Practice

  1. A 5 V pin must switch a 60 mA60\ \text{mA} LED string from 12 V with βforced=15\beta_\text{forced} = 15. Choose RBR_B (E12) with VBE,sat=0.8 VV_{BE,\text{sat}} = 0.8\ \text{V}.
  2. Why does the relay release more slowly with a flyback diode than without one?
  3. A transistor switches 500 mA500\ \text{mA} with VCE,sat=0.4 VV_{CE,\text{sat}} = 0.4\ \text{V}. What is the conduction loss, and is a TO-92 part (about 0.6 W0.6\ \text{W} maximum at 25 °C) suitable?
Answer
  1. IB=60/15=4 mAI_B = 60/15 = 4\ \text{mA}; RB=(5−0.8)/4 mA=1.05 kΩ→1.0 kΩR_B = (5 - 0.8)/4\ \text{mA} = 1.05\ \text{k}\Omega \to 1.0\ \text{k}\Omega (E12), giving 4.2 mA4.2\ \text{mA}.
  2. The diode lets the coil current decay slowly with τ=L/R\tau = L/R (1 ms here); the magnetic field, and the armature's hold, last until the current falls below the release value.
  3. P=0.4⋅0.5=0.2 WP = 0.4 \cdot 0.5 = 0.2\ \text{W}, about a third of the package rating at 25 °C and less at higher ambient: it works, warm, but a larger transistor or a MOSFET is the better choice.

Test yourself

A BJT switch drives a 40 mA40\ \text{mA} load. With a forced beta of 10, the base current should be…

  1. 0.4 mA0.4\ \text{mA}
  2. 4 mA4\ \text{mA}
  3. 40 mA40\ \text{mA}
  4. 400 mA400\ \text{mA}

Why is a forced beta far below the transistor's datasheet minimum chosen for a switch?

  1. it makes the transistor faster
  2. it guarantees saturation for the worst-case part and temperature
  3. it reduces the base current
  4. it keeps the transistor in the active region

With a flyback diode across a relay coil on a 12 V supply, the collector voltage at turn-off peaks at about…

  1. 0.7 V0.7\ \text{V}
  2. 12.7 V12.7\ \text{V}
  3. 24 V24\ \text{V}
  4. several hundred volts

A relay coil has L=200 mHL = 200\ \text{mH} and R=400 ΩR = 400\ \Omega. With a plain flyback diode, its current decays with a time constant of…

  1. 0.5 ms0.5\ \text{ms}
  2. 2 ms2\ \text{ms}
  3. 80 s80\ \text{s}
  4. 0.5 s0.5\ \text{s}