BJTs (Bipolar Junction Transistors) are inexpensive, reliable, relatively easy to use, and very useful. Here’s what you need to know in order to use them as a simple switch.
A BJT is, essentially, a current-controlled current source. In general, the current flowing into one pin is controlled by a smaller amount of current flowing into another pin. In this way, the transistor can act as a current amplifier
There are two types of BJTs — NPN and PNP. These refer to the order of doped silicon layers: negative, then positive, then negative, for an NPN transistor, and the reverse for a PNP.
The three pins on a BJT are the emitter, the collector, and the base. The way I remember which is which is that the emitter emits (into the device) whatever type of carriers (holes or electrons) are described by the transistor type. NPN? Electrons are negative, so they’re emitted by the emitter. PNP? Holes are positive, so the emitter emits those. The other right-hand-side pin, where the carriers are collected, is the collector.
The central pin is the base. This is where the controlling current flows into (NPN) or out of (PNP). (Note: I use conventional current notation, with current flowing from positive to negative.) The thing to remember is that the emitter-base junction is a diode, and therefore requires current limiting (just like an LED does.)
To use a NPN transistor as a switch, it’s easiest to use it to “ground” the load to turn it on. Suppose your load was a lamp. Tie one end of the lamp to the positive voltage rail, tie the other to the collector of the NPN, and tie the emitter of the NPN to ground. Now, flowing a small current into the base of the NPN will cause the NPN to conduct a potentially much larger current from the collector to the emitter, grounding the lamp’s negative terminal and completing the circuit.
PNP transistors are used similarly, except that they are typically used to switch the high side, and current flows out of the base, not in.