Table of contents
  1. The three terminals of a transistor
  2. The pinout differs between parts
  3. The NPN switch circuit
  4. The PNP switch circuit
  5. Common mistakes
  6. Checking in Patterndo
  7. Summary

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

2N3904 · 2N3906Flat sideEBCBC547 · BC337Flat sideCBE2SC1815Flat sideECBE emitter · B base · C collector (from the left)
Even in the same TO-92 package, the lead order differs by part. Read from the left with the flat face toward you and the leads pointing down.

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

+VR_C (LED resistor)LEDC (collector)E (emitter)R_BBInputLED on when HighGND
An NPN low-side switch. The load (LED and resistor) goes between +V and the collector, and the emitter goes to ground.

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 currentBase currentR_B (5 V input)R_B (3.3 V input)
10 mA1 mA4.3 kΩ → 4.7 kΩ2.6 kΩ → 2.2 kΩ
50 mA5 mA860 Ω → 820 Ω520 Ω → 470 Ω
100 mA10 mA430 Ω → 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

MistakeResultPrevention
Leaving out the base resistorBase current runs away and damages the input pin or transistorAlways use a series resistor
Wrong pinoutDoes not work, or gets hotCheck against the datasheet
No diode on an inductive loadBack-EMF destroys the transistorA reverse parallel diode across the load
Missing shared groundThe signal references differ so it does not workConnect the GND of the microcontroller and the load supply
Exceeding the current limitOverheatingUse 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.

This article is general guidance for learning purposes. Specifications and pinouts differ between parts, so check the datasheet of the part you use before real work. For safety-related work, read the cautions for each item first.