Study day: Transistors

Occasionally, in the course of my work, or just for fun, I end up learning about something new. I figured in the spirit of student teaching I would summarise the things that I learned. This week: transistors!

Most of my exposure to transistors has been in the context of "and that's how you get digital logic and then computers". I was surprised to learn they are substantially more complicated than that. The first rule of transistor club is that a transistor is not just a fancy switch. The second rule of transistor club is that what transistors actually are is way too complicated to keep in your head, so it's okay to think of them as fancy switches as long as you always remember how wrong you are for thinking it.

The most common kind of transistor is called a BJT, for bipolar junction transistor. They come in two varieties: NPN and PNP. P and N are layers of different kinds of semiconductor, so it's similar to a chicken sandwich vs a KFC Double Down. I never bothered to figure out what you use PNP for, but I'm sure they're pretty useful for something. NPN is the one you usually use if you want to make a fancy switch, so that's the one I used. There are various mnemonics for remembering which is which, I use "look it up on Google".

Theory break! This article does a good job of introducing the basis of transistors from scratch. Basically the N and P layers form variable resistive barriers between them that change how much current will flow through one part of the transistor based on how much voltage goes through another part. For reasons that take two pages of ranting to get to, you can pretend that current through one part increases the current through another part. In other words, the transistor multiplies current by some amount, the "power factor", which is represented as hFE or β.

The way you rig up a transistor to act as a switch is you follow this diagram. Or if you like words: hook the positive end of the thing you want to switch up to the positive end of your power supply, hook the negative end to the transistor's collector. Hook the transistor's emitter to the ground/negative of your power supply. Now to switch it on, you connect the transistor's base to a (smaller) positive voltage (like an I/O pin on an Arduino). Put a resistor in between the base and the voltage! If you don't do this your transistor will get really hot and then catch fire.

If you don't give the transistor enough current it won't switch on fully (aka, saturate), so you want a resistor that is large enough to stop the transistor from blowing up and small enough that it lets all the current through the thing you want to switch. How do you figure that out? You can do some maths, or just start with a high-ohm resistor and try smaller ones until it works.

You want to learn to read a dataheet for a transistor. Here's one for the 2n2222a. Specifically, look for VCEO and IC. These are the maximum voltage and current ratings for the thing you're switching. If you need more voltage or current than that, pick a different transistor or it will get really hot and catch fire. You also want to take your switching current (50mA for most I/O pins) and multiply it by the hFE value. That will be the current you can supply to the thing you're switching. If it's not enough, you need a bigger transistor. If you need really big numbers, check out a Darlington pair, which is two transistors hooked together like a human centipede.

Lastly, there are many good reasons to not use transistors for switching at all. If you can, use a MOSFET. A MOSFET is basically just a super buff ubertransistor that's better for switching in every way. You don't need a base resistor, they're more power efficient, and they can switch ridiculous amounts of voltage and current. If you're driving a motor, for example, a MOSFET is basically required because of the high current a motor needs. Also, if you have a motor with either a regular transistor or a MOSFET, don't forget a flyback diode across the pins of the motor, or else the built up charge in the motor will zap your transistor to death when it stops.

The only tricky thing about MOSFETs is that you have to make sure they will switch on at the voltage you can provide. In theory the datasheet specifies that voltage as a value called VGS(th), but its maximum only tells you the minimum that you need, and you want to be a volt or two over that so that it turns on fully. If the datasheet specifies an RDS(on) with a VGS next to it that means they tested it and it was fully on at that voltage. If that voltage isn't the one you want you can use this technique to check the datasheet's "I-V curve"; you want the voltage and current you're using to be on the flat part of the curve for your VGS. If it doesn't have a line for your voltage, don't use it. Keep in mind the graphs are "typical" (ie, optimistic).

General advice: check everything you're told two or three times before you believe it. Lots of people know nothing about transistors, and even people who know some stuff about them are often working off information they learned in undergrad back before we beat the soviets, or heard from someone's uncle who does electronics. Strive for either well-accepted theory or testable facts if you can. Don't trust any part recommendations that aren't backed up by the datasheet. Use a multimeter. Buy extras. Derate.