Skip to content

uC Meets BJT


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.

This article was submitted by Eric Wolf as part of the “Hobby parts for articles ” program. Eric receives a Modern Device Company Bare Bones Arduino Kit for this article.

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.

Posted in Microcontroller, Parts, Projects.

11 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. rsbohn says

    For driving 30mA the 2N3904 is more than enough. It took me awhile to sort out the math, but it does work out. I think I would solve for the minimum base current (0.3mA), then apply the derating factor (0.3mA x 5 = 1.5mA), then solve for Rbase (5V/1.5mA =~ 3.3Kohms). Nice article!

  2. Mark Rehorst says

    Low threshold mosfets that switch from off to on with a +5V logic level signal are available for about $1 each from digikey. The RFP30N06LE can switch 30A at up 60VDC. For many apps, the low on-resistance and relatively fast switching time mean that you don’t need a heatsink. is a schematic showing how I use it to switch 50us wide, 15A current spikes through a 5W LED. A slightly more complex circuit would speed up turn-off time by actively draining charge off the gate.

  3. dfowler says

    The question of which is best, BJT or FET is complex and depends on several factors. FETs can be easier to design with as they are voltage controlled devices. Other then the current required to defeat gate capacitance, no steady current is required to keep them on. BJT are current devices, they pass current between the collector and emitter pins as a function of the current flowing between the base and emitter. Continuous current is required to keep a BJT turned on.

    BJTs are usually cheaper then FETs and are very common in the gadgets you would be scrounging parts from as a starving electronics hobbyist.

  4. Eric says

    dfowler: Fantastic! Thanks for running such a great site. I do plan to write in the future if the inspiration strikes.

    Note in the lecture notes that adding a resistor to the emmiter leg of the transistor presents additions to the equation. The explanation above is for common configurations where there is no resistor there (which would work for just about any application where you are taking i/o from a microcontroller). This drastically simplified the equations necessary.

  5. dfowler says

    Chuck, the issue with high rate PWM for a transistor is turn on time vrs power dissipation. When you saturate a transistor the voltage drop across it is low which keeps the heat dissipation down for a given current. However, there is a switching time where current is flowing and the voltage drop across the transistor is high when the transistor is cycling from off to on/saturated and back to off. If the switching is slow, then your transistor starts heating up. Also there are some significant issues related to the time it takes to turn off a saturated transistor.

    These switching issues are also present with FETs and are a major source of inefficiency in switching power supplies.

  6. dfowler says

    Eric, this is great article and I hope to see more like this.

    I did a quick search for other online material about BJT and FETs and found this set of Lecture notes from OU.

  7. Eric says

    Also, thank you so much for the compliments everyone!

  8. Eric says

    Chuck: Admittedly, the 2N3904 isn’t the best of options for extremely high switching applications, but there are transistors that will handle it. It will do okay for moderate PWM response, but it is a VERY general purpose transistor, hence, the “cockroach” nickname. Really, it depends on the intended application. When the PWM frequencies increase, you start having to deal with interesting things like “slew rates”, which is the maximum rate of change of signal at any point in a circuit. The data sheet should have information on this.

    Arthur: Agreed, that would be a good application for a much higher current carrying application, but for our purposes here, it is very, very overkill. One of the reasons I might want to use something in that application, is for a PWM switching device, such as Chucks instance (JFET’s would work okay too). The switching capabilities are much better, but, you also have to remember your price per unit drastically increases when you add speacialized transistors where generalized ones will do. If I were carrying that much current on the same bus as a microcontroller, I would be concerned with how much noise it is likely to produce on the bus. For communication signals, and low power applications, the BPJ does just fine. I realize not everyone agrees on the same methodology, but that is my opinion, and nothing more.

  9. BrandonU says

    Great job on a well written and informative article. Understanding how and why components work allows you to find your own solutions to custom projects. Good job on clearly explaining key terms and ratings, and the math needed to choose the correct components!

  10. chuck says

    Thanks for the article! I have thought about doing this. How does this or other transistors handle high rate pwm?

  11. Arthur Benemann says

    Nice article,
    But if you want more power you culd use a mosfet like irfz44n(or a irf3205,better).For the above schematic with the leds
    you don’t need the resistor of base(gate),and the current from the colector(drain)to the emissor(source) could be as high as 50A(100A for the 3205)