How to use low amperage microcontroller signals to control high current devices. Eric explains how to use transistors to get from the logic of a microcontroller to the power needs of a motor.
I included some comments with Eric’s article to help explain some of the concepts. My text is colored blue. Let us know what you think in the comments.
The recent article on uC Hobby entitled “TTL to RS232 adaptor Explained” got me thinking about some basics people might be missing in their hardware hacker arsenal. With the surge of interest in X10, and other automation, and other user friendly microcontroller platforms like the Basic Stamp, Javelin, and the Arduino , limitless possibilities present themselves. Anything with a physical on/off switch (or, button) can be controlled relatively easy with TTL logic. “How do I do it!?”, you ask.
A little background on TTL voltage and current
As you may or may not already know, typical TTL logic outputs on microcontrollers run in the range of 4.5 – 5.5 volts. Some microcontrollers, such as the microcontroller devices manufactured by Texas Instruments, use voltages as low as 3.3 volts. In regard to current capability, generally speaking, most microcontrollers will only source around 20 mA or less (usually less) of current before you start running into problems. This is less than the amount of current used to power a super bright LED at maximum intensity (the ones I use run around 30 mA, a lot can run less). There comes a time in every man and woman’s life when they just need more power.
The cockroach of all transistors, the NPN BPJ 2N3904 (Link to Fairchild datasheet)
A Bipolar Junction Transistor has many different functions and applications in a circuit. It can be used in an amplifier configuration, a device to regulate the flow of current (think of a valve on a water pipe), or, in our case, a switch. For all intents and purposes, this “switch” can be referred to as a current amplifier. There are three leads on a bipolar transistor: The “base”, “collector”, and “emitter”. In order to use a transistor as an on/off switch, you have do something referred to as “saturation”. This means that you put enough current to the base lead to make the transistor transfer the maximum amount of current provided to the emitter from the collector. The NPN transistor is not the only way to do this. For instance, a PNP transistor can be used with a slight modification to the circuit.
You want to saturate the transistor to minimize the voltage drop across it. This is important because we want all the energy (heat dissipation) to be delivered to the load. If the transistor is not saturated it will waste the energy and possibly overheat.
For our discussion we will use a generic NPN transistor, 2N3904, in a TO-92 package. According to Fairchild Semiconductors datasheet, this transistor has a maximum current rating of 200mA, which, bear in mind, is not the working range, but the rating at which the transistor may become damaged.
If you are planning to output more current than say, 170mA from this signal source, I would suggest a beefier solution. We won’t go into it in this discussion, but you can also derive a lot more power from a device called a “Darlington Pair”, which is nothing more than two transistors with their collectors tied together, and the emitter from the first tied to the base of the second. This configuration is often sold as a package, and can produce very high current gains. The point is, find the right transistor for the job you are doing.
The ugly part. The math.
So, how much current does it take to saturate a transistor? Take this sample circuit, for instance:
Notice we have a resistor in line between the output of the controller, and the base of the transistor. We want our string of LED’s to come on when the microcontroller output goes high. This is where the resistor comes into play. It is a “voltage to current” converter, of sorts. Most of the 5 volt circuits I have seen in this scenario will just put a large value resistor in this position and be done with it. Knowing the math might be nice if you wanted to go further.
First, we need to calculate our actual load current and gain to make sure the transistor we are using is suited for it. In this case, we will refer to it as hFE, but it also goes by the terms gain and beta.
hFE(min) = 5 x load current/max chip current (Note the multiplier by 5, your transistor should be rated for at least 5 times the actual gain as a minimum)
Eric is saying that you should de-rate the current gain of your transistor by 5X. In other words, don’t design a circuit that requires more then 1/5 the rated current gain (beta or hFE) of the selected transistor.
load current = 3 / 100 (ohms law current = voltage across resistor/resistance) = .03 amps (30 mA)
Ohms Law is E=IR or I=E/R or R=E/I
Voltage (E) equals Current (I) times Resistance (R). This law can be rearranged to give you Resistance (R) or current (I) as applied here.
*You might be asking yourself where the 3 volts came from. We had a voltage drop of 3 volts for each LED. 9 volts total for the LED’s voltage drop leaves us with 3 volts left for the resistor (12 – 9).
hFE = 5 x (30mA / 20mA)
hFE(actual) = 7.5
To calculate the actual current for the circuit with 3 LEDs we use ohms law and assume the voltage drop across the transistor is 0 when it is switched on (saturated) the real voltage drop will be about 0.5V but we can ignore that detail. So if we assume that the transistor is really just a connection to ground, all the supply voltage drops across the 3 LEDs and the 100 Ohm resistor. An LED acts like a diode with a forward voltage drop that is dependent on the type of LED you are using. Some drop 2 volts but in this case Eric is using LEDs that are specified to drop 3V. If you are being exact you might have to iterate the math using a current vrs voltage chart for the specific LED or diode in use.
Again, check the datasheet for this information! The transistor I am using is rated at hFE = 100(min), so an actual gain of 7.5, and a load current of 30mA is well within the specifications of the datasheet.
If your chip output voltage, and the supply voltage were equal, you can use the equation: RB = 0.2 × RL × hFE (where RB is resistance of the base of the transistor, RL is the load resistance, and hFE is the gain from the datasheet). Since we are using a 12 volt supply due to using 3 LED’s in series, 5V from the microcontroller and 12V from our voltage source does not work. On to another equation!
Base Resistor = (chip supply voltage x gain) / (5 x load current)
Again, gain in this equation, is not from the calculation above, but from the datasheet.
RB = (VS * hFE)/(5 * lc)
RB = (5 * 100) / (5 * .03) *Note: .03 = 30 milliamps
RB =~ 3333.33 ohms. (3.3K Ohms)
Choose a resistor value that is close to this value.
Just as a check step, we will make sure that the new base resistor does not exceed the chip’s maximum current output:
Base Current (I) = Voltage (V) / Resistance (R) (Ohms law)
I = 5/3.3K
I = .0015 amps *Note: 1.5 milliamps
This is less than the maximum of 20 mA, so the resistor value will work fine!
The reason for doing these calculations is because if there is an incorrect amount of current flowing to the base of the transistor (too much), the transistor will overheat and you can damage it, or the microcontroller.
The voltage drop across the base drive resistor will be slightly less then the full supply voltage (5V) but ignoring this should not greatly effect the result. It is usually safe to use estimations like assuming 0V across the Base to Emitter in cases where the design requirements are not tight. Another good reason to de-rate the beta by 5X. This de-rating will more then make up for errors in estimation.
Just to note, the voltage drop from base to emitter is often around 0.7V but can be greater when you use large base currents which saturate the transistor. If you want to be more exact you can look at the voltage vrs current curves for your specific transistor and iterate the math.