Microcontroller Interfacing –  Part 7

Using Transistors to Drive Loads

Goals

Many components such as relays, solenoids, high power LEDs, buzzers and others require more drive current and/or higher voltages than the microcontroller outputs can handle.  One way around this problem is to use the microcontroller to drive a transistor, which in turn controls the load. This section will consider only the NPN type transistors. Other transistor types will be considered in other sections.

Transistor Basics

Transistors are current amplifiers.  A small current is driven through the base and emitter, which is essentially a diode.  This is referred to as Ib.  The collector current will be the base current multiplied by the transistor’s DC gain, referred to as hFE in the data sheets. Figure 7-1 shows a simple transistor circuit with the elements of the transistor labeled.

A transistor can be operated in its linear range.  A boom box radio will have a number of transistorized linear amplifier stages, increasing a weak radio signal to a level loud enough to disturb an entire neighborhood. 

Microcontroller applications usually do not run the transistors in the linear range. Rather, they are used as ON-OFF switches.  Consider the circuit in Figure 7-1.  Assume that when the output pin P0 is set high the base current, Ib, is 10 ma, and the transistor’s hFE is 100.  What will be the collector current? 

Ic = Ib * hFe= .01 * 100 = 1A

Is that correct? No.  Suppose we shorted out the transistor. What would the current be then? Ohm’s Law tells us

I = V/R = 10/100 = .1A  = 100ma

At some point the external components will force a limit on the collector current. At that point the transistor is in saturation. Any increase in base current will have no effect on the collector current.  The transistor is acting as a switch.  In most microcontroller circuits transistors are used as switches, and are either on or off.

A transistor is not a perfect switch. Even in saturation there will be a voltage across the transistor between the collector and the emitter.  This voltage is known as Vce(sat). This will usually range between .3V and 1V, depending on the voltage and currents. In many circuits Vce(sat)  can be ignored. 

Figure 7-1

Example: Driving a Relay

Figure 7-2 shows a typical circuit where a transistor is used to drive a relay.  A relay can be used to switch higher voltages and currents than the microcontroller can. It can also be used to switch AC signals.  Looking through a catalog we select a 12V relay.  The data sheet says the coil resistance is 360Ω.  What will the current through the coil be? Ohm’s Law says

I = V/R = 12/360 =  .033A or 33ma.

We cannot drive this relay with the microcontroller output pin directly for two reasons. First, the relay must use 12V and the micro runs at 5V. Connecting 12V to a microcontroller pin would probably destroy the chip. Fortunately we can run most transistors at higher voltage and isolate the microcontroller from it. Second, the relay requires 33ma of current, and the micro we are using is limited to 25ma. We must use a transistor to drive the relay.

The first step is to select a transistor.  The transistor must be able to handle the voltages and currents of the application.  In this case just about any NPN switching transistor will do. The 2N2222 and is relations, the 2N2222A, PN2222, etc. are commonly used in these circuits. A quick review of the transistor data sheet shows: 

Absolute Maximum Ratings

Symbol Parameter                               Value

Vceo Collector-Emitter voltage             30V

Ic Collector current                               600 ma

Figure 7-2

Our circuit uses 12V and will draw 33ma. Clearly this transistor will easily handle this.

The next step is to ensure we drive the transistor into saturation. We will do this by selecting the proper value for R1.  As mentioned before, transistors are current amplifiers.  The collector current will be the base current multiplied by the DC gain, hFE.  One question is what is the hFE? The data sheet for a PN2222 shows the hFE as a minimum of 35 under one set of circumstances, and a minimum of 100 and a maximum of 300 under other circumstances. That is a huge range!  What value should we use?

The reality is that transistor parameters run all over the place.  The simplest thing to do here is use the worst case value, or 35 in this case.

Changing the transistor gain equation around and using the 33ma relay coil current and PN2222 hFE gives us

Ib = Ic/hFE  = 33/35 = .94 ma

For simplicity we will round up Ib to 1ma.  Actually we will probably want to double it.  Tolerances in the value of R1, the transistor and microcontroller suggest we should be on the conservative side. Running Ib at 2 or 3 ma will not cause any damage and will give an extra margin to ensure the transistor is driven hard into saturation. 

One reason you might want to stay near the 1ma base current is if your application is battery operated and you want to conserve every microamp to increase battery life. In that case you would probably not be using a relay anyway, so we will use 2 ma as our base current for this example.

The driving pin from the microcontroller will supply 5V.  There will be a voltage drop between the base and emitter VBE(sat). The data sheet shows this as 2V maximum with much higher base and collector currents that we will be using. In our circuit it will probably be in the .7V to 1V range. For simplicity we will use 1V.

Since the voltages across the circuit loop must be 0V we have the following: 

VP0 = VR1 + VBE

Solving for the voltage across R1, we get the following

VR1 = VP0 – VBE = 5V – 1V = 4V

So, R1 will drop 4V with a current of 2ma. Ohm’s Law says

R = V/I = 4/.002 = 2000Ω

We will use a 2K resistor for R1.  There are two more components in Figure 7-2 we have not accounted for, D1 and R2.  D1 is a diode to snub the current spike from the inductor coil.  A lot of energy is stored in the magnetic field of the relay coil when it is energized. When we turn off the relay, that energy has to go somewhere. The collapsing magnetic field will generate a current spike. It sees a turned off transistor. This current, combined with a high transistor resistance will be translated into a high voltage across the transistor, possibly damaging it. The diode should be put across all inductive loads including relays, solenoids, motors, etc.

The diode type is not too particular. Diodes from the 1N400X family (1N4001, 1N4002, 1N4004) are commonly used and inexpensive.  Their current rating is 1A, and the 1N4001 has a breakdown voltage of 100V. The other members of the family have even higher voltage ratings.

The remaining component is R2, and may not be needed in some applications.  When power is first applied to the circuit, you might get some current glitch through the transistor, turning it and the relay on momentarily.  This might not be tolerable.  Putting R2 in will help prevent the transistor from turning on (no guarantees) by draining any charge out of the base of the transistor.  10K resistors are a typical value for this component.

Let’s take a closer look the voltages across the relay and transistor.  We know we will have 12V from the supply, and Vcesat is going to be around 1 volt.  We know that the voltage across the transistor plus the voltage across the relay must be 12V. That means that the voltage across the relay must be

Vcc = Vrelay + Vcesat

Vrelay = Vcc – Vcesat = 12 – .3 = 11.7V

But our relay is a 12V relay!  Is it going to work? Relays are very forgiving devices and will usually work at much lower voltages than the nominal voltage. The data sheet will usually have a specification like Must Close Voltage.  For a 12V relay this will usually be in the 9-10V range.  Keep in mind that the operating time will usually increase as the voltage drops below the nominal voltage.

It might seem to be a lot of work to calculate all this. After you work with these circuits for a while you will just know that the value of R1 in not very critical and anything from 1K to 3.3K will work fine in most cases.  

Example: Driving a high current LED

In Part 4 we had an example of driving an LED directly by the microcontroller pin. In this example we will assume we want to drive a bright blue LED. The data sheet shows we want to run this at 50ma, and the forward voltage drop across this LED is 3.1V. Let’s further assume that this is a portable device and we want to run it off a 9V battery.  The circuit has a 5V regulator to run the micro, but we want to run the LED off the 9V.  We will use a similar circuit to the relay driver above, but like the LED circuit in Part 4, we need a current limiting resistor R2. The circuit is shown in Figure 7-3.

From our earlier example we learned that the value of R1 is not too critical, and just decide to use a 1.5K resistor.  We want to control the current through the LED fairly closely so we need to consider a few things we ignored in the relay example. We will factor in the voltage drop (VCE) across the transistor switch for this application.  The data sheet says VCE(sat) = 1V max. The conditions for this value are pretty high base and collector currents, which we are not using.  VCE will probably be closer to .3V for us, and we will use that value.

The LED circuit is driven by 9V. The voltage around the circuit loop must be 0, so we have

Vbat = VR2 + VLED + VCE

Rearranging this gives us

VR2 = Vbat – VLED – VCE =  9 – 3.1 - .3 = 5.6V

We now know that R2 will drop 5.6V and the current will be 50ma.  Ohm’s Law says

R = V/I = 5.6 /.05 = 112Ω

  

The nearest common resistor value is 110Ω so that is what we will use.

Note this circuit does not have a couple of components that are in the relay driving circuit. We don’t need the D1 diode across the LED because the LED is not an inductive load. We also removed the resistor from the transistor base to ground. We decided in this application that a brief flash of light from the diode would not be a problem when the device is first turned on.

Figure 7-3

Other considerations

In both of the examples we had the transistor in a common emitter configuration, meaning the transistor’s emitter is grounded. The transistor is acting as a “low side switch”.   Since the transistor is a switch, couldn’t we also connect the NPN transistor’s collector to the power supply voltage, and put the load between the emitter and ground?  After all, there are some cases we would like the load to be at ground potential.

This will not work in most cases. The reason is that the load will generate negative feedback, causing the transistor to try to turn off.  Suppose we tried that. As the port pin started to go high, current would flow through the base of the transistor, causing current to flow into the collector and out the emitter though the load. This current would cause a voltage to form on the load. As the voltage at the emitter rises, the current through the base would decrease, causing a reduction in current through the transistor. Essentially the transistor will not turn on properly in this configuration.

If it is necessary to ground the load and use a transistor as a high side switch, a PNP transistor will be needed. This will be covered in a future segment.

Summary

When the load the microcontroller must control has voltage or current requirements that exceed the capability of the micro’s output pin, an NPN transistor can be used to switch the load.  Be sure the transistor can handle the voltage and current requirements.  Gain and other specifications of transistors have a very wide tolerance range. Fortunately it is easy to make the design tolerant of component variations. 

Gotcha Checklist

1. Ensure the transistor can handle the voltage and current required by the load.

2. Protect the transistor with a snubbing diode if the load is a relay, solenoid, motor or otherwise inductive.

3. With an NPN transistor use a common emitter circuit with the emitter grounded and the load on the collector.

  

© 2009 - 2022 Gary C. Sutcliffe

  

Created with the QTH.com SiteBuilder.