Using a bipolar transistor to turn a load on and off

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.

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.

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.
​

3 The resistor value

This is a very important part. In fact it is almost the sole purpose of this resource.

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:

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
​

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:

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.
​

4.3 What was that minimum gain, and where do I get it?

For gain figures, you're looking in the datasheet for hFE (or hFE).

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.
​

4.4 Can my signal drive the base?

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).

From this equation here:

R = ((V - VBE) × hFE) / (Iload × n)
​

We have assumed that the signal can maintain the voltage (V) when it supplies the current ((Iload × n) ÷ hFE).

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.

In this case you'd need to consider a heatsink, or at the very least getting actual voltages for VCE under load.

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: