Transistors as Switches

Basic Electronics

Course 1 · Chapter 6 · Transistors as Switches

A microcontroller pin can supply a few milliamps at 3.3 V or 5 V. A motor wants hundreds of milliamps at 6 V or 12 V, a relay coil wants 50 mA at 12 V, an LED strip wants amps. The transistor bridges the gap: a tiny current or voltage at one terminal controls a much larger current between the other two, so a weak signal can switch a heavy load. Used as a switch it is either fully off or fully on, and that is the only way of using it in this chapter (the half-on, amplifying use comes in Basic Electronics 7). The chapter builds on Ohm's law and power (Basic Electronics 1), series circuits (Basic Electronics 2), voltage dividers and pull-down resistors (Basic Electronics 3), the gate-charging time constant (Basic Electronics 4) and the flyback diode (Basic Electronics 5).

1. The idea: a switch you control with electricity

A transistor has three terminals. Two of them carry the load current; the third is the control. Put it in series with the load and it behaves like a switch whose contacts are operated by the control terminal. The ideal switch has an off state with no current and an on state with no voltage drop. A real transistor leaves a small drop in the on state (the cost, which turns into heat) and a tiny leakage in the off state (usually negligible).

Bipolar junction transistor (BJT)MOSFET
Terminals (control first)Base (B), Collector (C), Emitter (E)Gate (G), Drain (D), Source (S)
Load current flowsCollector to emitter (NPN)Drain to source (N-channel)
Controlled byA small current into the baseA voltage on the gate (almost no current once charged)
On-state lossA fixed drop, VCE(sat), about 0.1 to 0.3 VA resistance, RDS(on), from milliohms to a few ohms
Two polaritiesNPN and PNPN-channel and P-channel
Reading the symbols in a schematic: BJT: the emitter leg carries an ARROW. NPN: the arrow points OUT, away from the base (mnemonic: "Not Pointing iN"). PNP: the arrow points IN, towards the base. The arrow shows the direction of conventional current in that leg. MOSFET: the gate is drawn as a plate, separate from the channel (it is insulated: no gate current flows once the gate has charged). An arrow on the source/body leg points IN for N-channel, OUT for P-channel. Most real MOSFETs have a built-in body diode from source to drain (N-channel) that you cannot remove. In this chapter's diagrams each transistor is shown as a labelled box with its three pins: C D | | B o------[Q1 NPN] G o----[Q2 N-MOSFET] | | E S

Three regions, two of them useful

RegionBJT conditionWhat the transistor doesUse
Cut-offVBE below about 0.5 V, no base currentNo load current: the switch is OFFSwitch off
ActiveBase current flows, but not enough to saturateIC = hFE × IB: acts as a current amplifier; VCE in the middleAmplifiers (Basic Electronics 7), not switching
SaturationSo much base current that the load, not the transistor, limits ICVCE falls to VCE(sat), about 0.2 V: the switch is ONSwitch on

2. The BJT as a switch

Current gain, hFE

For an NPN transistor, a current IB flowing into the base (and out of the emitter) lets a much larger current IC flow into the collector. In the active region the two are proportional:

IC = hFE x IB IE = IC + IB = (hFE + 1) x IB (the emitter carries both) hFE (also written beta) is the DC current gain. For a 2N3904 the datasheet gives hFE from about 100 to 300 at 10 mA, but only a MINIMUM of about 60 at 50 mA, and it varies with temperature and from sample to sample by 2:1 or more. A design must work with the minimum, not the typical. Base-emitter voltage: the base-emitter junction is a diode (Basic Electronics 5): VBE is about 0.65 to 0.7 V in the active region and up to about 0.8 to 0.95 V in hard saturation. Use 0.8 V for the base resistor sums below.

Saturation and the base resistor

With the load connected, the supply and the load resistance cap the collector current: it can never exceed what the load allows when the transistor is a perfect closed switch. Call that the saturation current:

IC(sat) = (Vs - VCE(sat)) / RL the most the load can draw The minimum base current that just reaches saturation is IB(min) = IC(sat) / hFE(min). But at that point the transistor is only just on: VCE(sat) is still falling, hFE is a guess, and a hot transistor has a different hFE. So deliberately supply MUCH more base current: choose a "forced beta" (IC(sat) divided by the base current you supply) of about 10: IB = IC(sat) / 10 forced beta = 10 RB = (Vdrive - VBE(sat)) / IB Vdrive = the logic HIGH level of the pin; VBE(sat) about 0.8 V Then round RB DOWN to a standard value (more base current is safe; less may not saturate). The datasheet quotes VCE(sat) at a base current of one tenth of the collector current, so forced beta 10 is also the condition under which the datasheet's VCE(sat) is true.

Worked example 1: a 6 V, 50 mA load from a 5 V pin

A 120 Ω resistor stands in for a 6 V relay coil (or any 50 mA load). The pin is a 5 V output (an Arduino Uno or Nano is the natural board here: 5 V logic and a pin that can supply up to 20 mA comfortably).

+6 V | [ R2 120 ] load (a 6 V, 50 mA relay coil stand-in) | C | pin o---[ R1 820 ]---+-----------B Q1 2N3904 (NPN) | | [ R3 10 k ] E | | 0 V o----------------+-----------+ R3 is optional: it keeps the base at 0 V while the pin is floating (during a reset), so Q1 stays off. 1. Saturation current (VCE(sat) = 0.2 V): IC(sat) = (6 - 0.2) / 120 = 48.3 mA 2. Base current for forced beta 10: IB = 48.3 / 10 = 4.83 mA 3. Base resistor: RB = (5 - 0.8) / 0.00483 = 869 ohm -> next standard value DOWN: 820 ohm 4. Check with R1 = 820 ohm: current in R1 = (5 - 0.8) / 820 = 5.12 mA (the pin supplies this; the Uno limit is 20 mA) through R3: 0.8 / 10000 = 0.08 mA into the base: 5.12 - 0.08 = 5.04 mA forced beta = 48.3 / 5.04 = 9.6 (against a minimum hFE of 60 at 50 mA: 6 times spare) 5. Power: transistor: P = VCE(sat) x IC = 0.2 x 0.0483 = 9.7 mW (TO-92 limit is about 625 mW) load: P = IC^2 x RL = 0.0483^2 x 120 = 0.28 W (a 1/2 W resistor, 56 % loaded) base resistor: 0.00512^2 x 820 = 21.5 mW 6. Voltages with the switch ON: load gets 6 - 0.2 = 5.8 V. OFF: the load gets 0 V and Q1 holds off 6 V.

Worked example 2: a 12 V relay from a 3.3 V pin

The relay coil is 12 V, 240 Ω (50 mA). A 3.3 V board (a Raspberry Pi Pico or an ESP32) cannot supply as much base current as an Uno: the Pico defaults to 4 mA per pin (12 mA at most when configured), and about 10 mA is a sensible ceiling on an ESP32.

IC(sat) = (12 - 0.2) / 240 = 49.2 mA forced beta 10: IB = 4.92 mA, RB = (3.3 - 0.8) / 0.00492 = 508 ohm -> 470 ohm check: IB = 2.5 / 470 = 5.3 mA, forced beta 9.2 (OK on an ESP32; above the Pico's default 4 mA) If the pin cannot give 5 mA, relax the forced beta. The datasheet minimum hFE at 50 mA is 60, so forced beta 20 still leaves a safety factor of 3: RB = 1 kohm: IB = 2.5 / 1000 = 2.5 mA, forced beta = 49.2 / 2.5 = 19.7 (fine for a Pico at its default) The coil needs the flyback diode from Basic Electronics 5 (cathode to +12 V, anode to the collector): +12 V o--------+----------+ | | [ relay coil ] K D1 1N4001 (cathode, the band, to +12 V) | /_\ A | | +----------+ | C | pin o--[ RB ]--------B Q1 2N3904 | E | 0 V o----------------+
Tie the grounds together
The 3.3 V board and the 12 V supply are two separate supplies. The transistor's emitter must connect to the common 0 V shared by both. With no shared ground there is no complete path for the base current and nothing switches, or it switches erratically.

What goes wrong if the base current is too small

Same circuit as example 1, but R1 = 22 kohm (and no R3), 5 V drive: IB = (5 - 0.7) / 22000 = 0.195 mA Sample with hFE = 100: IC would be 100 x 0.195 mA = 19.5 mA load drop = 19.5 mA x 120 = 2.35 V, VCE = 6 - 2.35 = 3.65 V NOT saturated transistor power = 3.65 x 0.0195 = 71 mW, load only gets 2.35 V (a relay may not pull in) Sample with hFE = 300: IC would be 58.6 mA, more than the 48.3 mA the load allows, so it saturates. The same circuit behaves differently with different transistors and at different temperatures: a design that works only for a lucky hFE is a bad design. Forced beta 10 removes the dependence. Why the half-on state is dangerous for the transistor, with a resistive load RL on supply Vs: P = VCE x IC = VCE x (Vs - VCE) / RL This is largest when VCE = Vs / 2: Pmax = Vs^2 / (4 RL) = 36 / 480 = 75 mW here. Fine for a 2N3904, but a 12 V, 2 ohm load would give 144 / 8 = 18 W: the part would burn out in a moment. A switch must pass through the half-on region quickly and spend its time fully off or fully on.

When the BJT cannot do it: the Darlington

Suppose the load is a 6 V motor drawing 400 mA. Forced beta 10 asks for IB = 40 mA, far more than a microcontroller pin can give. Raising the forced beta needs a transistor with far more gain. A Darlington pair puts two transistors in one package, with the first one's emitter feeding the second one's base, so the gains multiply:

Q1 (small) feeds the base of Q2 (power). Overall gain = hFE1 x hFE2. Example: 100 x 100 = 10,000. The TIP120 guarantees hFE of at least 1000 (a ratio of 250 or more is easy to meet). Two junctions in series: VBE(total) = 0.7 + 0.7 = 1.4 V (use 1.4 V in the base-resistor sum) Q1 can never saturate Q2 fully, because Q1's collector sits one VBE above Q2's emitter: VCE(sat) = VCE1(sat) + VBE2 = 0.2 + 0.7 = about 0.9 V (datasheets give 1 to 2 V at higher currents) Example: 6 V motor, 400 mA, 5 V pin, TIP120. Forced beta 250. IB = 400 / 250 = 1.6 mA RB = (5 - 1.4) / 0.0016 = 2250 ohm -> 2.2 kohm (IB = 3.6 / 2200 = 1.64 mA) Motor voltage = 6 - 0.9 = 5.1 V (15 % of the supply is lost) Heat in the transistor = 0.9 x 0.4 = 0.36 W (a TO-220 is fine alone up to about 1 to 2 W) A logic-level MOSFET with RDS(on) of 25 milliohm in the same job loses 0.4^2 x 0.025 = 4 mW.

PNP, briefly

A PNP transistor is the mirror image: the emitter connects to the positive supply, the load sits between collector and 0 V, and the transistor turns on when the base is pulled about 0.7 V below the emitter, with current flowing out of the base. That makes it a high-side switch (section 4). With a logic signal the trouble is the off state: if the emitter is at +12 V and the pin's HIGH level is only 5 V (or 3.3 V), the base is still 7 V (or 8.7 V) below the emitter and the PNP stays on. A PNP is therefore easy to use only when the logic level equals the supply; otherwise use the level-shifter circuit in section 6.

3. The MOSFET as a switch

How a MOSFET switches

In an N-channel enhancement MOSFET the gate is a metal plate separated from the silicon by a thin layer of oxide. A positive voltage on the gate (relative to the source) pulls electrons into a channel under the plate, connecting drain to source. No current flows into the gate itself (apart from the brief charge of its capacitance), so the pin supplies nothing once the gate is charged. A P-channel device is the mirror image: its source goes to the positive supply and it turns on when the gate is pulled below the source.

N-channel: VGS = VG - VS must be POSITIVE and above the threshold to turn on. Source usually at 0 V. P-channel: VGS = VG - VS must be NEGATIVE: VGS = -10 V means the gate is 10 V BELOW the source. Two numbers decide everything for a switch: 1. Gate threshold voltage VGS(th): the VGS at which the drain current is only a tiny test value (typically 250 microamps). It is the voltage where the part just begins to conduct. It is NOT the voltage at which it is fully on. 2. On-resistance RDS(on): the drain-source resistance when fully on, and it is quoted AT a stated VGS (for example 25 milliohm at VGS = 5 V). Look for the test voltage in the datasheet. Small-signal model for a MOSFET in the ohmic (switch) region, for small VDS: ID = k x (VGS - VGS(th)) x VDS so RDS(on) = VDS / ID = 1 / ( k x (VGS - VGS(th)) ) k is a constant of the device. The overdrive, VGS - VGS(th), sets how low RDS(on) can go: double the overdrive and RDS(on) roughly halves. (Real parts follow this loosely: use it to understand the datasheet, not to predict it.) Illustration, taking VGS(th) = 1.5 V: RDS(on) at VGS = 3.3 V is about 3.5 / 1.8 = 1.9 times the value at 5 V. Taking a standard-level part with VGS(th) = 3 V, rated 44 milliohm at 10 V: at VGS = 3.3 V the overdrive is 0.3 V instead of 7 V, so the model gives 44 mohm x 7 / 0.3 = about 1 ohm. And the worst sample (4 V) is OFF.

Standard-level versus logic-level

A logic-level MOSFET is designed so that RDS(on) is low with only 4.5 V (or 2.5 V) on the gate, so a microcontroller pin can switch it directly. A standard-level MOSFET is specified at VGS = 10 V and needs a gate-driver stage if driven from 3.3 V or 5 V. Never choose by threshold voltage alone: look for the RDS(on) quoted at your own gate voltage.

PartTypeVDS maxID max (continuous)VGS(th)RDS(on) (at the VGS shown)Suits
IRLZ44NN, logic-level55 V47 A1 to 2 V25 mΩ at 5 V; 22 mΩ at 10 V5 V logic (Arduino Uno), motors and strips to a few amps
IRLB8721N, logic-level30 V62 A1.35 to 2.35 V16 mΩ at 4.5 V; 8.7 mΩ at 10 V3.3 V to 5 V logic, low-voltage supplies (3.3 V is at the edge: test it)
IRF540NN, standard100 V33 A2 to 4 V44 mΩ at 10 VNeeds a 10 V gate drive; not for a microcontroller pin
2N7000N, small signal60 V0.2 A0.8 to 3 Vabout 5 Ω at 10 VSmall loads on 5 V (relay at 50 mA); not for 3.3 V gates
BSS138N, small signal50 V0.22 A0.8 to 1.5 Vabout 3.5 Ω at 10 VSmall loads at 3.3 V; the level-shifter in section 6
IRF9540NP, standard−100 V−23 A−2 to −4 V117 mΩ at −10 VHigh-side switch on 12 V (section 4)

These are typical datasheet figures; always read the datasheet of the exact part you buy, since brands differ and the gate-voltage test condition is what matters. Parts for lower voltages (an SMD such as the AO3400A, with RDS(on) of about 50 mΩ quoted at 2.5 V) exist for 3.3 V boards.

Losses: conduction and heat

In the ON state the MOSFET is just a resistor: VDS = ID x RDS(on) P = ID^2 x RDS(on) Motor: 6 V, 0.25 A running, 1.2 A when stalled, IRLZ44N with 5 V on the gate (RDS(on) = 25 mohm): running: VDS = 0.25 x 0.025 = 6.25 mV P = 0.25^2 x 0.025 = 1.6 mW stalled: VDS = 1.2 x 0.025 = 30 mV P = 1.2^2 x 0.025 = 36 mW The motor gets within 6 mV of the full supply and the MOSFET stays cold. Compare the BJT: 0.2 V lost. The same motor through a standard-level part on 3.3 V (using the 1 ohm model estimate above): running: VDS = 0.25 x 1.03 = 0.26 V P = 64 mW stalled: VDS = 1.2 x 1.03 = 1.2 V P = 1.5 W Stalled, it would run hot, and on a cool-gate sample it might not turn on at all. Heat: temperature rise = P x thermal resistance. A TO-220 in free air is about 62 degrees C per watt. RDS(on) also rises with temperature (it roughly doubles from 25 C to 150 C), so check the hot value.

Gate resistor, pull-down and gate capacitance

The gate is a capacitor (Ciss, the input capacitance, a few nanofarads for a power MOSFET). To change state the pin must charge or discharge it, which causes a short pulse of current and takes a short time. Two resistors manage this:

pin o---[ Rg 220 ]---+---o G | [ Rp 10 k ] Rg: series GATE RESISTOR Rp: gate PULL-DOWN to the source | 0 V o---------------+---o S Rg limits the charging current. Without it the pin sees almost a short circuit for an instant: Peak gate current = V / Rg = 5 / 220 = 22.7 mA (inside a pin's 20 to 40 mA limits; an Uno pin's own internal resistance of about 25 ohm adds a little) With Rg = 0 the peak would be limited only by that internal resistance: about 200 mA, enough to damage the pin over time. Rg also damps ringing that a long wire's inductance would cause (Basic Electronics 4). Rp holds the gate at 0 V while the pin is not driving it (reset, boot, a cut wire), so the load cannot switch on by itself. A floating gate acts as an antenna; a hand near the wire can turn the MOSFET on. Rp must be large so it does not waste current or reduce the gate voltage (it forms a divider with Rg): Rp = 10 kohm: gate = 5 x 10000 / (10000 + 220) = 4.89 V current wasted 5 / 10220 = 0.49 mA (good) at 3.3 V: gate = 3.3 x 10000 / 10220 = 3.23 V Rp = 1 kohm: gate = 5 x 1000 / 1220 = 4.10 V current wasted 4.1 mA (too low: the gate loses 0.9 V)
Switching speed. The gate charges through Rg, like the RC circuit in Basic Electronics 4: IRLZ44N input capacitance Ciss about 3.3 nF, Rg = 220 ohm: tau = Rg x Ciss = 220 x 3.3e-9 = 0.73 microseconds 10 % to 90 % rise time = 2.2 x tau = 1.6 microseconds The datasheet's total gate charge gives a second estimate. IRLZ44N: Qg is about 48 nC at 5 V. A charging current of about half the starting value (11 mA on average) fills it in t = Q / I = 48e-9 / 0.0114 = about 4.2 microseconds. The real value is of this order, a few microseconds. Consequences for PWM (pulse width modulation, covered in Basic Electronics 10): at 20 kHz the period is 50 microseconds, so a 4 to 5 microsecond edge is under 10 % of it: fine. at 500 kHz the period is 2 microseconds: the gate would never reach its full voltage. Use a gate driver instead. The MOSFET dissipates most heat during the edges, when both VDS and ID are large. A rough upper estimate for the 6 V, 0.25 A motor at 20 kHz, edges of 5 microseconds each: P(switching) = 0.5 x V x I x (t_rise + t_fall) x f = 0.5 x 6 x 0.25 x (10e-6) x 20000 = 0.15 W That is about 100 times the conduction loss (1.6 mW). A higher PWM frequency or a larger Rg is not free: slower edges and a higher frequency both mean more heat.

The N-channel motor switch

+6 V o---------------------------+-------------+ | | ( M ) --- K D1 1N5819: cathode (band) to +6 V | /_\ A | | +-------------+ | D pin o--[ Rg 220 ]-----+----------G Q2 IRLZ44N (N-channel) | S [ Rp 10 k ] | | | 0 V o-----------------+----------+ Trace it: pin HIGH (5 V) charges the gate; current flows +6 V -> motor -> drain -> source -> 0 V. Pin LOW: the MOSFET opens. The motor's current (it is an inductor, Basic Electronics 4) cannot stop at once. It leaves the motor's lower end, passes up through D1 (anode to cathode) and back into the top end: the loop of Basic Electronics 5. Without D1: v = L di/dt. An illustrative 2 mH motor winding with 0.25 A falling to zero in 1 microsecond gives 2e-3 x 0.25 / 1e-6 = 500 V (far above the 55 V rating: the MOSFET avalanches and is damaged) With D1: the drain rises only to 6 + the diode drop (about 6.3 V).

4. Low-side versus high-side switching

You can put the switch on either side of the load. The load works in both cases, but the choice affects what is wired to what.

LOW-SIDE (switch between the load and 0 V): HIGH-SIDE (switch between +V and the load): +V o------+ +V o------+ | | [ LOAD ] [ SWITCH ] | | [ SWITCH ] [ LOAD ] | | 0 V o-----+ 0 V o-----+ Low-side: the control pin is referenced to 0 V, the same 0 V as the logic. An N-channel MOSFET or an NPN transistor does it directly with VGS = the pin's HIGH level. This is the easy and usual way. Disadvantage: when the switch is off the load's bottom terminal sits at +V, not 0 V. If the load is also wired to ground by anything else (a sensor's shield, another board), current can find another path and the load is "off" in name only. High-side: the load's bottom terminal is always at 0 V, which suits loads that must share a ground (sensors, boards with their own connector, a motor on a metal chassis). The difficulty is the control: P-channel MOSFET: source to +V. VGS = VG - VS must be negative. To turn OFF, the gate must reach +V (VGS = 0). A 3.3 V pin can only reach 3.3 V, which with a 12 V source leaves VGS = -8.7 V: STILL ON. So a P-channel high-side switch on 12 V needs a level shifter, a small NPN that pulls the gate down (section 6). N-channel MOSFET on the high side: its source is the load's top terminal, which rises towards +V when on. VGS = VG - VS needs about 5 V ABOVE the source, so the gate must reach +V + 5 V: higher than any supply you have. Done with a dedicated gate-driver chip with a charge pump (the high-side drivers inside motor-driver chips, Basic Electronics 9).
Low-sideHigh-side
DeviceNPN or N-channel MOSFETPNP or P-channel MOSFET (or an N-channel with a gate driver)
Drive from a 3.3 V or 5 V pinDirect (logic-level MOSFET) or with one resistorNeeds an inverting level shifter when +V is above the pin's level
Load groundFloats up to +V when offAlways at 0 V
Typical useMotors, relays, lamps, LED stripsPower rails that must be cut cleanly, reverse-polarity protection, loads with a fixed ground

5. Driving real loads

LoadBehaviour to plan forWorked checkSwitch and protection
Relay coil (12 V, 240 Ω)Inductive (L = 240 mH): needs the flyback diode of Basic Electronics 5; draws a steady 50 mASection 2, example 2: RB = 470 Ω from 3.3 V2N3904 (up to 200 mA) or a 2N7000 or BSS138 on a gate; 1N4001 across the coil
DC motor (6 V, 0.25 A run, 1.2 A stall)Inductive and generates back-EMF; the start-up and stall currents are 3 to 5 times the running current; size the switch for the stallSection 3: RDS(on) 25 mΩ loses 36 mW stalledLogic-level MOSFET; Schottky flyback diode (1N5819) across the motor
Filament lamp (6 V, 0.1 A)Cold tungsten has about one tenth of its hot resistance, so the inrush is about 10 times the running current for several tens of millisecondsHot R = 60 Ω, cold about 6 Ω: inrush up to 6 / 6 = 1 AThe inrush (1 A) is five times a 2N3904's 200 mA rating: use a MOSFET. No flyback diode needed (a lamp is not inductive).
LED strip (12 V, 2 m at 9.6 W/m)Resistive, with the resistors already built in; you only dim it with PWMI = 19.2 W / 12 V = 1.6 A. IRLZ44N: VDS = 1.6 × 0.025 = 40 mV, P = 64 mWLogic-level N-channel MOSFET; no flyback diode; at 3.3 V use a part with RDS(on) quoted at 2.5 V (AO3400A: 1.62 × 0.05 = 0.13 W)
Choosing the board for each job
Relay from 5 V: an Arduino Uno or Nano, pin current 20 mA, plenty for a BJT base resistor. Motor or LED strip from 3.3 V: an ESP32 or Raspberry Pi Pico driving a logic-level MOSFET, because a MOSFET gate needs almost no pin current, which suits boards with weak pins. A Raspberry Pi (the full computer) has 3.3 V pins, only a few milliamps each, and no protection if you overload one: always use a transistor between its pin and any load, a gate resistor on a MOSFET, and never connect 5 V back to a pin. Basic Electronics 11 compares the boards in full.

6. Level shifting: when the signal and the load disagree

Logic levels differ: a Pico and an ESP32 output 3.3 V, an Uno outputs 5 V, a motor supply may be 12 V. A signal at one level can be too small to drive a part that works at another (the PNP and P-channel examples above), or too large and destroy an input (5 V into a 3.3 V pin). Level shifting converts between them. Three simple ways:

1. A resistor divider (for going down, 5 V to 3.3 V)

5 V signal o---[ 1 k ]---+---o 3.3 V-side input | [ 2 k ] | 0 V o------------------+---o 0 V Vout = 5 x 2 / (1 + 2) = 3.33 V current = 5 / 3000 = 1.67 mA Speed: the divider's output resistance is 1 k || 2 k = 667 ohm. With 20 pF of pin and wire capacitance, tau = 667 x 20e-12 = 13 ns: fast enough for ordinary signals (up to about a megahertz). Works for one direction only, high to low, and the divider cannot raise a level.

2. A bidirectional MOSFET shifter (3.3 V and 5 V sides, such as I2C)

+3.3 V o-------+------------+ +5 V o-------+ | | | [ 10 k ] G [ 10 k ] | | | LV o----------+-----S [Q4 BSS138] D-----------------+--------o HV Q4's gate is tied to +3.3 V; its source is on the 3.3 V (LV) side, its drain on the 5 V (HV) side. Each side has its own pull-up. Both sides idle high: VGS = 3.3 - 3.3 = 0 V, Q4 is off, LV = 3.3 V, HV = 5 V (through the pull-ups). LV side pulled low: VGS = 3.3 V, Q4 turns on, and pulls the HV side low too. HV side pulled low: current first flows through Q4's body diode, taking LV to about 0.6 V; then VGS = 2.7 V, Q4 turns on and pulls LV to 0 V. A low on either side appears on the other. This is the standard breakout for I2C buses. For fast one-way signals use a buffer chip (a 74AHCT125 or the like: its logic-high threshold is low enough for 3.3 V when it is powered from 5 V). Note an Uno's input needs at least 0.6 x 5 V = 3.0 V, so 3.3 V into it usually works but with little margin.)

3. An NPN shifter (for going UP to a higher voltage, inverting)

A transistor does the job of the NPN switch in section 2 with the load replaced by a pull-up resistor to the higher supply. The collector node swings 0 V to the higher supply, and the signal is inverted (the pin HIGH gives a LOW at the collector). Stage C of the build uses this to drive a P-channel high-side switch on 12 V from a 3.3 V or 5 V pin.

7. BJT or MOSFET?

BJT (2N3904, BC337, TIP120)MOSFET (IRLZ44N, BSS138, IRF9540N)
ControlNeeds a base current: 1/10 of the load currentNeeds a gate voltage; no steady current
Load current rangeSmall: up to about 0.2 A (2N3904); 0.8 A (BC337); more with a DarlingtonMilliamps to dozens of amps
On-state lossFixed VCE(sat) of 0.2 V (about 1 V for a Darlington): power = VCE(sat) × IRDS(on) of milliohms: power = I2 RDS(on)
Works from a 3.3 V pinYes, if you can spare the base currentOnly if it is logic-level and quoted at 3.3 V or lower
Switching speedFast; storage time slows turn-off a littleFast, limited by gate charge; gate driver for MHz
Static damageRobustThe gate oxide dies above about ±20 V; static can kill it; keep Rp fitted
Cost, parts countCheapest, one resistorA little dearer; gate resistor and pull-down resistor
Best forSmall loads (LEDs, relays, buzzers) of up to 100 to 200 mA; level shiftersMotors, strips, lamps, anything over about 0.3 A; PWM; 3.3 V logic; high efficiency; battery-powered builds
Rule of thumb
Under about 100 mA, use a BJT: one resistor and a 5 cent part. Above about 300 mA, or with PWM, use a logic-level MOSFET. In between, check how much heat each design makes: P = VCE(sat) × I for the BJT or I2 RDS(on) for the MOSFET.

8. Build it: switch a load from a weak signal

Three stages, each switching a load that a pin cannot drive directly. A is the BJT; B is the project of the chapter, a motor switched by a logic-level MOSFET; C is a high-side switch on 12 V from a 3.3 V or 5 V signal, with a level shifter (simulator first, real parts optional).

Two supplies, one ground
Use a separate supply for the load (4 AA cells for 6 V, a 12 V adapter for stage C) and a 5 V logic supply (a USB breakout or the 5 V pin of an Arduino). Join all the 0 V rails with a jumper wire. Connect each supply last, and disconnect the battery before changing the wiring. A stalled motor or a short through a MOSFET can draw amps: never run a motor from the USB port of a computer.

Parts list

PartValueUsed in
Q12N3904 NPN (TO-92)A, C
Q2IRLZ44N logic-level N-MOSFET (TO-220)B
Q3IRF9540N P-MOSFET (TO-220)C
R1820 Ω, 1/4 W (grey-red-brown)A
R2120 Ω, 1/2 W (brown-red-brown), the loadA
R3, Rp, R6, R810 kΩ, 1/4 W (brown-black-orange)A (R3), B (Rp), C (R6, R8)
Rg220 Ω, 1/4 W (red-red-brown)B
R74.7 kΩ (yellow-violet-red) for 5 V drive; 2.2 kΩ (red-red-red) for 3.3 VC
R9470 Ω, 1/2 W (yellow-violet-brown), the loadC
D11N5819 Schottky (or a 1N4001)B
MotorSmall 6 V hobby DC motor (about 0.25 A running, about 1.2 A stalled)B
Supplies5 V logic supply; 6 V (4 AA cells) for A and B; 12 V adapter for CA to C
Breadboard, jumper wires, multimeterA to C

Stage A: the BJT switch (6 V, 50 mA load)

Use the top rails for the 6 V supply (top + is 6 V) and the bottom rails for the 5 V logic supply. Join the two 0 V rails. Hole list (one column per connection): Q1 2N3904, flat face towards you, legs down: E column 10 row a, B column 11 row a, C column 12 row a (check the datasheet or the meter's diode test for your brand; some 2N3904s have a different pin order) R1: column 5 row a -> column 11 row b (820 ohm: signal to base) R3: column 11 row c -> bottom - rail (10 k: base to 0 V) Emitter: column 10 row b -> bottom - rail R2: column 12 row b -> column 16 row b (120 ohm load) Jumper: column 16 row c -> top + rail (6 V) Signal jumper: column 5 row b -> bottom + rail (5 V = ON) or bottom - rail (0 V = OFF) Join: top - rail -> bottom - rail (common 0 V) Connect the supplies LAST. Measure ON: Vce: red probe column 12 row e, black on the - rail Vload: red column 16 row e, black column 12 row e (load current = Vload / 120) Vbe: red column 11 row e, black column 10 row e Base current: Ib = (V across R1) / 820, measured red column 5 row e, black column 11 row e
  1. With the signal on 0 V (OFF): Vload should be 0 V and VCE should be 6 V.
  2. Move the jumper to 5 V (ON): VCE falls to about 0.1 to 0.3 V; the load has about 5.8 V across it (48 mA).
  3. Measure VR1 (about 4.2 V); calculate IB and the forced beta.
  4. Try the half-on case: replace R1 with 22 kΩ. VCE will be somewhere around 2 to 5 V and will change if you warm the transistor with your finger: that is the current-gain dependence in action.

Stage B: the MOSFET motor switch (the project of the chapter)

Hole list. Q2 IRLZ44N, printed face towards you, legs down: G column 10 row a, D column 11 row a, S column 12 row a. Rg: column 5 row a -> column 10 row b (220 ohm: signal to gate) Rp: column 10 row c -> column 12 row c (10 k: gate to source) Source: column 12 row b -> bottom - rail D1 1N5819: anode column 11 row b, cathode (band) column 15 row b Jumper: column 15 row c -> top + rail (6 V) Motor: one lead to column 11 row c, the other lead to column 15 row d Signal jumper: column 5 row b -> bottom + rail (5 V = ON) or bottom - rail (0 V = OFF) (or a microcontroller pin) Join: top - rail -> bottom - rail Connect the supplies LAST. Measure: gate: red probe column 10 row e, black on the - rail Vds: red column 11 row e, black column 12 row e (use the 200 mV range) motor volts: red column 15 row e, black column 11 row e
  1. Jumper to 0 V: gate 0 V, motor off, VDS 6 V.
  2. Jumper to 5 V: gate about 4.9 V; the motor runs; VDS only a few millivolts; motor volts close to 6 V.
  3. Hold the motor shaft: VDS rises to about 30 mV as the current approaches 1.2 A; the MOSFET stays cold. Do not hold for more than a few seconds.
  4. Take the signal jumper out completely (floating signal): the motor stays off because Rp holds the gate low. Then try removing Rp as well (battery disconnected while you do it) and reconnect the battery: a floating gate may switch on by itself; do not rely on it.
  5. If you have a microcontroller board, use analogWrite or PWM on the pin to vary the speed; Basic Electronics 10 covers this.

Stage C: high-side P-channel switch on 12 V (simulator first)

+12 V o--------+-----------+ | | [ R6 10 k ] S | | +-----------G Q3 IRF9540N (P-channel) | | | D | | | [ R9 470 ] load +---C | | | pin o--[ R7 ]+-----B | Q1 2N3904 | | | [R8 10k] E | | | | 0 V o--------+-----+-------+ R7 = 4.7 k at a 5 V pin; R7 = 2.2 k at a 3.3 V pin. R8 = 10 k from Q1 base to 0 V. The node joining R6's lower end, Q3's gate and Q1's collector is one node. R8 runs from Q1's base to 0 V. Q1's emitter is at 0 V. Pin LOW: Q1 is off. R6 pulls the gate up to +12 V: VGS = 0, Q3 is OFF, the load sees 0 V. Pin HIGH: Q1 saturates and pulls the gate down to about 0.1 V. VGS = 0.1 - 12 = -11.9 V, Q3 is ON. Numbers (5 V pin, R7 = 4.7 k): gate pull-down current through R6 = (12 - 0.1) / 10000 = 1.19 mA (Q1's collector current) base current = (5 - 0.7)/4700 - 0.7/10000 = 0.92 - 0.07 = 0.85 mA (forced beta = 1.19/0.85 = 1.4: deeply saturated; the minimum needed for forced beta 10 would be only 0.12 mA) at 3.3 V with R7 = 2.2 k: (3.3 - 0.7)/2200 - 0.07 = 1.11 mA (works equally well) load: RDS(on) = 0.117 ohm, so Vload = 12 - 0.0255 x 0.117 = 12.0 V, Iload = 12 / 470 = 25.5 mA gate-source limit +/-20 V: fine on 12 V. On a 24 V supply the gate would see -24 V and be destroyed (Challenge 3). Turn-OFF speed: Ciss of about 1.3 nF recharges through R6: tau = 10000 x 1.3e-9 = 13 microseconds, 10-90 % in 29 microseconds. Turn-on is fast (Q1 sinks the current). So this circuit suits switching and PWM below about 2 kHz.
Hole list for a real build (12 V adapter; the 5 V or 3.3 V signal on the bottom rails; join all 0 V): Q3 IRF9540N, printed face towards you, legs down: G column 10 row a, D column 11 row a, S column 12 row a Source: column 12 row b -> top + rail (12 V) R6: column 10 row b -> column 12 row c (10 k: gate to source) R9: column 11 row b -> column 15 row b (470 ohm load) Jumper: column 15 row c -> top - rail Q1 2N3904, flat face towards you: E column 20 row a, B column 21 row a, C column 22 row a Jumper: column 22 row b -> column 10 row c (Q1 collector to Q3 gate) Emitter: column 20 row b -> top - rail R7: column 25 row a -> column 21 row b (4.7 k at 5 V, 2.2 k at 3.3 V) R8: column 21 row c -> top - rail (10 k: base to 0 V) Signal jumper: column 25 row b -> bottom + rail (signal HIGH) or bottom - rail (LOW) Join: top - rail -> bottom - rail Measure: gate: red probe column 10 row d, black on the - rail load: red column 11 row d, black on the - rail

Simulators

Falstad Circuit Simulator (falstad.com/circuit): build each circuit from the Draw menu (Transistors: NPN, N-MOSFET, P-MOSFET; Passive parts; Diodes; Inputs and Sources: DC voltage sources and a logic input to flip the signal). The default NPN has a gain of 100 and a VBE near 0.7 V. The default MOSFET has a threshold voltage of about 1.5 V, which suits a logic-level part: right-click it, choose Edit, raise the threshold to 3 V, and watch a 3.3 V gate fail to turn it on, which shows why standard-level parts need more gate voltage. A motor is not a stock part: use a series resistor and inductor (for example 5 Ω and 2 mH) and see the voltage spike at the drain with and without D1. The simulator's meters on each node read the values in the table below, within its own transistor models (a few tenths of a volt different).

Tinkercad Circuits (tinkercad.com): the component list contains an NPN transistor, a power MOSFET, a DC motor, a multimeter, a battery, an Arduino Uno and an oscilloscope. Use the Uno's pin 9 as the weak signal for stage B (pin HIGH is 5 V; put 220 Ω between pin and gate), add the multimeter in voltage mode to the nodes in the hole lists, and check each transistor's part number in its property box (model names and values differ from real parts, so compare to the table's ranges). Tinkercad's Uno also lets you run the PWM sketch from Basic Electronics 10 against the same wiring.

Expected readings

MeasurementCalculatedReal (typical)Simulator
A OFF: VCE / Vload6.0 V / 0 V6.0 V / 0.00 V6 V / 0 V
A ON: VBE0.8 V0.72 to 0.85 V0.7 to 0.8 V
A ON: VCE0.2 V0.1 to 0.3 V0.05 to 0.3 V
A ON: Vload / current5.8 V / 48 mA5.7 to 5.9 V / 47 to 49 mA5.8 V / 48 mA
A ON: VR1 / base current4.2 V / 5.1 mA4.1 to 4.3 V / 5.0 to 5.2 mA4.2 V / 5.1 mA
A with R1 = 22 kΩ: VCE3.65 V (if hFE = 100)1.5 to 5.5 V, depends on the sample3.6 V (default gain 100)
B OFF: gate / VDS0 V / 6.0 V0.00 V / 6.0 V0 V / 6 V
B ON: gate voltage4.89 V4.8 to 4.95 V4.9 V
B ON (running): VDS6 mV5 to 20 mVa few mV (depends on the model)
B ON (running): motor volts5.99 V5.9 to 6.0 V (battery voltage sags under load)6.0 V
B stalled: VDS / current30 mV / 1.2 A20 to 60 mV (depends on the motor)(motor model dependent)
B turn-off drain spike, 1N5819 / noneabout 6.3 V / above the 55 V rating(scope needed) about 6.3 V / tens of voltsabout 6.3 V / large (hundreds of volts)
C signal LOW: gate / Vload12 V / 0 V12.0 V / 0.00 V12 V / 0 V
C signal HIGH: gate / VGS0.1 V / −11.9 V0.05 to 0.2 V / about −11.9 V0.1 V / −11.9 V
C signal HIGH: Vload / current12.0 V / 25.5 mA11.9 to 12.0 V / 25 mA12.0 V / 25.5 mA
C turn-off 10 to 90 % time29 µs(scope needed) about 25 to 45 µsdepends on the MOSFET model's capacitance

Real readings spread because transistors vary (hFE by 2:1 or more, VBE by tenths of a volt, RDS(on) by 30 % or more) and with temperature and the battery's state. The things that do not vary are the relationships: a saturated BJT drops about 0.2 V whatever the load; a logic-level MOSFET at full gate voltage drops I × RDS(on), millivolts at these currents; a floating gate is held off by its pull-down; and the base or gate resistor sets the control current, not the load current.

9. Challenges

Challenge 1: BJT base resistors
(a) A 2N3904 switches a 12 V relay coil of 400 Ω (flyback diode fitted) from a 5 V pin. Using VCE(sat) = 0.2 V, VBE = 0.8 V and a forced beta of 10, find the saturation current, the base current, the base resistor (calculated, then the E12 value below), the actual base current and forced beta, and the power in the transistor. Then do the same from a 3.3 V pin that can supply no more than 2.5 mA, and state the forced beta you end up with and whether the minimum hFE of 60 still leaves a margin. (b) The designer uses 47 kΩ by mistake (VBE = 0.7 V). Find the base current and the collector voltage for a transistor with hFE = 100 and with hFE = 300, and say what each does to the relay. (c) A 9 V motor draws 500 mA and is driven from a 5 V pin through a TIP120 Darlington (VBE total 1.4 V, VCE(sat) 0.9 V, forced beta 250): find the base current, the base resistor (round to E12 down), the motor voltage and the transistor's power, and compare with a MOSFET of RDS(on) = 25 mΩ.
View solution
Challenge 2: MOSFET losses and the gate
(a) A 12 V motor runs at 1 A and stalls at 3 A. An IRLZ44N (RDS(on) 25 mΩ at 5 V, doubling when hot to 50 mΩ) switches it. Find VDS and the power running and stalled with the hot value, the temperature rise when stalled in free air at 62 °C/W, and compare the stalled power with a BJT-style VCE(sat) of 0.9 V at 3 A. (b) The gate is driven from a 5 V pin through Rg = 220 Ω with Ciss = 3.3 nF: find the peak gate current, τ, and the 10 to 90 % rise time; then for Rg = 1 kΩ find the same values and say what that does to a 20 kHz PWM edge. (c) A 10 kΩ pull-down is fitted: find the gate voltage and the wasted current with a 5 V pin and Rg = 220 Ω; then with a 3.3 V pin and a 100 kΩ pull-down (Rg still 220 Ω), and say which is the better compromise for a floating pin. (d) An LED strip of 24 W on 12 V is switched by an AO3400A with RDS(on) of 50 mΩ from a 3.3 V pin: find the current, VDS and the power in the MOSFET.
View solution
Challenge 3: High-side switching and level shifting
(a) A 5 V signal must drive a 3.3 V input through a divider using a 1 kΩ series resistor and a 2 kΩ to ground: find the output voltage, the divider current, and the time constant with 20 pF of load; then find what happens if the pull-down is replaced by 4.7 kΩ. (b) A P-channel IRF9540N switches a 470 Ω load on a 24 V supply from a 3.3 V pin using the section 6 NPN shifter. With the gate pull-up R6 = 10 kΩ alone, find VGS when Q1 is on and say why the part is destroyed; then add a second 10 kΩ resistor in series between the gate and Q1's collector and find the new VGS (VCE(sat) = 0.1 V), Q1's collector current and VCE when off, whether a 2N3904 (VCEO 40 V) copes, and the base resistor for a 1 mA base current from 3.3 V (VBE = 0.7 V, E12 down, with no base pull-down). Find also the 10 to 90 % turn-off time with Ciss = 1.3 nF. (c) Choose BJT or MOSFET and a board for each job and give a one-line reason: a 12 V buzzer at 30 mA from an Uno; a 12 V, 2 A pump pulsed at 1 kHz from an ESP32; a 5 V relay at 70 mA from a Pico; a 6 V, 100 mA filament lamp from an Uno.
View solution

Quick reference

  • BJT switch: IC(sat) = (Vs − VCE(sat))/RL; IB = IC(sat)/10 (forced beta 10); RB = (Vdrive − 0.8)/IB, round down; VCE(sat) about 0.2 V; power = VCE(sat) × IC
  • Active region: IC = hFE IB; use the datasheet minimum hFE at your current; the half-on transistor dissipates up to Vs2/(4RL)
  • Darlington: gains multiply, VBE about 1.4 V, VCE(sat) about 0.9 V or more; forced beta about 250
  • MOSFET switch: VDS = I × RDS(on), P = I2 RDS(on); read RDS(on) at your gate voltage, not the threshold; logic-level for 3.3 V and 5 V pins
  • Gate network: Rg (100 to 470 Ω) limits the charging pulse (V/Rg) and sets the speed (τ = Rg Ciss, 10 to 90 % = 2.2τ); Rp = 10 kΩ gate to source keeps it off at boot
  • Low-side: NPN or N-MOSFET between load and 0 V, the easy way; high-side: PNP or P-MOSFET (source to +V); P-channel on 12 V from a 3.3 V pin needs an NPN shifter that pulls the gate down; keep |VGS| under 20 V
  • Loads: relay and motor need a flyback diode across the load; a lamp has about 10× inrush; a motor stalls at 3 to 5× its running current; size for the worst case
  • Level shift: divider (down only), BSS138 shifter with a pull-up on each side (two-way, I2C), NPN with a pull-up to the higher supply (up, inverting)
  • Choice: BJT under about 100 mA and for small signal jobs; logic-level MOSFET over about 300 mA, for PWM, or for 3.3 V logic

Next, Basic Electronics 7 uses transistors in the half-on region on purpose: amplifiers, then the op-amp as comparator, buffer and amplifier, and conditioning the signal from a sensor so a microcontroller can read it.