Volume 05 Beginner 5 sub-modules ~15 min read

Transistors as Switches

A microcontroller pin gives a few milliamps at 3.3 V; a motor wants an amp at 12 V. A transistor bridges the gap. This volume switches a lamp with an NPN transistor and then a MOSFET, shows why a switch must be fully on, tames the voltage spike from a relay coil with one diode, and stops inputs floating with pull-up and pull-down resistors.

You will learn
  • Why loads need a transistor between them and a microcontroller pin
  • How an NPN transistor switches, and how to choose its base resistor so it is fully on
  • How a MOSFET switches with no steady gate current, and why logic-level MOSFETs matter
  • Why switching off a relay or motor makes a spike, and how a flyback diode stops it
  • What a floating input is, and how pull-up and pull-down resistors fix it
You need
  • Volume 04: diodes, and the 0.7 V they take.

5.1 Why switch with a transistor

A transistor is a switch with no moving parts. A small current or voltage on one lead turns a much bigger current on or off through the other two. That lets a tiny control signal run lamps, motors and relays.

A pin is not a power supply

A microcontroller controls things through its pins. Each pin can switch between 0 V and its supply voltage, often 3.3 V. But it can only supply a little current, usually about 10 mA to 20 mA before it is damaged. Many loads want far more, often at a higher voltage:

Load Typical need
A small LED 10 mA at 2 V
A 12 V lamp 100 mA at 12 V
A relay coil 30 mA to 100 mA at 5 V or 12 V
A small motor 200 mA to 1 A at 6 V or 12 V

A mechanical switch could handle those currents, but a program cannot press it. A relay could, but it is slow, and it wears out. A transistor switches in a millionth of a second, uses little power to control, and never wears out.

Three leads

Every transistor has three leads. Two of them carry the big current, and the third controls it. There are two main families:

  1. The bipolar transistor. A small current into its control lead lets a current about a hundred times bigger flow. Module 2.
  2. The MOSFET. A voltage on its control lead switches it on, and it draws no steady current at all. Module 3.
Quick check

Why not drive a 12 V, 100 mA lamp straight from a 3.3 V microcontroller pin?

Show the answer

Answer: D. The lamp wants 12 V and 100 mA. A pin gives at most 3.3 V and a few tens of milliamps, so a transistor has to do the heavy lifting.

5.2 The bipolar transistor as a switch

In an NPN bipolar transistor, current into the base lets a bigger current flow from collector to emitter - up to about a hundred times bigger. Give it enough base current, and it turns fully on, with only about 0.2 V across it.

Collector, base and emitter

The three leads are the collector, the base and the emitter. The load goes between the supply and the collector, and the emitter goes to ground. The base is the control. From base to emitter, the transistor behaves like a diode, so the base sits about 0.7 V above the emitter while current flows into it.

An NPN transistor switching a 12 V lamp from a 3.3 V control pin + + B1 12 V L1 120 Ω RB 1 kΩ B2 3.3 V Q1
Figure 5.1 - B2 stands for a microcontroller pin at 3.3 V. A small current flows from it through RB into Q1's base. That lets a far bigger current flow from the 12 V supply through the lamp, into Q1's collector and out of its emitter.

circuit npn-switch: pin at 3.3 V: base current 2.6 mA; lamp current 98.3 mA; Q1 is fully on with 200 mV across it
pin at 0 V: base current 0 A; lamp current 0 A; Q1 is off with 12 V across it

RB sets the base current, by Ohm's law:


base current = (3.3 V - 0.7 V) / 1 kΩ = 2.6 mA

A 2.6 mA control current switches 98.3 mA at 12 V. The pin never meets the 12 V at all.

Gain, and fully on

The transistor multiplies its base current by its current gain. It is about 100 for a small transistor, though it varies a lot from one part to the next. As the base current grows, the lamp current grows a hundred times faster - until the lamp has the whole supply across it. Then no more can flow. The transistor is fully on, or saturated:

Lamp current against base current in the NPN switch 0 0.25 0.5 0.75 1 1.25 1.5 1.75 2 0 20 40 60 80 100 120 base current (mA) lamp current (mA)
Figure 5.2 - Up to about 1 mA of base current, the lamp current is 100 times the base current. Beyond that, the lamp already has the whole supply across it, and more base current changes nothing: the transistor is fully on.

fully on needs a base current of at least 98.3 mA / 100 = 983 µA

Designers give the base two to five times the least it needs, because the gain varies. Here 2.6 mA is plenty.

Why a switch must be fully on

A fully-on transistor has only 0.2 V across it, so it wastes very little:


Q1 turns 0.2 V x 98.3 mA = 19.7 mW into heat

With too little base current, it is only part on. Then it has a large voltage across it and a large current through it at the same time - and that makes heat:


with RB = 10 kΩ: base current 260 µA; lamp current 26 mA; Q1 is only part on, with 8.88 V across it, and turns 231 mW into heat
with RB = 100 kΩ: base current 26 µA; lamp current 2.6 mA; Q1 is only part on, with 11.7 V across it, and turns 30.4 mW into heat
Common mistake

Choosing the base resistor from the gain alone, with no margin. A transistor whose gain is lower than expected ends up only part on: the load is dim, and the transistor runs hot. Give the base at least twice what the load current divided by the gain needs.

Quick check

An NPN switch must carry 200 mA, and its gain is about 100. What is the least base current it needs?

Show the answer

Answer: B. The collector current is at most the gain times the base current, so the base needs at least 200 mA / 100 = 2 mA. In practice, give it two to five times that.

5.3 The MOSFET as a switch

A MOSFET is switched by a voltage, not a current. Raise its gate above its threshold voltage and it conducts with a tiny resistance; the gate itself takes no steady current.

Gate, drain and source

An N-channel MOSFET has a gate, a drain and a source. The load goes between the supply and the drain, and the source goes to ground. The gate is a metal plate insulated from the rest - a small capacitor, as in Volume 03. Once it is charged, no more current flows into it.

When the gate is more than the threshold voltage above the source, the drain-to-source path conducts, with a small on-resistance. This MOSFET's threshold is 2 V and its on-resistance 0.05 Ω:

An N-channel MOSFET switching the same lamp + + B1 12 V L1 120 Ω RG 100 Ω R2 100 kΩ B2 3.3 V M1
Figure 5.3 - The pin drives M1's gate through RG. The gate draws no steady current, so the gate sits at almost the pin's voltage. R2 holds the gate at 0 V whenever the pin is not driving it.

circuit nmos-switch: pin at 3.3 V: the gate sits at 3.3 V; lamp current 100 mA; M1 has 5 mV across it
M1 turns 500 µW into heat
pin at 0 V: lamp current 0 A; pin not driving at all: R2 holds the gate at 0 V, lamp current 0 A

Five millivolts across it, where the bipolar transistor had 200 mV. That difference grows with the current:


at 2 A: a MOSFET of 0.05 Ω turns 200 mW into heat; a transistor at 0.2 V turns 400 mW

That is why MOSFETs switch most heavy loads today.

The two resistors on the gate

RG, the gate resistor, limits the brief current that flows each time the pin charges or empties the gate's capacitance. R2 holds the gate at 0 V when nothing drives it. For a moment after power-up, a microcontroller's pins are not driving yet. Without R2, the gate would float, and the load might switch on by itself. Module 5 has more on resistors like R2.

Logic-level MOSFETs

Many MOSFETs need 10 V on the gate to turn fully on. A 3.3 V pin needs a logic-level MOSFET, whose datasheet promises a low on-resistance at 3.3 V or less. Volume 08 shows where to find that number.

Low side and high side

Both switches here sit between the load and ground: a low-side switch. To switch the supply side of a load instead, designers use a P-channel MOSFET, which turns on when its gate is pulled below its source. Volume 06 uses both kinds.

Quick check

What current flows into a MOSFET's gate while it stays switched on?

Show the answer

Answer: A. The gate is insulated from the channel, like a capacitor's plate. Current flows only briefly, to charge or empty it. A MOSFET held on or off draws no steady gate current.

5.4 Motors, relays and the flyback diode

Motors and relays are coils, and a coil switched off makes a huge voltage spike. A flyback diode across the coil gives its current somewhere to go, so the spike never happens.

Relays

A relay is a switch worked by an electromagnet. Current through its coil pulls a metal contact across, closing a separate circuit. The two circuits never touch, so a transistor on a safe, low voltage can switch a completely separate load.

A transistor driving a relay coil, with a flyback diode + + B1 12 V RC 120 Ω L1 50 mH D1 RB 1 kΩ B2 3.3 V Q1
Figure 5.4 - The relay's coil is drawn as its inductance L1 with its wire's resistance RC. D1 sits across the coil, pointing up towards the supply, so it never conducts while Q1 is on.

circuit relay: Q1 on: the coil carries 98.3 mA; D1 is off

With Q1 on, D1 is backwards: its cathode is at 12 V and its anode near 0 V. It does nothing.

The moment of switching off

Volume 03 showed that an inductor's current cannot stop instantly. When Q1 turns off, the coil keeps pushing its 98.3 mA out of its lower end. D1 is now the only way round, so the current flows up through D1 and back into the top of the coil:


the instant Q1 turns off: D1 carries the coil's 98.3 mA, and the collector rises only to 12.7 V
the current then dies away with a time constant of L / R = 50 mH / 120 Ω = 417 µs

The collector rises just 0.7 V above the supply, which Q1 can take easily. Without the diode, the story is very different:


without D1: no current path at all - the coil forces the voltage as high as it takes
stopping 98.3 mA in 1 µs: V = 50 mH x 98.3 mA / 1 µs = 4.92 kV

Nearly five thousand volts would destroy Q1 at once. Motors are coils too, and need the same diode.

Common mistake

Fitting the flyback diode the wrong way round. Pointing down, it would conduct straight from the supply through Q1 as soon as Q1 turned on: a short circuit. The diode's cathode, the striped end, always goes to the supply side of the coil.

Quick check

While Q1 is on and the relay is energised, what does the flyback diode do?

Show the answer

Answer: C. While Q1 is on, the diode's cathode is at the supply voltage and its anode near 0 V, so it is backwards and blocks. It only conducts at switch-off, to carry the coil's current round.

5.5 Pull-up and pull-down resistors

An input connected to nothing floats, and reads at random. A pull-up or pull-down resistor gives it a definite level whenever nothing else is driving it.

The floating input

A chip's input takes almost no current, so it follows whatever voltage it is connected to. Connected to nothing, it has no voltage of its own. It drifts, picks up noise from nearby wires, and can read 0 or 1 at random. That is a floating input.

A button alone does not fix this. When it is pressed it connects the input to something, but when it is released the input floats again.

The pull-up resistor

A push button with a pull-up resistor + B1 3.3 V R1 10 kΩ S1 to the chip
Figure 5.5 - R1 pulls the input up to 3.3 V while the button is open. Pressing S1 connects the input straight to ground.

circuit pull-up: button open: the input is at 3.3 V and 0 A flows; pressed: 0 V and 330 µA flows

With the button open, no current flows through R1, so there is no voltage across it, and the input sits at 3.3 V. Pressing the button joins the input to ground. R1 then limits the current that flows, and the input reads 0. Note the logic is turned round: pressed reads 0.

A pull-down resistor is the mirror image: a resistor to ground, and a button to the supply. Then pressed reads 1.

Choosing the value


a 1 kΩ pull-up wastes 3.3 mA while the button is held
a 10 kΩ pull-up wastes 330 µA while the button is held
a 100 kΩ pull-up wastes 33 µA while the button is held

A small resistor wastes current while the button is held. A very large one pulls only weakly, so noise and leakage can overcome it. Around 10 kΩ is the usual compromise. Many microcontrollers have pull-up resistors built in, which a program can switch on.

Pull-ups appear all over digital electronics. The gate resistor R2 in Module 3 is a pull-down. Shared wires such as I²C use open-drain outputs with a pull-up on each line.

Quick check

An input has a 10 kΩ pull-up to 3.3 V and a button to ground. What does it read while the button is not pressed?

Show the answer

Answer: B. No current flows through the pull-up while the button is open, so there is no voltage across it. The input sits at the full 3.3 V.

What you learned

Key words from this volume

Every word below has a plain-English entry in the glossary.

Practice

Practice 1

A base resistor

A 5 V pin drives an NPN transistor switching a 100 Ω load on 5 V. Is a 1.5 kΩ base resistor enough to turn it fully on?

Show the solution

5 V, 100 Ω load, RB = 1.5 kΩ: base current 2.87 mA; load current 48 mA; Q1 fully on
fully on needs at least 48 mA / 100 = 480 µA of base current

Yes: 2.87 mA is about six times the 480 µA it needs.

Practice 2

A pull-up's cost

A 4.7 kΩ pull-up to 3.3 V feeds a button to ground. How much current flows while the button is held?

Show the solution

a 4.7 kΩ pull-up on 3.3 V wastes 702 µA while the button is held
Practice 3

A different relay

The relay circuit is fitted with a relay whose coil has 400 Ω of resistance. What current does it take, and what does the flyback diode carry at the moment Q1 turns off?

Show the solution

a 400 Ω relay coil on 12 V with Q1 on: 29.5 mA

At switch-off, the coil's current cannot change instantly, so the diode takes the same 29.5 mA.

Interview corner

Interview question 1

The flyback diode

"Why do you put a diode across a relay coil, and which way round does it go?"

Show the solution

"A relay coil is an inductor, so its current can't stop instantly. When the transistor switches off, the coil drives whatever voltage it takes to keep its current flowing - thousands of volts, which would destroy the transistor. A diode across the coil gives that current a path back round through the coil. Then the voltage only rises about 0.7 V above the supply, and the current dies away in the coil's resistance. It goes with its cathode to the supply side, so it's backwards while the transistor is on and does nothing until switch-off."

Volume 06 builds logic gates from MOSFETs: the CMOS inverter, NAND and NOR gates, inside every chip.