Transistors. Phew. Biiig topic!
There are several types of transistors. The most common one is the BJT. There are two variants, which are schematically drawn like this (from that same Wikipedia page):
A transistor has three pins, called Base, Collector, and Emitter. Using the convention that current “flows” from “+” to “-”, it’s easy to remember which in an NPN transistor (the most common variant):
- the collector, eh, “collects” current – IOW, this is where current flows into the transistor
- the emitter is where the current flows out again, to ground at some point
- the base is the main control which determines what the transistor does
So what does it do?
One way to look at it, is as a current amplifier: put 1 mA into the base, and the current from C to E will be a multiple of that – anywhere between 50 and 500 times depending on the particular model, i.e. 50 .. 500 mA!
Note that I’m not suggesting that a transistor generates current. Only that when a voltage is applied between C and E, then the current flowing will be a multiple of the “control current” fed into the base.
Things are not quite that simple, though. How do you “put a current” into a component? You can’t just put a voltage on all the pins… before you know it, the current might easily exceed some allowed maximum rating! Transistors have pretty rigid limitations. When you exceed them, they can easily overheat and get damaged.
We don’t really have an easy way to control current, but what we can do is turn ATmega I/O pins on and off, i.e. control the output voltage on those pins. It’s relatively easy to turn that into current though – Ohm’s law: use some resistors!
Here’s a simple circuit to let us explore this behavior:
The input pin can be fed a voltage, and the R1 resistor will make sure that the current flowing into the base of the transistor will be limited. Likewise, R2 makes sure that no matter how much current the transistor wants to pass from its collector to its emitter, there will be a limit.
How much are these limits? Depends on the voltages and resistor values, of course. Let’s assume the following:
- the “+” voltage is 3.3V (and GND is 0V, as always)
- the input pin can have voltages between 0 and 3.3V applied to it
- resistor R1 is 1000 Ω
- resistor R2 is 100 Ω
The maximum current flowing into the base will never be more than when IN = 3.3V, i.e. 3.3V over 1000 Ω = 3.3 mA (using “I = E / R”).
The maximum current flowing from collector to emitter is what you’d get if the transistor were a complete short circuit between C and E, i.e. 3.3V over 100 Ω = 33 mA.
Note how I’ve made all sorts of worst-case assumptions about the transistor. In fact, I’ve calculated these values as if B, C, and E were all shorted together, and hence tied to ground (since E is tied to ground).
A real transistor behaves somewhat differently. But no matter how it actually behaves, I know that no more than 3.3 mA will be flowing into the base, and not more than 33 mA will be flowing into the collector and out of the emitter. Just about any NPN transistor can handle that.
Here’s what happens when IN is 0V (i.e. GND):
- no current will flow into the base
- since the transistor amplifies the current, no current will flow from C to E
- if no current flows from C to E, then it’s basically like an open (unconnected) circuit
- if unconnected, there will be no current flowing through R2
- with Ohm’s law, the voltage across R2 is (E = I x R) = 0 x 100 = 0
- so the voltage on the OUT pin will be 3.3V, the same as on the other side of R2
Now, when IN is 3.3V, this is what happens:
- current will flow through R1
- lots of current will flow from C to E
- that’s the same as saying that C and E are more or less shorted
- that means the OUT pin will now be (close to) 0V
We’ve created a signal inverter: 0V in = 3.3V out and 3.3V in = 0V out!
As I said, big topic. I haven’t even touched on some important details of a “real” transistor.