The output pin of a microcontroller or logic IC supplies so little current that even lighting one or two LEDs strains it. In that situation, the part that switches a large current on and off with a small signal is the transistor. This article covers how to use the most common bipolar junction transistor (BJT) as a switch, how to calculate the base resistor, and how to check the pinout, which differs between parts.
The three terminals of a transistor
A BJT has three terminals: emitter (E), base (B), and collector (C). A small current in the base controls a large current from the collector to the emitter.
- NPN: when current is put into the base (High), current flows from collector to emitter. Put the emitter on ground (GND).
- PNP: when current is drawn out of the base (Low), current flows from emitter to collector. Put the emitter on +V.
As a switch you use only the fully-on saturated state and the fully-off cut-off state. When saturated, the collector–emitter voltage falls to about 0.1–0.3 V, and it behaves almost like a closed switch.
The pinout differs between parts
The biggest cause of accidents is getting the pinout wrong. The 2N3904 and 2N3906 are E-B-C, the BC547 and BC337 are C-B-E, and some Japanese parts (such as the 2SC1815) are E-C-B. Always check the pinout in the datasheet (see How to Read a Datasheet). In Patterndo you can pick the NPN or PNP polarity and the role of each pin (E, B, C) for each transistor, so you can lay it out to match the pinout of the real part.
The NPN switch circuit
This arrangement puts the load between +V and the collector and connects the emitter to ground. When the input is High, current flows into the base and the load turns on.
Calculating the base resistor
Set the base current to about 1/10 of the collector current to guarantee saturation (forced beta of 10). Subtract the base–emitter voltage (about 0.7 V) from the input voltage and divide by the base current.
Ib = Ic / 10 R_B = (input voltage - 0.7V) / Ib
| Collector current | Base current | R_B (5 V input) | R_B (3.3 V input) |
|---|---|---|---|
| 10 mA | 1 mA | 4.3 kΩ → 4.7 kΩ | 2.6 kΩ → 2.2 kΩ |
| 50 mA | 5 mA | 860 Ω → 820 Ω | 520 Ω → 470 Ω |
| 100 mA | 10 mA | 430 Ω → 390 Ω | 260 Ω → 220 Ω |
The continuous collector current limit of the 2N3904 and 2N3906 is about 200 mA. Also check the current the input pin can supply (usually a few mA to 20 mA). To switch a 100 mA load you need 10 mA of base current, so check that it fits the pin's allowed current.
For example, to light an LED at 10 mA from a 5 V logic signal, the LED resistor is (5 − 2 − 0.1) ÷ 0.01 ≈ 300Ω and a 4.7 kΩ base resistor will do.
The PNP switch circuit
This is a high-side switch with the emitter on +V and the collector going through the load to ground. The base has to be pulled toward ground to turn it on, so it turns on when the input is Low. To keep it off, the base has to be at the same voltage as the emitter (+V), so put a pull-up resistor of about 10 kΩ between the base and +V.
Common mistakes
| Mistake | Result | Prevention |
|---|---|---|
| Leaving out the base resistor | Base current runs away and damages the input pin or transistor | Always use a series resistor |
| Wrong pinout | Does not work, or gets hot | Check against the datasheet |
| No diode on an inductive load | Back-EMF destroys the transistor | A reverse parallel diode across the load |
| Missing shared ground | The signal references differ so it does not work | Connect the GND of the microcontroller and the load supply |
| Exceeding the current limit | Overheating | Use a MOSFET or dedicated driver for large current |
Checking in Patterndo
Patterndo has the 2N3904 (NPN) and 2N3906 (PNP) transistors, so you can check by simulation that the connection direction and polarity are right and that the LED lights when voltage is applied to the base. However, the transistor in the simulation is a simple switch model that turns on when the voltage between base and emitter exceeds about 0.5 V, and it does not reflect current gain (hFE) or the degree of saturation. So even if you change the base resistor to a very large value, the LED does not dim. Work out the base resistor value yourself with the formulas in this article, and check brightness and heat on the real part. How to use simulation is in Using Patterndo Simulation.
Summary
An NPN has its emitter on ground and turns on with High, and the base resistor is (input voltage − 0.7 V) ÷ (collector current ÷ 10). The pinout differs between parts so check the datasheet, and inductive loads must have a flyback diode. For examples with big loads, see Driving Motors, Relays, and Solenoids, and to build one yourself, An LED Flasher with Two Transistors.