Transistor as a switch
On this page
Your hand turns a small valve handle, and a large flow of water starts or stops. A microcontroller pin can give only a few milliamps (about 8 mA on an STM32F103, 20 mA on an ATmega328P), which is far too little for a relay or a motor. A transistor used as a switch is that valve: a small current from the pin controls a large current through the load.
See it
A 3.3 V pin drives a transistor through a base resistor. The transistor has a current gain of 75, and its relay needs 70 mA. Someone fits a 10,000 Ω base resistor to protect the pin. What happens?
The thin line into the base is the small current from the pin. The thick line through the relay is the large current it controls. The bar shows where the 5 V goes: a transistor that is fully on keeps only 0.2 V for itself. Make the base resistor too large and the transistor keeps much more, which the relay loses.
The idea
A common transistor has three legs: the base, the collector and the emitter. A small current into the base lets a much larger current flow from collector to emitter. The current gain, written β, is how many times larger: at least 75 for the 2N2222.
A switch must be fully off or fully on. With the pin at 0 V no base current flows, and the transistor is off. To turn it fully on (engineers say saturated), give the base 3 to 10 times the load current divided by β. Then only about 0.2 V stays across the transistor. The base resistor sets the base current:
RB is the base resistor, VGPIO the pin voltage, VBE the 0.7 V the base keeps, IB the base current you want
This is the bipolar transistor. The other family, the MOSFET, is turned on by a voltage and takes almost no current from the pin. The rule is the same: fully on or fully off.
One worked number
A relay coil draws 70 mA from 5 V. A 2N2222 transistor (β at least 75) switches it from a 3.3 V pin. What base resistor turns the transistor fully on?
Show the worked solution, 3 steps
- Find the smallest base current that could work
IB = IC ÷ β = 70 mA ÷ 75 = 0.93 mA.
- Add a margin
About five times the minimum makes sure the transistor is fully on: use 5 mA.
- Find the resistor
RB = (3.3 V − 0.7 V) ÷ 0.005 A = 2.6 V ÷ 0.005 A = 520 Ω. Buy 470 Ω or 560 Ω. The figure opens at 520 Ω: the relay has its 70 mA.
The mistake to avoid
A second trap: a relay or motor is a coil, and a coil makes a high voltage when its current stops. A diode across the coil protects the transistor.
Check yourself
An LED strip draws 200 mA. The transistor has a minimum gain of 100 and the pin gives 5 V. You want five times the minimum base current. Which base resistor?
A small motor runs slowly from its transistor, and the transistor is too hot to touch. The motor runs at full speed when connected straight to the supply. What is the most likely cause?
A datasheet gives the current gain of a transistor as 75 minimum and 300 typical. Which value do you use to choose the base resistor?
Where you meet it
- Relays and valvesThe controller of a pump, a cold room or an irrigation valve switches each coil through a transistor.
- Motors and fansA MOSFET carries 0.5 A to a 12 V fan and wastes about 10 mW, so it needs no heat sink.
- LightingLED strips take several amperes. A pin switches them through a MOSFET, often with PWM to dim them.
- A small base current controls a large load current. The pin never carries the load.
- A switch is fully off or fully on. Partly on means heat in the transistor and a weak load.
- Minimum base current = load current ÷ minimum gain. Give 3 to 10 times that.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.