I'm using the transistor (NPN bjt 2N4401) as a switch to light up 20 leds
(leds use 2v 20mA) (my power source is 6V).
How to calculate the base resistor?
Best Answer
20 LEDs times 20 mA each is 400 mA collector current.
Divide that by the current gain to get the minimum required base current. This transistor is specified to have a minimum gain of 100 at 150 mA, and 40 at 500 mA. We will have to go with the 40 value.
(400 mA)/40 = 10 mA, which is the minimum required base current to sustain the required collector current.
Now find the voltage across the base resistor. You say one end will be driven to 6 V. The other end will be the B-E drop above ground, let's say 750 mV. That leaves 5.25 V across the resistor.
By Ohm's law (5.25 V)/(10 mA) = 525 Ω. However, that results in the absolute minimum required base current. I'd use a 470 Ω resistor at least to get some margin.
Now use the above method to work backwards and see what the base current will be, then check that the digital output can support that. If not, you need a different topology with more gain.
In this case, I'd use a small N-channel FET that can be turned on fully with regular logic level voltages, like the IRLML2502. You drive its gate directly from the digital output without a resistor, it's source to ground, and its drain to the bottom ends of the LED strings.
If you want to conduct up to 150mA of current between node 1 and ground and only drop 1.0V (it's not a perfect switch) then you'll need to assume the current gain is typically 50. If you want a lower saturation voltage then the spec sheets says feed the base with 15mA i.e. the current gain has dropped to only 10 but saturation will only be 0.3V.
So assuming you are happy with 150mA while saturating the transistor to about 1V, you need to push \$\dfrac{150mA}{50} = 3mA\$ into the base.
The base voltage will need about 0.7V so the remainder (1.8V - 0.7V) needs to be across the resistor R1. Ohms law tells us that R = \$\dfrac{1.1V}{3mA} = 366.7\Omega\$.
So choose maybe a 360 ohm resistor.
If this isn't good enough for your needs look for an N channel MOSFET with low \$V_{GS(threshold)}\$ - something like 1V or less.
You will notice the different hFE values are for different conditions of operation. Ask yourself which of those is closest to the way you want the transistor to work when on. Looks like you want Vce to be very low (less wasted power) at Ic=200mA - so the closest match would be the value for Vce=1V, Ic=150mA which gives hFE>= 50 and therefore Ib=4ma so you COULD use 4ma as the base current for resistor calculation.
In fact it is better to turn on the transistor even harder (lower Vce, less power wasted in the transistor) so I would recommend Ib=10ma or even 20ma. The latter value will "saturate" the transistor (at which point Vce<=0.2V) and saturation is usually considered to occur for Ib=Ic/10 (or equivalently, hFE=10).
There is no point in increasing Ib any further and some CPUs would have trouble driving 20mA or even 10ma from an I/O pin (though an AVR should be OK).
Best Answer
20 LEDs times 20 mA each is 400 mA collector current.
Divide that by the current gain to get the minimum required base current. This transistor is specified to have a minimum gain of 100 at 150 mA, and 40 at 500 mA. We will have to go with the 40 value.
(400 mA)/40 = 10 mA, which is the minimum required base current to sustain the required collector current.
Now find the voltage across the base resistor. You say one end will be driven to 6 V. The other end will be the B-E drop above ground, let's say 750 mV. That leaves 5.25 V across the resistor.
By Ohm's law (5.25 V)/(10 mA) = 525 Ω. However, that results in the absolute minimum required base current. I'd use a 470 Ω resistor at least to get some margin.
Now use the above method to work backwards and see what the base current will be, then check that the digital output can support that. If not, you need a different topology with more gain.
In this case, I'd use a small N-channel FET that can be turned on fully with regular logic level voltages, like the IRLML2502. You drive its gate directly from the digital output without a resistor, it's source to ground, and its drain to the bottom ends of the LED strings.