Pages: [1]   Go Down
Author Topic: Driving an LED from an Arduino pin/TTL pin *the right way*.  (Read 2002 times)
0 Members and 1 Guest are viewing this topic.
Show Your Work
Offline Offline
Edison Member
*
Karma: 14
Posts: 1100
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The normal accepted hobbiest way of driving a small LED is to drive it directly from a logic IC pin.  I've heard over and over that this is not the right way to do it and I can understand why.  The Arduino has a reasonably low maximum drive current for the whole package and a lot of TTL packages have poor drive current.  But lighting LEDs this way is still done.  Now, what is the right way?  Say that I have a pin and want to light a 20mA LED on logical 1 and draw 1mA of current from the pin.  The obvious way (to me) is to connect the LED through an NPN transistor, right?  Connect the output pin through a 5K resistor to the base of the transistor (1mA current), connect +5V to the LED, through a 250 ohm resistor (20mA to the LED) to the transistor collector, connect transistor emitter to ground.  Does that sound right?  If so, what is a good transistor choice?  How do I know that 1mA on the base will give me at least 20mA from the emitter to the collector.  How do I know the transistor is open and not operating in the ohmic region?  Thank you.  I am not too familiar with discrete transistors.
« Last Edit: December 13, 2012, 12:43:03 pm by JoeN » Logged

I have only come here seeking knowledge. Things they would not teach me of in college.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34246
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've heard over and over that this is not the right way to do it and I can understand why.
I can't. There is nothing wrong with driving an LED and its resistor directly from a processor pin. It is done all the time. As long as the current capacity of the pin is up to the job.

Quote
The Arduino has a reasonably low maximum drive current
No, in comparison with other processors it is very high.

Quote
a lot of TTL packages have poor drive current.
Source rating yes but they are much better at sinking.

Quote
The obvious way (to me) is to connect the LED through an NPN transistor, right?  Connect the output pin through a 5K resistor to the base of the transistor (1mA current), connect +5V to the LED, through a 250 ohm resistor (20mA to the LED) to the transistor collector, connect transistor emitter to ground.  Does that sound right?
Yes. Although you won't find many 5K resistors use 4K7.

Quote
If so, what is a good transistor choice?
The cheapest transistor you can get. Sights like Farnell allow you to sort them by price.

Quote
How do I know that 1mA on the base will give me at least 20mA from the emitter to the collector.
You look at the data sheet and see that the gain of the transistor you are using is at least 20.

Quote
I am not too familiar with discrete transistors.
Yes I know that from this:-
Quote
How do I know the transistor is open and not operating in the ohmic region?
No such region, do you mean saturated? It is down to the gain of the transistor.
Logged

Show Your Work
Offline Offline
Edison Member
*
Karma: 14
Posts: 1100
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know the ATMega328P is a pretty good driver compared to most microprocessors, but it is not a driver.   The maximum package source current is 200mA. That is all I was getting at.  In this case, really I want to drive LEDs (correctly)from a TTL package.  Since I want to turn on an LED for logic 1, the fact that TTL has good sinking current is not meaningful as far as I can see, but maybe I am having tunnel vision here in some way.

Quote
How do I know the transistor is open and not operating in the ohmic region?
No such region, do you mean saturated? It is down to the gain of the transistor.

Help me on that.   How do I know if I saturated the transistor to keep the emitter to collector resistance minimal?

Can someone recommend a good NPN transistor for my scenario?  1mA to the base must allow 20mA from emitter to collector while saturating the transistor.  Correctly stated?

Thank you all.
Logged

I have only come here seeking knowledge. Things they would not teach me of in college.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can someone recommend a good NPN transistor for my scenario?  1mA to the base must allow 20mA from emitter to collector while saturating the transistor.  Correctly stated?

A couple of popular low(ish) current npn transistors:

2N3904
2N2222A

For driving simple 20ma leds I like the 2N7000 mosfet transistors, no series base (gate) resistor needed and they are cheap.

Lefty
Logged

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 159
Posts: 2916
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's far too much trouble to wire up an NPN inverter for every single Led. Also, newer Leds
don't need anywhere near 20 mA to get good output, more like 4-5 mA on many. If you're
worried about current drive off the I/O pins, just buy some high-output Leds.

Quote
I've heard over and over that this is not the right way to do it and I can understand why.
Duh. The only thing right about *the right way* is don't exceed the current source/sink rating
for the I/O pins.
Logged

Show Your Work
Offline Offline
Edison Member
*
Karma: 14
Posts: 1100
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can someone recommend a good NPN transistor for my scenario?  1mA to the base must allow 20mA from emitter to collector while saturating the transistor.  Correctly stated?

A couple of popular low(ish) current npn transistors:

2N3904
2N2222A

For driving simple 20ma leds I like the 2N7000 mosfet transistors, no series base (gate) resistor needed and they are cheap.

Lefty

OK, I know I have all of these in my Jameco transistor kit.   100 of the 2N3904s, and only 10 each of the other two.  I will try that out.  I especially like the MOSFET idea, thank you.

Edit:  The 2N7000 looks like a great part and Jameco has free shipping this week on orders of $25 or more so I picked up five hundred of them, five cents each (and some anti-static foam which normally really kills me on the shipping cost).  I guess I am set for life.
« Last Edit: December 13, 2012, 01:57:04 pm by JoeN » Logged

I have only come here seeking knowledge. Things they would not teach me of in college.

Show Your Work
Offline Offline
Edison Member
*
Karma: 14
Posts: 1100
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I should also note, for my silly current project I am actually working with 74LS TTL packages again and I want LEDs (on 330 ohm resistors) on some of the inputs and outputs.  An issue I have is that the input LEDs are obviously dimmer than the inputs so clearly the package can't supply 15.15mA and probably I am pulling it at max which can't be good, hence I think I need the transistors.  I like the idea of using these MOSFETs, it's a part I have not used so far.
Logged

I have only come here seeking knowledge. Things they would not teach me of in college.

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5535
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The normal accepted hobbiest way of driving a small LED is to drive it directly from a logic IC pin.

If you just want it to light up an LED then there's nothing wrong with it.

I've heard over and over that this is not the right way to do it

If you've heard that then it's probably because they weren't use a resistor to limit the current properly or something like that.

The Arduino has a reasonably low maximum drive current

It actually has far too much current for normal LEDs (which enter their death zone at 20mA).

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5535
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know the ATMega328P is a pretty good driver compared to most microprocessors, but it is not a driver.   The maximum package source current is 200mA.

Well...aiming at exactly 20mA with just a resistor is a bad idea. Much better to aim at the 12-15mA mark.

In that case 200mA is 13-16 LEDs. About as many as you can physically connect to an Arduino.

(Of course this requires the user understands the limitations, which isn't guaranteed ... remember the guy who connected 60 LEDs to a single Arduino pin then posted about them being a bit dimmer than he would have liked?)
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Show Your Work
Offline Offline
Edison Member
*
Karma: 14
Posts: 1100
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, I am aiming at 15.15mA with a 330 ohm resistor and the white LEDs I use are BRIGHT.  Very nice.  Except the one coming off the 74LS04 package which doesn't make the grade.
Logged

I have only come here seeking knowledge. Things they would not teach me of in college.

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 146
Posts: 5535
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can someone recommend a good NPN transistor for my scenario?  1mA to the base must allow 20mA from emitter to collector while saturating the transistor.  Correctly stated?

Absolutely any common NPN transistor will do that (TO-92 size).

Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34246
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
200mA. That is all I was getting at.  In this case, really I want to drive LEDs (correctly)from a TTL package.  Since I want to turn on an LED for logic 1, the fact that TTL has good sinking current is not meaningful as far as I can see, but maybe I am having tunnel vision here in some way.
Yes you are totally missing the point. Why the fixation with a logic one turning it on. Anyway if you use an open colllector buffer and power the LED by sinking a logic one will turn it on. Try an 74LS05.

Quote
How do I know if I saturated the transistor to keep the emitter to collector resistance minimal
Back to front. When the collector emitter voltage is at a minimum you have saturated the transistor. You saturate a transistor by having more current through the base than the collector current divided by the gain.
Logged

Pages: [1]   Go Up
Jump to: