LED Driver – Simple LED Driver with Transistor and GPIO

driverledraspberry pitransistors

I'm trying to figure out how to create a driver for a led from the GPIO pins of the raspberry and a NPN 2N3904 transistor. I am currently following what seems to be a good sparkfun tutorial. On this tutorial page is a video. So I tried to reproduce what is explained in this video (but English is not my native language) and here is what I understood at my level, it seems that there are several misunderstandings. I simulate the circuit in falstad:

enter image description here

First of all I would like to know if the value of 5mA (the sparkfun sir chosen 0.005A is 5mA) corresponds to the maximum value visible in the data sheet for Vbe (SAT):

(IC=50 mAdc, IB=5.0 mAdc) = 0.95V

, but the sparkfun sir chose 0.6V (which he does not name Vbe(SAT) but 'drop voltage as a diode'? So can we say that IbMAX = Vbe(SAT)MAX = 0.5mA (I also keep in mind that the GPIO port of the RPi seems to give 16mA maximum current).
More specifically, does the highest notation for Ib in this form (IC=50 mAdc, IB=5.0 mAdc) in the data sheet actually define Ib MAX? So can we say 'drop voltage as a diode' = datasheet Vbe(SAT) ?
which would mean the part of my calculation based on the maximum current of the GPIO port is wrong in this case.

enter image description here

For Vce(Sat) the sparkfun sir uses 0.3V that is 300mV which seems to correspond to the style sheet: (IC = 50 mAdc, IB = 5.0 mAdc) = 0.3V, but falstad displays 110.163mV ?

The LED used is noted 3.0V, 100mA so I deduce 3@0.1A
Now if we observe Ic its value is 191mA: 191mA < 100mA? FALSE ! So for me the LED is burning. Knowing that he chose a resistance of 330 Ohms in the end for Ib, that means that there is even a little more intensity which is worse. (unless this person specifies at a given time that it really depends on LED used and that I did not understand).

I want to create a program that can find Rc and Rb automatically and make the most of transistor and LED power. I also wish that this program indicates according to the limits of the LED , transistor, GPIO max current, Vcc used and their characteristics any possible errors.

I saw some people do a calculation for Rb with a factor of 10 to make sure the saturation mode of the transistor (Rb = (Vgpio-Vth) / (10 * (Ic / Beta)) if I remember), here the calculation is a little different for sparkfun (Vgpio-Vth / Ib) where Ib come from ? have a limit ? etc… but the transistor goes into saturation mode too.

Thank you for your help and your time.

Best Answer

When you use a BJT as a simple switch (and assume that it will always be saturated when on) the rules are very simple:

  1. Assume an hfe less than the minimum in the datasheet. In the data you showed, you could assume an hfe of say 10 which makes calculations very easy.
  2. Divide your target Ic by your assumed hfe to find the base current required.
  3. Look up your expected VCE(sat) from the datasheet at your target Ic.
  4. Look up the Vbe(sat) for your selected device.

Now lets look at a simplified circuit:

schematic

simulate this circuit – Schematic created using CircuitLab

Now you can calculate the value of R1 and R2 without considering any interaction, since we have assumed an hfe much less than the device provides.

Specification: You must specify the output (Ic), V+ and Vf of the LED required to proceed and here I'll use 20mA to drive a LED with a VF=3.2V from a 5V supply.

You need some details from the 2N3904 datasheet:

enter image description here

R1: ( Signal_Vin - Vbe(sat) ) / (Ic / 10)

For a Raspberry Pi Signal_Vin is about 3.3V (providing the current is not too high).

Vbe for the 2N3904 is about 0.95V worst case at 50mA Ic.

R1 --> ( 3.3 - 0.95 ) / ( 0.02 /10 ) --> 1175 Ohms

You could select either 1.2k Ohm or 1k Ohm and would still be well within the Raspberry Pi I/O current limit since we are drawing only about 2mA base current.
Note: If you had a 2N3904 with a Vbe of 0.65V (the minimum from the data sheet) then the base current would be slightly higher than calculated at 2.2mA for the 1.2k Ohm and 2.65mA for the 1k Ohm values calculated. This is still well within rating.
Note also: we did not consider what might be happening on the Collector side of the 2N3904 at all making the calculation easy.

R2 --> ( V+ - Vf - Vce(sat) ) / Ic

R2 --> ( 5 - 3.2 - 0.2 ) / 0.02 --> 80 Ohms

It would be simple to automate this process in either a spreadsheet or programtically. By choosing a target Ic/Ib you simplify the design process, so you need only consider a small number of details from the transistor datasheet. You will see the Ic/Ib=10 used in many processes when calculating switch characteristics, but you could chose any value you want providing you are ALWAYS assured it will be available in your configuration.

Update: this solution for driving LEDs is inefficient. If you use a 5V supply to provide the LED current through the switch Collector, there is power dissipated in the series resistor. You must allow for resistors large enough to cope with this dissipation.