This resource describes very simply how to use an NPN transistor to switch a load (for example a relay or a buzzer, or a motor) from a signal source (for example a microcontroller).
1 The basics
You want to use a small current (from your microcontroller) to switch a larger current (for a motor). We will assume that the motor runs from the same power supply as your microcontroller (but it need not)
You need a simple circuit like this:
Figure 1.1 - Basic circuit.
When the input is taken high, the load will switch on. When the input is pulled low, the load will switch off.
Your obvious next question is "What are all those components, and how do I select them?"
(Good questions, by the way)
2 Choosing a transistor
This is not always simple, but for a load of 50mA or so and a voltage of 20 V or less, I would use a BC548 or a 2N3904.
For higher currents a 2N2222 is also suitable (up to about 500 mA).
If you are switching a heavier load, or from a higher voltage, you might like to ask for advice.
If you're somewhat familiar with transistors then you want an NPN transistor capable of a current (IC) at least twice the maximum load current, and capable of withstanding a voltage (VCE) substantially higher than your supply voltage.
If you're switching a motor, remember that the current increases under load and that it is highest when the motor is stalled. You may need to consider this current.
3 That diode? What's that for?
The diode D1 shown in figure 1.1 is used to prevent large voltage spikes that an inductive load generates as it is switched off. A motor is an inductive load. Other examples of inductive loads include electromagnets, solenoids, and relays.3 The resistor value
If you don't know whether your load is inductive or not, either ask or just use a diode anyway.
The diode should ideally be rated for the load current and for a voltage significantly higher than the supply voltage. In many cases a 1N4001 (1A 50V) is a suitable choice. Most diodes allow large peak currents (higher than their continuous rating) so a diode like this may be suitable for larger current loads.
Be very careful which way around you place the diode in the circuit. Putting it around the wrong way and switching the transistor on will probably result in failure of your transistor. The band on the diode should be connected to the positive supply rail (the opposite way that you normally would use a diode).
Figure 3.1 - Diode (symbol and common appearance)
Note the + and the - shown in figure 3.1. The diode will conduct if the anode is more positive than the cathode. When we say to put a diode in "reverse biased" or "backwards" we mean that the cathode is connected to a point in the circuit that is normally more positive than the point that the anode is connected to.
It's beyond the scope of this resource to explain why, but an inductance reacts against changes in current. This reaction can damage or destroy the transistor. The diode acts to suppress the voltage spike that would otherwise be produced when current through the inductive load suddenly stops. Without the diode, the voltage spike can easily destroy the transistor.
This is a very important part. In fact it is almost the sole purpose of this resource.4 Is that all?
The resistor limits (and essentially sets) the base current for the transistor. This in turn determines how much current the transistor can switch. We need to be generous so that the transistor turns on completely.
The value of the resistor is given by:
If we were to assume you wanted to drive a 300 mA load from an output pin of a microcontroller running at 5 V and our transistor has a minimum gain of 100 at IC of 300 mA, and that we chose n = 3, then:R = ((V - VBE) × hFE) / (Iload × n)Where:
R is the resistor value in ohms
V is the signal voltage
VBE is an estimate, 0.8 is a good start.
hFE is the minimum gain of the transistor at IC = Iload
Iload is the maximum load current
n is a number between 2 and 10
Therefore we need a resistor of 467 Ω or less.R = ((V - 0.8) × H) ÷ (I × n)
= ((5 - 0.8) × 100) ÷ (0.3 × 3)
= (4.2 × 100) ÷ 0.9
= 420 ÷ 0.9
= 467 Ω
Resistors come in preferred values, with E12 values being particularly easy to find. Thus we might choose a 390 Ω resistor
However see section 4.4 because if we are really driving this from a PIC, there might be another issue to think about.
In many cases that may be all you need to worry about, however there are still some outstanding questions you might have:
4.1 Where did that n come from and why did you pick 3?
4.2 What's that 0.8?
The value for n is chosen to ensure that the transistor is saturated, that is, that it is fully turned on for that particular current.
I normally pick 2, but Kris has some good arguments for picking a higher number.
For more information, you might like to read this.
4.3 What was that minimum gain, and where do I get it?
This represents the voltage which appears between the base and emitter of the transistor. You'll find it in the datasheet as VBE(on).
Figure 4.1 - VBE(on)
Whilst we're probably used to seeing 0.6V or 0.65V, this value rises with base current. A value of 0.8V errs on the side of safety (I've often used 0.7 V). When in doubt, refer to the datasheet, and ensure you pick the value for the same or greater IC than the one you'll be using.
Note that this example is only given for IC of 2 mA and 10 mA. You should use the maximum value (the three columns are minimum, typical, and maximum).
Some datasheets will contain a graph of VBE against IB or IC. Since we know IC, that would be more useful.
Figure 4.2 - Graph of IC Vs. VBE
It may be hard to see, but this graph is showing the typical values (and that is normal unless stated otherwise). As you can see, VBE does not hit 0.8V until 100mA collector current (which is large, but not the maximum for this transistor).
As you can see 0.8 is fairly generous, but for collector currents approaching the maximum for the transistor, and especially where you're driving it from a low voltage, check the datasheet.
For gain figures, you're looking in the datasheet for hFE (or hFE).4.4 Can my signal drive the base?
Sometimes you get something that's not particularly useful:
Figure 4.3 - hFE ranges.
This tells you that for an 'A' designation transistor (e.g. BC548A) the gain is between 110 and 220, with different ranges for 'B' and 'C' variants. If you don't know what the variant you have is, the gain could vary between 110 and 800. You would use 110 in your calculations, but you would need to be aware that the gain is also dependent on load current (IC).
Something like this is more usual:
Figure 4.4 - hFE range at specified IC
At least this tells us that the gain is at a load current of 2 mA (a very low current for our purposes).
Sometimes you might find a graph:
Figure 4.5 - Graph of typical DC current gain (hFE) Vs. IC.
As mentioned earlier, this reflects typical values (and it is for the unnamed variant!). It looks like the minimum value is about half the typical value, so I would halve any value I read from this graph.
This is a log/log graph, so at 100 mA, my reading is that the typical gain is about 170, so for minimum I would predict 85.
Another method (and one I personally prefer because it's easier to interpret) is this (and it's for a different transistor, so the figures will be different):
Figure 4.6 - Table of hFE ranges at specified IC and Tj.
This tells us the gain at a wide range of load currents and even at some different temperatures. In this case I would note that at 250 mA the minimum gain is likely to be around 40 (I take the next highest current).
Note that the gain peaks at some sweet spot of load current and falls off at either side of it. The exact pattern of this will vary between transistors.
Also note that the gain is less than half at -55ºC than it is at 25ºC (the normal default for datasheets). If you're going to be operating equipment at really low temperatures that may be worth knowing.
If you can't figure it out, ask. Datasheets can be really good at expressing common information in yet another new and unusual way, or you might simply not be confident of picking the right figure.
We're almost always considering a case where a comparatively small current is used to drive a larger one. The gain of the transistor allows us to do this. But for larger currents especially, the gain of the transistor may be insufficient -- the current required may still be too large. And even if the current can be supplied, will the voltage drop? (This will lead to a lower current).4.5 Do I have to worry about the resistor power?
From this equation here:
We have assumed that the signal can maintain the voltage (V) when it supplies the current ((Iload × n) ÷ hFE).R = ((V - VBE) × hFE) / (Iload × n)
If the load can't supply that current at all, then you need to look at some other solution (mosfet or darlington transistor perhaps).
If the signal can supply this current, but at a reduced voltage, you need to change the V used in the equation to reflect the voltage which can be supplied.
Figure 4.7 - Maximum current from an I/O pin.
This is an example for a PIC12C508, but it will be reasonably similar for other PICs. But you really need to look up the datasheet if you're not familiar with it. In this case the data starts on page 69 (in the "Electrical specs), so you may have to do some digging.
Let's assume we require 4mA. Clearly that's under 25 mA, so let's go to the next stage (but surely there can't be a problem, right?).
Figure 4.8 - Output voltage will change with load.
At 4 mA, the output voltage drops to a little over 4.5 V at 25ºC, and to about 4.3 V at 125ºC. We're probably not expecting it to run at 125ºC, but it would not be a stretch to have it operating at above 25ºC.
I would use 4.4 V instead of 5 V if I was expecting a current of 4 mA.
Notice that the voltage is not specified for currents in excess of 5 mA. If you require 10 mA (which is not a huge amount) you could either extrapolate (to 3 V?) or do some tests and measure it. However you'll need to remember that you can't easily do these tests over a range of temperatures.
In the example in section 3, we need about 9 mA of base current, so let's assume we only get 3.5 V at the output.
Repeating the calculation with the new value for V:
Therefore we need a resistor of 311 Ω or less (we'd probably use 270 Ω).R = ((V - VBE) × hFE) / (Iload × n)
= ((3.5 - 0.8) × 100) ÷ (0.3 × 3)
= (2.8 × 100) ÷ 0.9
= 280 ÷ 0.9
= 311 Ω
You only have to worry if it's a problem.4.6 Do I need a heatsink?
The easiest calculation is:
So if your calculation was for a 3900 Ω (also written as 3.9 kΩ, 3.9 k, or 3k9) resistor on a 5 V supply, the dissipated power would be:P = V2 ÷ R
Since the smallest resistor you're likely to use (even with surface mount) is 1/16W (0.0625 W) you have no problems (you're only dissipating 1/160 W!)P = V2 ÷ R
= (5 × 5) ÷ 3900
= 25 ÷ 3900
= 0.006 W
= 6 mW
Note that the actual power dissipation is likely to be less because we didn't bother with the VBE voltage drop.
If you do this rough calculation and the power is more than about 50% to 75% of the rated power of your resistor then you might consider a higher powered resistor.
The quick calculation would be:
Note that this calculation assumes 100% duty cycle.T = 25 + ( IC × VCE(sat) × 2 × RθJA )
If that comes to more than 100 then you definitely need to investigate further.
In this case you'd need to consider a heatsink, or at the very least getting actual voltages for VCE under load.For:
IC = 0.3 A
VCE(sat) = 1 V
RθJA = 200ºC/W
T = 25 + ( IC * VCE(sat) × 2 × RθJA )
T = 25 + ( 0.3 × 1 * 2 × 200 )
= 25 + 120
To get some perspective, 145ºC only a shade under the maximum junction temperature (that's what we calculated) of 150ºC (this can vary from 125ºC to 200ºC depending on the device). Above these limits you'll definitely cause damage. At these temperatures the lifetime of the transistor will be shortened.
If you need to consider a heatsink (or if you just want to know more), see here.
From that calculation here are some rough ideas of what you should be thinking:
- < 50ºC -- You're cool!
- ≥ 50ºC, <75ºC -- Consider a heatsink in high ambient temperature environments
- ≥ 75ºC, < 100ºC -- Consider a heatsink in all cases
- ≥ 100ºC -- Do full calculations just to be sure.