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
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 , 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 . keeps the base firmly off while the microcontroller is still in reset and its pin floats.
Designing for saturation
In saturation is about to , so the load current is set by the load:
The forced beta is a design choice, deliberately far below the transistor's minimum active-region . A value of 10 to 20 is the usual rule: it saturates the worst-case transistor at low temperature with margin, and datasheets specify at . is higher than the active-region , typically to (onsemi's 2N3904: to at , at most at ; at most and at the same points). Overdriving more than necessary costs base current and slows turn-off.
Dissipation. On, the transistor dissipates , a few milliwatts here. Off, . 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: . When the transistor turns off in a few hundred nanoseconds, the coil tries to keep its 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 , and the stored energy dies away in the coil's own resistance with the time constant (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 , coil carrying has ; after one the current is () and the stored energy , down from . 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.
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 , 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 , 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 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 maximum), a 12 V relay with a coil, an NPN with , , .
Find: , a base resistor for , the actual forced beta, and the on-state dissipation.
Solve:
Check: is far below , so the transistor saturates. The pin supplies , well within its ; of it flows into , leaving for the base (a forced beta of , still deep in saturation). is nothing for a TO-92 package.
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 choice would also saturate a part, but with less margin for low temperature (where falls) and for the part's lower 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 in series with . The session runs the circuit twice, the second time with the flyback diode added.
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 (it is still finishing its own rise) with . Without the diode the model's collector flies to : the model has no breakdown, so only the junction capacitance stops it. A real transistor would avalanche at its (40 V for a typical small-signal part) and may be destroyed. With the diode the collector peaks at , 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 .
Simulation. The Python table and the SPICE run.
What you would measure. Use a bench supply at , current limit 100 mA, a 12 V relay (or a resistor plus LED for a first test), an NPN general-purpose transistor, , , a 1N4148-class diode across the coil, and a 3.3 V source (a microcontroller pin or a second supply channel). Expect of to on the meter (meter accuracy about on the 200 mV range), about to , and coil current within the coil's resistance tolerance (often ). On the scope (10× probe, ground at the emitter), the collector at turn-off should rise to about 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 ), 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 . 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
- A 5 V pin must switch a LED string from 12 V with . Choose (E12) with .
- Why does the relay release more slowly with a flyback diode than without one?
- A transistor switches with . What is the conduction loss, and is a TO-92 part (about maximum at 25 °C) suitable?
Answer
- ; (E12), giving .
- The diode lets the coil current decay slowly with (1 ms here); the magnetic field, and the armature's hold, last until the current falls below the release value.
- , 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 load. With a forced beta of 10, the base current should be…
Why is a forced beta far below the transistor's datasheet minimum chosen for a switch?
- it makes the transistor faster
- it guarantees saturation for the worst-case part and temperature
- it reduces the base current
- 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…
- several hundred volts
A relay coil has and . With a plain flyback diode, its current decays with a time constant of…