8

I'm creating a circuit with an infrared led and a transistor connected to a gpio pin. I'm currently using 5v with a 40 ohm resistor (four 10 ohm resistors chained) but I'm not sure what resistor to put on the transistor.

The only transistor I have is the PN2222, will that work, and what resistor should I use?

0

3 Answers 3

13

There's not quite enough information in your question to give a definitive answer, but let's go through the design steps so that not only can you figure out this one, but you might be better equipped to solve the next transistor question that occurs to you.

Ohm's law

Ohm's law expresses the relationship among resistance (R), current (I) and voltage (V): V = I * R. So if we had a 5V supply and a 100 ohm resistor across it, the current I would be V/R = 5V/100ohm = 0.05A = 50mA. In your particular example, however, there is an IR diode as well. The circuit you describe with the IR diode and no transistor looks like this:

schematic

simulate this circuit – Schematic created using CircuitLab Note that I've shown your four 10 ohm resistors as a single 40 ohm resistor for clarity. In this circuit, the forward voltage is around 1.6V (which is under the the maximum VF of 1.7V per the datasheet) and the current is around 85mA. If you're using a different IR diode, you'll have to find and look up its datasheet to get the specifics for your part.

What's a transistor?

Yes, that's a rhetorical question. There are many ways to think about transistors, though. One useful way is to think of them as current amplifiers. The gain of a transistor, specified as hFE is roughly the ratio between the collector current IC and the base current IB. This a very simplistic way of thinking about it, but it's actually still useful in practice. That is IB * hFE = IC (approximately). The actual value of hFE varies, but a typical value is 100 or more.

The next thing to remember is IB + IC = IE; all of the current flows through the emitter. That's split between the base (tiny) and collector (most of the current). Direction of the current depends on whether the transistor is PNP or NPN. A 2N2222 is an NPN transistor, which is also the more common kind of bipolar transistor, so the rest of the discussion will assume NPN. Everything's mostly the same for a PNP transistor except that the currents and voltages are reversed with respect to an NPN transistor.

Using a transistor as a switch

What we often want from a transistor, as in your case, is for it to act like a switch. We want it "on" if the input voltage is high and "off" if the input voltage is low. There's a linear region in which transistors act, well, linearly as the current amplifier described above. That's useful if you're using a transistor as an analog signal amplifier. However, if we're looking for a binary on/off operation, we are not interested in the linear range. In fact, we seek to avoid it and operate the transistor solely in one of two regions: cut-off and saturation. The typical arrangement for driving this from a GPIO port (for any processor) looks like this:

schematic

simulate this circuit

Load resistor calculation

The resistor R2 represents the load. In your case the load is the IR diode, and whatever current limiting resistor might be needed. We can calculate that first.

The voltage and current for the IR diode, as mentioned above, we can get from the datasheet. The datasheet says that the maximum continuous current is 100mA (your datasheet may specify some other number). So we can start with that. We could either use a 3.3V Vcc or a 5V Vcc. Let's say 5V. The voltage across the diode will be less than 1.7V according to the datasheet, so 5V - 1.7V = 3.3V.

Next we look at the datasheet for a 2N2222 transistor and look up the at the VCE(sat) which means the voltage from the collector to the emitter when the transistor is in saturation (fully on). That is 0.3V at IC=150mA according to the datasheet which is close enough to our target of 100mA to use. So 3.3V - 0.3V = 3.0V. So now 3.0V/100mA = 30 ohms. You could use three of your 10 ohm resistors or a single 33 ohm resistor (which is the nearest standard value). However, I'd recommend increasing that a bit so that the IR diode isn't operating at its absolute limit. We can step up to the next standard value which is 39 ohms.

Input resistor calculation

Given that we have determined the collector current (around 100mA), we can calculate a minimum base current by using the transistor's hFE, if that's the only figure we have. So 100mA / 100 = 1mA. However, the value of hFE isn't really that useful when trying to drive the transistor into saturation. That's because hFE is only meaningful in the linear range of the device in which more base current translates into proportionally more collector current. Because we want to drive the transistor into saturation, which is a region in which more base current does NOT result in more collector current (because it's saturated), we have to add in a factor to make sure it's driven all the way to saturation. We can arbitrarily choose a value of 5 to 10 or so for that. So a 10x multiplier would give us 10mA in. The GPIO of the Pi is capable of delivering that, but let's do the calculation a different way instead.

The VBE(sat) is the base to emitter voltage when the transistor is in saturation. The data sheet gives a minimum value of 0.6V (which is very typical) and a maximum value of 1.5V at 15mA base current. If the Pi is supplying 3.3V when the pin is driven high, then the voltage across the input resistor is 3.3V - 0.6V = 2.7V. 2.7V / 15mA = 180 ohms which just happens to also be a standard value. The resulting circuit looks like this:

schematic

simulate this circuit

A few important static (DC) simulation results for this circuit are:

V(R1.nA)  = 739.5 mV
I(R1.nA) = -14.23 mA
I(R2.nA) = 83.48 mA
V(D1.nA)-V(D1.nK) = 1.656 V

We see that the input current is about 15mA, as calculated and the diode current is 83.48 mA (comfortably less than the maximum 100mA). The diode voltage drop is 1.656 V which is close to but less than the maximum 1.7V. For some additional safety margin, you could further increase the value of current-limiting resistor R2.

Note too, that this is close to the maximum specified current for the Pi, so you will have to program the particular port for that much current (current is specified from 2mA to 16mA).

Conclusion

The calculation of resistor values is a multistep process that uses data from datasheets for both the load and the transistor being used. It only requires some simple mathematics that can easily be done by hand. I hope that this answer not only answers your specific question but can also be employed by others wishing to do their own designs.

5
  • Would a 200 ohm resistor be acceptable?
    – user61421
    Commented Nov 22, 2017 at 16:16
  • Yes, 200 ohms would be fine. Typical resistors are rated for +/- 20% of the marked value so a 180 ohm resistor might translate into 200 ohms anyway.
    – Edward
    Commented Nov 22, 2017 at 16:23
  • @Edward this is an exemplary answer. Keep up the good work.
    – Darth Vader
    Commented Nov 24, 2017 at 11:39
  • For the Load Resistor calculation, how does this work when the load device is 5v? Commented Oct 26, 2020 at 9:51
  • @StringsOnFire: I'm not sure what you mean, but if it's significantly different from this question, why not ask a new one?
    – Edward
    Commented Oct 26, 2020 at 12:10
2

I assume that the LED is a 100mA type? And from the PN2222 data sheet we assume the gain of the transistor is 100 or so. This means the transistor current will need to be arround 1mA, and the GPIO voltage is 3.3v. Using Ohms law, 3.3/0.001 = 3k3, or if you take into account the bias voltage, (3.3-0.6)/0.001 2700 ohms, and this would be my choice.

To recap..

Current required / transistor gain = base current.

Resistor value = (drive voltage - 0.6) / base current

(Gain of a transistor is sometimes written as HFe)

Yours Simon M.

However if I was to start with a clean sheet, I'd look at using a MOSFET device.

3
  • The hFE is not relevant when the device is being used as a switch.
    – Edward
    Commented Nov 22, 2017 at 14:31
  • yes it is if you want to ensure saturation! Commented Nov 22, 2017 at 16:22
  • What I mean is that if you just divide the collector current by hFE to get the base current, the value will tend to be too low because hFE describes the linear region and what we want is beyond that, into saturation.
    – Edward
    Commented Nov 22, 2017 at 16:25
0

Find the data sheet for the transistor and it should have some info there on voltage regulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.