How to Use A Transistor as a Switch

How to use a transistor a switch (work in progress)

1. Let’s assume you want to switch a motor or a light bulb. The first step is to determine the voltage and current of the load, the thing you are trying to control.

BJTTransistorSwitch_schem

In the case of the motor, if you know where it came from, you can look up the specifications on the manufacturers website. If you don’t know where it came from, you might have to take a guess or make some measurements.

For a light bulb, the voltage and wattage is usually printed on the bulb. To calculate the current, simply divide the wattage by the voltage. For example, a 12VDC light bulb rated at 24 Watts draws 24W/12V = 2 Amps.

In summary:

  1. Can your transistor handle the load current through its collector?
  2. Can the transistor handle the supply voltage?
  3. Find the worst case current gain during saturation.
  4. Calculate the minimum base current needed to keep the transistor in saturation, using this worst case current gain and the load current
  5. Compare this base current to the maximum current your microcontroller (or other controlling circuit) can deliver.
  6. Find the very highest (worst case) Vbe.
  7. Calculate the voltage drop across the base resistor using Vbe and the controller output voltage.
  8. Calculate the required base resistor.
  9. Calculate the collector power dissipation

Example 1

Jameco 400995 DC gear motor. According to the datasheet for this motor, at 12VDC with no load it only draws 76 mA, but if you stall the motor it shoots up to 1250 mA, or 1.25 Amps. A motor always consumes the maximum current when stalled.

Thus the load voltage is 12VDC and the maximum load current is 1.25 A

Vload = 12V
Iload(max) = 1.25A

In general, we can use both PNP or NPN transistors as switches. However, PNP transistors can only control the same voltage as is supplied to the Arduino chip. In this case, since the voltage we are controlling (12 V) is different from the Arduino voltage (5 V), we have no choice but to use an NPN transistor.

Next, we see what NPN transistors we have in our box of parts. Let’s pretend we have a PN2222A, a TIP31 and a TIP120.

Next, we need to check the datasheet for each transistor.

First we need to make sure that the transistor can safely handle the worst current we might draw. The parameter we are looking for is the maximum collector current, Ic(max).

The datasheet for the PN2222A shows Ic(max) = 0.6 A, which is too little for our needs.

The datasheet for the TIP31 shows Ic(max) = 3 A, which is safely above the 1.25 A our motor will draw if it is stalled. So the TIP31 is a contender.

Next we have to verify that the transistor can safely handle the supply voltage we plan to use. The parameter we are looking for is the maximum collector emitter voltage, Vceo(max).

The TIP31 comes in 4 versions, with Vceo(max) ranging from 40 V to 100 V, all safely above the 12 V we plan to use. So the TIP31 is still a contender.

Now we need to calculate whether we can provide sufficient base current to keep the transistor in saturation. First we need to find what the base current will be when the transistor is carrying the worst-case current of 1.25 A. According to Figure 2, Ic/Ib = 10 or Ic = 10 * Ib. This means that for our collector current of 1.25 A, we would need to deliver a base current of .125 A, which is too much for our Arduino, which can deliver (safely) at most 40 mA.

Finally let’s take a look at the datasheet for the TIP120. First, we see that Ic(max) = 5 A, and that Vceo(max) is 60, 80, or 100 V, so we are fine so far.

Next we check the base current. Again this is indicated in Figure 2, but this time Ic=250 * Ib or our collector current of 1.25 A requires a base current of 5 mA (5 * 250 = 1250), which is well below the maximum of 40 mA the Arduino can put out.

Finally we need to select a base resistor which will be low enough to ensure that the TIP120 remains saturated, but high enough to prevent the Arduino from trying to deliver more current than it should. We want a current between 5 mA and 40 mA, so let’s pick a midway point of 20 mA.

Back to Figure 2 where we see that when the collector current is 1 A, Vbe(sat) is about 1.5 V. Now if the Arduino is putting out 5 V, and Vbe is 1.5 V, that means that the resistor has a voltage drop of (5 – 1.5) or 3.5 V across it. Using Ohm’s law, R = V/I = 3.5/(20 mA)= 175 Ohms.

Example 2

This example is from an excellent discussion here. To summarize:  A strip of LEDs requires 400 mA at 12 volts, and is to be controlled from a Raspberry Pi.

A TIP120 is proposed.

  1. Can the TIP120 handle the load current? According to the first page of the datasheet, in the table titled “Absolute Maximum Ratings”, the maximum collector current Ic is 5A, well above our requirements.
  2. Can the transistor handle the 12V required? According to the same table as above, Vceo(max) is 60V, well more than we need.
  3.  Find the worst case current gain during saturation. Note that on page 2 of the datasheet, the DC current gain hfe is quoted at 1000 minimum, but this is at Vce=3V, which is not saturated, so we can’t use that value. Instead, look at Figure 2 on page 3. In the top right corner of this graph is Ic = 250 Ib, so let’s use that value. This seems much higher than the worst case default we use of 10, but given this is a Darlington pair (2 transistors, one after the other), it’s not unreasonable.
  4. Calculate the minimum base current needed to keep the transistor in saturation, using this worst case current gain:
    Ib = Ic/250 = 400 mA / 250 = 1.6 mA.
  5. Compare to the maximum current an output of the Raspberry Pi can deliver. There is some confusion over this, but as discussed on this page 1.6mA seems safe.
  6. Find Vbe. According to the graph in Figure 2, Vbe(sat) is below 1.5V all the way up to 1A, so by choosing a worst case Vbe(sat) of 1.5V we should be extremely safe.
  7. Now we calculate the voltage drop across the base resistor. The output voltage of the Raspberry Pi is 3.3V, so the voltage drop across the resistor is
    3.3V – 1.5V = 1.8V
  8. Now we calculate the base resistor. Recall that the minimum base current we need to drive the transistor into saturation is 1.6 mA so
    R = V / I = 1.8V/1.6mA =~ 1.2K ohm
  9. Finally, we should calculate the collector power dissipation, because it’s worse for a Darlington due to the higher Vce(sat). Using Figure 2 again, we see that Vce(sat) is about .75V, so the collector power dissipated is:
    Pc(max) = Ic(max) * Vce(sat)(max) = 400mA * .75V = 0.3 W.
    According to the “Absolute Maximum Ratings” table on page 1, the maximum power dissipation is 2W when the ambient temperature Ta = 25C, so we should be safe here as well.

References

  1. Excellent tutorial on using transistors as a switch, but I’m not convinced they addressed properly the issue of reduced gain in saturation
  2. Much more concise tutorial containing less theory and more practical
  3. Example showing how to create an H-bridge using transistors

TO DO:

  • We have calculated this for the worst case current draw, which is when the motor is stalled. How does our transistor switch perform with the motor is operating at the other extreme, the no-load current of only 76 mA? How do we verify that the transistor is still saturated, i.e. turned on?
  • Explain why we need the flyback  diode for inductive loads.
  • Include an example of a 2N2222 followed by a TIP31, to show how multiple stages can be used.
  • Explain the difference between NPN and PNP and why we prefer NPN.

49 Responses to “How to Use A Transistor as a Switch”

  1. sakshi Says:

    nice..:)

  2. Mike Says:

    “Ic=250 * Ib”
    Why?
    TIP120 has hfe of 1000

    • michaelshiloh Says:

      Very good question, and understanding this is at the heart of working effectively with transistors as switches. The answer is pretty simple: Hfe of 1000 is only in the linear or active range. When the transistor is used as a switch we want it to be in saturation which minimizes the collector-emitter voltage drop Vce. Determining Hfe in saturation is not easy: it depends so much on the collector current and other factors. Some datasheets have graphs, but most sources I found recommend using a very conservative value of 10 (yes, only ten) to assure that the transistor is in saturation. Why then did I use 250? Look at Figure 2 in the datasheet. In the top right corner is this equation: Ic = 250 * Ib.

      If you are interested in understanding this further let me know and I’ll try to dig out some of the references I used. I’m pretty sure one of my sources was “The Art of Electronics”.

  3. Abraham Says:

    Yes, I’m interested in that reasoning too. Why is that equation provided in the graph? Is 1000 the “theoretical” gain while 250 is the more practical and more “safe” value to use?

    • michaelshiloh Says:

      The equation and the graph only apply when the transistor is operating in the linear or active region. When used as a switch, we want to drive it from cutoff to saturation as quickly as possible, so the information for the active region does not appy.

  4. Bo Says:

    Sorry, might be a stupid question but why do you choose 1A in the last step? Shouldn’t this be 1.25A?

    • michaelshiloh Says:

      Never apologize for a question! Not stupid at all. In this case the answer is quite simple. If you look at Figure 2 you’ll see that it doesn’t have a whole lot of precision, and there is a good line at 1A, so I used that to get in the ballpark. I guess I could have bumped Vbe(sat) up to 1.6V to allow for this. I should make this correction to avoid confusing others. Good call!

      In the end, though, you won’t find a perfect 175 Ohm resistor anyway – you’ll use a lower standard value to assure that there is enough current to drive the transistor well into saturation. The 175 Ohms that we calculated should be considered the maximum resistance.

      Good question.

  5. Mark Says:

    Sorry, but i have an additional question. In the datasheet of the PN2222A transistor there isn’t a figure with a formula like Ic/Ib = 10. I noticed that it is missing in more datasheets of different kind tranistors. How do i know what de Base current must be to keep de tranistor saturated?

  6. michaelshiloh Says:

    No apologies! That’s a good question.

    A good rule of thumb is to assume a worst case current gain of 10, so your steps should be:

    1. Calculate the maximum collector current you must deliver. Check that your transistor can handle this.
    2. Calculate Ib = Ic/10. This is your minimum base current required for saturation Ib(sat)(min)
    3. Make sure your controller can deliver that Ib(sat)(min)
    4. Check the datasheet for the maximum base current the transistor can handle Ib(max), and use a base resistor to limit Ib to something between Ib(sat)(min) and Ib(max)

    Don’t hesitate to ask if you have more questions

    • Mark Says:

      Thanks for your reply. It helped me a lot.
      However, I have a problem now.
      I want to control leds 2:
      – Source voltage: 12v
      – diode forward voltage: 3.5v
      – diode forward current : 20mA
      – number of LEDs: 2
      – LED-resistor i use: 330 ohms
      – my controler can deliver: 3.3v / 40mA

      I think the best way to connect them is as follows:
      12v+ —-|>|—-|>|—/\/\/—- – transistor collector (GND).
      (side note: or it is better to put the LED-resistor after the emitter?)

      I have a bc547B & a bc337B tranistor. I did as you say above:
      1: Calculate the maximum collector current: 20mA. both the bc547B & bc337B can handle this
      2: Calculate Ib = Ic/10. For the bc547B this is: Ib(80)=800/10. For the bc337B this is Ib(10)=100/10.
      3: Make sure your controller can deliver that Ib(sat)(min). I conclude that I can only use the BC547B.
      4: Check the datasheet for the maximum base current. My problem: i can’t find in both datasheets the Ib(max).

      bc547B : http://www.datasheetcatalog.org/datasheet/stmicroelectronics/8854.pdf
      bc337B: http://www.fairchildsemi.com/ds/BC/BC337.pdf

      What should I do now to determine whether the traisistor is suitable and what base resistor I should use?

      Thanks in advance!

      • michaelshiloh Says:

        In step 2 you used 80 mA (for the 547) and 100mA for the 337. This is wrong. You should use the 20 mA that your LEDs will consume.

  7. igor gumush Says:

    hi , sorry i did understood the last part
    can you tell me if this:

    http://hep.fi.infn.it/PAMELA/pdf/FFB2907A.pdf

    would work with arduino and supply 5v / 500mA

    • michaelshiloh Says:

      The question of whether a given transistor will work or not depends on the load, and not on the power supply. Yes, the power supply has to be able to power the load, but that’s not the topic of this discussion.

      Arduino can deliver safely at most 40 mA per output. If your transistor data sheet shows a graph for Ic/Ib (i.e. gain) during saturation, you can use that number. If you lack this information, a very safe gain factor is 10. So, in the very worst case, you can drive a 400 mA load using any transistor driven by an Arduino.

      I hope that helps. Ask me more if not. You’ll need to provide more details.

  8. Eric Says:

    This is my first time trying to figure all of this out. So here it goes. I’m trying to power 4 sensors that run at 5V and have a peak current of 100mA peak or 400mA (as all 4 will be running at the same time). So my minimum base current would = 400mA/10 which is 40mA. The Arduino is rated to around 40mA – 50mA so that should be fine. Will this work without a base resistor, as the Arduino will not deliver much more than the minimum? Also can you recommend a through hole transistor to get this job done?

    • michaelshiloh Says:

      Thanks for your interest. I’m confused about your application. Can you post your schematic somewhere? Normally transistors are used to turn loads i.e. output devices on and off; a sensor is an input device and is normally always on.

  9. spuder Says:

    Thanks so much for this post, this really helps. I still have some questions which I’ve outlined here http://electronics.stackexchange.com/questions/80437/using-a-microcontroller-to-turn-on-led-strip

    • michaelshiloh Says:

      Thanks for your comment Spuder. Your outline is great, and I see has sparked excellent discussion. I think that Oli Glaser answered your questions, but I’ll also try to answer your question by doing an example based on your case. Let me know if that helps.

  10. ben Says:

    Hi.
    when i used the transistor as a switch,i need to find the Base Emitter Saturation Voltage and the Collector Emitter Saturation Voltage.for example on 2N2222 data sheet,there are min-Vbe(sat)of 0.6v and max-Vbe(sat)of 1.3v or 2.6(depend of the test condition currents.and there are only max-Vce(sat) value(min is not mention)of 0.4v and 1.6v(depend on test condition).
    The problem is that i don’t understand what and how to choose the right value?what are the factors that should i take into consideration when choosing the values for my calculations?
    When should i choose the 0.6Vbe(sat) and when the 1.3Vbe(sat) ?What is the relationship between the currents in the circuit to the decision of picking a value from the data sheet?

    Thanks in advance.
    Ben.

    • michaelshiloh Says:

      The easy answer to this difficult question is to use the worst case. In other words if you are given a range of .6V to 2.6V for Vbe, you pick the worst one. What is the worst one? Since we use Vbe for calculating the base current, we pick the Vbe that gives us the largest base current. This way, if we satisfy the most difficult requirement, we necessarily satisfy the easier requirements as well.

      Confused? Try both and see which gives you the smaller base resistor. That’s the one to use, because that gives you the bigger base current, which certainly also provides the smaller base current.

      Remember this: The whole goal here is to make sure the transistor stays in saturation. There is a base current at which the transistor goes into a saturation, but providing more base current doesn’t hurt (as long as we don’t exceed the maximum base current). For example, if one calculation (based on one value for Vbe) gives you a base current of 10mA, and the other calculation (based on the other value for Vbe) gives you a base current of 50mA, then pick your resistor to provide 50mA. This way you know that if everything is the worst case, your transistor will still stay in saturation.

  11. Nick Says:

    Great article. So if my load was an LED (say a high-powered IR LED) I also need to subtract the Vce as well as the forward voltage for my particular LED from my supply voltage in order to come up with the correct voltage in order to get the correct resistor value to drive it at the current I want it driven at?

    • michaelshiloh Says:

      Thanks Nick for your compliment,

      When you say resistor I presume this is a current limiting resistor in series with the LED.

      If so, then the answer to your question is “yes”.

  12. Aleksander Hansen Says:

    Hi.
    I’m trying to power a 5vdc motor which have a peak current of 500mA. So, my minimum base current will be 500 / 10 = 50mA during stall condition. The arduino is rated for around 40mA on the I/O-pins. I have my pin 9 supplying the base. Is it the resistor before the base that adjust for this so that my arduino will deliver less current?

    I am using the P2N2222AG transistor that followed my arduino experimentation kit.

    • michaelshiloh Says:

      You have it partially correct. The base resistor is to limit the current into the base, but not to protect the Arduino. The reason for limiting the base current is to avoid overheating the transistor unnecessarily.

      If you need 50mA of base current in order to driver your motor, then you can not do that with Arduino. If you limit the current to 40mA, then the transistor might not be in saturation, in which case a) it might overheat and b) the motor might not work properly.

      In practice, if it’s not a critical application, e.g. a toy that needs to work for only 2 minutes, you could limit the current to 40mA, use the Arduino, try to avoid stalling the motor, and see if everything works. It probably will.

      On the other hand, if it’s a critical application, for instance a prop that runs in a museum in a completely inaccessible space (hanging from the ceiling 40 meters above priceless art) that must work for a year, I would either switch to a much more powerful transistor or use another transistor to boost the Arduino current up to the 50mA.

  13. Michael Bawden Says:

    Hi ,thank you for the tutorial, it’s helped a lot. I’ve got an LED matrix and each row draws about 1.5A, I’m trying to use a TIP32 to drive the rows and TLC5917s as the sink controllers. When I work out my base resistor I keep getting about 20 ohms but this seems too low. I’m using an hfs of 10 and a vbe of 1.7. Do you think this is right? I’m driving the 8 TIPs with a UDN2982 so the base current is not a problem.
    Thanks
    Mike

    • michaelshiloh Says:

      Hi Mike,

      Step 3 above says:

      3. Find the worst case current gain during saturation.

      What did you use for that, and where did you find it?

  14. beyondallmeadows Says:

    I’m trying something similar, I have a trinket with a tip120 motor control script for a dc motor. But it’s not working I did the same as in the TIP120 motor driver for adrduino. Last thing I did is attach the trinket to usb, the tip120 to a 3.7 and a 9 volt battery and in any ways nothing happens.

    this is the code: but it seems fine to me?

    int randNumber; // variable to store the random value
    int motor = 0;

    void setup() {
    }
    void loop()
    {
    randomSeed(millis()); // sets millis() as seed
    randNumber = random(255); // random number from 0-255
    analogWrite(motor, randNumber); // outputs PWM signal
    delay(3500);
    }

    Lastly,
    How do I know wich resistor to use?
    I’m not good at the equations?

    Oh and it’s a trinket 5v.

    Help would be very nice!

    • michaelshiloh Says:

      Before you complicate things with Arduino, make sure the transistor is working (always break your problem down into small pieces that can be tested individually).

      If you connect the base of the transistor directly to 5V, does the motor turn on?

      If the motor does turn on, there must be a problem with the Arduino section.

      If the motor does not turn on, there must be a problem with the transistor section.

  15. Chris Says:

    This is the closest I have come to understanding what the heck I am looking for. Great stuff…

    I want to regulate a programmable constant current from a 30v battery pack using an Arduino. The device I am trying to control needs a constant voltage of ~30vdc (which is set by the battery pack voltage) and has a maximum current draw of 10 amps.

    I want the ability to regulate the current between IC max and min while keeping the ~30vdc using a NPN transistor and Arduino programing.

    1) Do you think this is doable?
    2) Need help in calculating the correct transistor

  16. Vitaly Dramaretskiy Says:

    Hi, if the maximum current that your microcontroller can supply is lower than required, then wouldn’t it be easier to use an IGBT transistor instead?

    • michaelshiloh Says:

      That’s a good question to which I don’t know the answer. I’ve actually never used an IGBT. I do often use a MOSFET, which also has an insulated gate, and a MOSFET is pretty simple to use in that the “base” resistor is not needed. However you still have to calculate maximum current carrying capacity, current gain, and power to dissipate.

      I usually see IGBTs used in higher voltage, like 3 phase 220V systems.

      Finally, the real purpose of this exercise was to understand how to calculate these factors for a given bipolar transistor. These notes grew out of my own notes while trying to figure this out, and I thought they might be useful to others.

  17. sshah Says:

    now i understand how to calculate the transistor comsuption.thanks a lot but i dont know how to joint it.can show to me the schematic diagram

  18. hapusr Says:

    That’s the best tutorial on how to use a transistor as a switch I have seen so far. Thanks a lot for your efforts!
    Could you consider writing “How to Use A Mosfet as a Switch” tutorial in a same manner? There are some pitfalls using mosfets as switches and I think community would benefit having a a tutorial on that.
    I have followed other tutorials on how to choose a proper mosfet and I think I did it right, but it turned out that the minimum load to activate mosfet was not taken into consideration (I was switching 2mA, 30V). It took me hours to figure out the problem.

    • michaelshiloh Says:

      That’s a great idea for a future tutorial. I’ll start outlining and researching that.

      Thanks for your suggestion!

  19. Sam Says:

    will you help me I did the electronics but don;t know the code…2 small DC motors spin at full speed in one direction for 1 second..but stop for rendomly1-3 seconds. Each of them work independently…I know code must be simple…I have written the start as below; please help

    int motor1 = 9;
    int motor2 = 8;
    void setup() {

    pinMode1(motor1, 255);
    pinMode2(motor2, 255);
    }

    • michaelshiloh Says:

      Hi Sam,

      You’re a little confused between pinMode() and digitalWrite(). I suggest you learn about those first before you try to implement the rest of your project. Start with the Arduino Blink tutorial at http://arduino.cc/en/Tutorial/Blink. From the software point of view, it makes no difference that you are turning on a motor and not an LED.

      When you understand the Blink tutorial and the difference between pinMode() and digitalWrite() and can turn your motors on and off, let me know and we’ll go from there.

      Good luck!

  20. Dave Says:

    Perfect tutorial !
    Finally fully understood how to select an appropriate transistor and a base resistor. Thanks a lot.

  21. Mike Says:

    Thanks for the tutorial.
    However, I have a question confusing me;

    When finding the Vbe, why did we select 1A for the first example ?

    And for the second example again we selected 1A, why ?

  22. Mike Says:

    equation is;

    (5-Vbe) / I = R

    If load current (collector current) gets smaller,Vbe will decrease so, for a given R, I will increase (over 20mA) right ?

    So, to be safe why don’t we take the 76mA collector current into consideration as the worst case ?

    This “I” selection really makes me confused

  23. Mwenda Says:

    Well done job. But would you please explain how you can tell whether the transistor circuit is a switch or amplifier. I will much appreciet! Kudos and thanks.

    • michaelshiloh Says:

      Hi,

      Thanks for the praise, and that’s a good question you ask.

      A transistor is generally thought of to operate in one of three regions: cutoff, which is essentially off, where no current flows through the collector; saturation, in which Vce is very small (and increasing the base current has little or no effect on the collector current); and finally the linear, or active region, where increasing the base current causes a proportional increase in the collector current, defined by the current gain beta.

      When a transistor is used as a switch, the designer attempts to keep it in either the cutoff region, when the switch is turned off, or the saturation region, when the switch is turned on. In fact the transistor has to go through the linear region when transitioning between the two, but the designer tries to make this transition as quickly as possible to minimize the time in the linear region.

      When a transistor is used as an amplifier, the designer tries to keep it in the linear region, because this is where accurate amplification takes place, where a signal is faithfully (linearly) scaled and replicated. In fact, one of the causes of distortion in an amplifier is that the transistor gets out of the linear region. Sometimes this is done intentionally: The so-called guitar “fuzz” effect is created by intentionally driving the transistor into saturation, which flattens the top of an audio sine wave and creates that “fuzzy” sound.

  24. GeorgeM Says:

    You are great! Thanks for this tutorial!
    Can you help me?
    I want to control brightness of a led strip (12V, 400mA) with my Arduino Uno Rev3 through PWM I/O pin. The base current should be approximately 20mA. Which transistor should I use? I have ULN2003 and TIP120. My power supply can deliver 12V, 3A. And the most important question: How to calculate the base resistor in this case, when I want to use transistor as amplifier? I’ll control brightness with remote control, but, it’s not important now.
    Thanks in advance!

    Scheme: http://snag.gy/gChZb.jpg

    • michaelshiloh Says:

      Glad you found my tutorial helpful.

      The ULN2003 is rated at only 500mA, which is not very much above the 400mA draw of the strip. I prefer a much wider safety margin.

      On the other hand, the TIP120 can handle 5A if it’s kept cool, which means it should handle 400mA easily without a heat sink, assuming it’s not in a closed hot box. I’d go with the TIP120.

      Perhaps I am missing something in your question. When controlling brightness using PWM, you are still using the transistor as a switch, rapidly switching between off and on. Why do you think the transistor is used as an amplifier?

      • GeorgeM Says:

        Sorry, I had the wrong approach. Now I understand how pwm works.
        I put ~1.9K Ohm base resistor. Works great!

        Thank you once again.

      • Michael Shiloh Says:

        Delighted to have been of assistance.

  25. Mahmoud Eldeeb Says:

    Thank you.

  26. Ashika Says:

    can u plz help me with the programming part

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: