Transistor Questions - Driving LED Arrays from GPIO

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm building a [relatively] low-tech game, and part of the functionality is bright, flashing lights, which I'm creating from an array of LEDs.

Several questions around the practical use of transistors arose when trying to design the system, which I'll enumerate below.

To add some additional context, I've already built the LED segments (they're a single strand of lights in a ring, six rings total). The Internet suggested that a combination of series and parallel connections was the appropriate choice, short of putting a resistor on every LED. I'm happy to have feedback here for future projects, but I'm not rebuilding the rings, which look fantastic when lit!

Last, I'm using TIP41A transistors because I happen to have them on-hand and no others at the moment - although if this is a poor choice I'm not against buying some other ones (especially since we're only talking 6).

1) When do I need to use a resistor on the output of a GPIO to a transistor? I connected one of the rings up to a transistor that was connected directly to GPIO and it worked fine with <2mA of current from the GPIO (I'm not suggesting I /don't/ need one, I just want to understand!)

2) (Follow-up to #1) What value base resistor should I use? Based on suggestions to use a resistor, I found http://www.kpsec.freeuk.com/trancirc.htm, which has a section on determining the base resistor. I used 5V as Vc, hFE 75 (DC hFE(max) per the TIP41 datasheet) and 100mA for Ic (the current through one of six rings is 80mA but I rounded up), which gave me 750 ohms. Is this reasonable? Or should I have used 18V as Vc? I assume that too high of a resistor would not drive the base to saturation, and too low could somehow cause too high of a current draw from the GPIO.

3) Must the load on a transistor always be on the Collector side, or could it instead be on the Emitter side?

4) If the load was 10x greater (say, 800mA) is the appropriate solution to use a logic-level gate MOSFET as markus_b suggested in an earlier post? If so, are there any other changes would I need to make to the circuit (other than eliminate the base resistor)?

5) n1ist suggested adding a 330nF and 100nF capacitor to my power supply, which I placed in the schematic. It seems odd to me to have the two caps on both sides of the 7805, but I honestly don't understand why the nF caps were suggested. Should I keep these? If so, do they need to be electrolytic or can they be ceramic disc?

Any additional comments and observations are very much welcome. If you've gotten this far, thank you for taking the time to read and consider my questions!

Attachment(s): 

The truly evil murder with style.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, Eagle is a pain to learn and use! Had to figure that out today just to put together the schematic! Seriously, whoever designed/implemented the group-move functionality is a sadist. Upside: it seemed to have all the parts I needed! And it's free...

The truly evil murder with style.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

that is a lot of questions.

1) always or you will potentially destroy the transistor at one point. You could perhaps use the internal pull-up resistor of the AVR, but if ever by accident the pin goes to output and high the transistor will be gone.

2) I think your tip41 is largely overrated.... 6Amp max cont is a big overkill if you only do 80mA.
The problem is that you have a low current gain transistor. you need to take into account tht the transistor only might have a gain of 30. I learned that if you wnt to use a transistor as a switch you need about 5 to 10 times overcurrent, so you end up with a current gain of 6 or less. So for your 100mA out you need to put 100/6=16mA into the base of the transistor.
You could consider using a FET, I sometimes use the FMMT617 that is a 3amp transistor and they have a gain of 300, so you get a factor of about 30 so for 100mA/30 = 3mA
that will save you a heavy power supply design....

I always use a divider resitor so GPIO -> R -> base with R parallel to base emmitter to ground. I want to have 0,8V over the base emmitter then the transistor is fully open. you need to check the currents running and then see what reistors are comming from that. You can omit the base emitter resistor, but it also assures that the leds will be off when the processor resets or if the GPIO pin is floating.

3) if you are smart, YES. You could opt for a PNP transistor switching the high side, but switching the low side is much easier, specially if you want to work with different supply voltages (as you do)

4) that would save base current that you normally throw away, but the transistor opted might also do. keep an eye on the power dissipation of that transistor though (also counts for a fet) if that is to high the transistor will also not survive on the longer run(it probably will survive development and die in the field... when dissipation is not excessive)

5) see datasheet. Keep in mind that long lines/wires are an inductor with resistance. A sudden surge of current will need to be regulated by the 7805 with no caps nearby that will become difficult and might even make a perfect oscillator of the 7805 ( seen that happen)

regards

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use ULN200? integrated Darlington pairs. Those have a gain of about 10000. Some versions (A or B) already have a base resistor integrated so actually you only need a 1:1 connection to IO pins. Each transistor can sink 0,5A.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A ULN200x type of transistor array would be ideal here, but i guess you want to do it with those tip41 that you have available.

Anyways, as stated before, you should look at the HFE minimum rating since you want it to be in saturation even at worst case conditions.

To ensure saturation it's better to, as previously stated, assume the HFE is lower and "overcurrent" it.
I think that doing 5-10 times the current is way to much though.

According to datasheet the minimum HFE @ 300mA is 30. I'll take a safety margin and calculate the resistor for an HFE of 15 or 20.
Note that for lower currents the HFE is higher.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forget bjt's, now that there are logic level mosfets. Much easier to design with, though I've never tried to make a linear amplifier with them.

The largest known prime number: 282589933-1

In my humble opinion, I'm always right. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vaxocentric wrote:

1) When do I need to use a resistor on the output of a GPIO to a transistor? I connected one of the rings up to a transistor that was connected directly to GPIO and it worked fine with <2mA of current from the GPIO (I'm not suggesting I /don't/ need one, I just want to understand!)

2) (Follow-up to #1) What value base resistor should I use? Based on suggestions to use a resistor, I found http://www.kpsec.freeuk.com/trancirc.htm, which has a section on determining the base resistor. I used 5V as Vc, hFE 75 (DC hFE(max) per the TIP41 datasheet) and 100mA for Ic (the current through one of six rings is 80mA but I rounded up), which gave me 750 ohms. Is this reasonable? Or should I have used 18V as Vc? I assume that too high of a resistor would not drive the base to saturation, and too low could somehow cause too high of a current draw from the GPIO.

3) Must the load on a transistor always be on the Collector side, or could it instead be on the Emitter side?

4) If the load was 10x greater (say, 800mA) is the appropriate solution to use a logic-level gate MOSFET as markus_b suggested in an earlier post? If so, are there any other changes would I need to make to the circuit (other than eliminate the base resistor)?

5) n1ist suggested adding a 330nF and 100nF capacitor to my power supply, which I placed in the schematic. It seems odd to me to have the two caps on both sides of the 7805, but I honestly don't understand why the nF caps were suggested. Should I keep these? If so, do they need to be electrolytic or can they be ceramic disc?

I will try to give some answers.

1) You always need the resistor. Not to protect the transistor, it can take several amperes base current without damage. But to protect the AVR. A transistor base i just a diode, voltage over it probably never exceeds 1V. So for the GPIO its a shortcut. You may burn the GPIO or even kill the AVR by overheating. If you want to continue with TIP41, calculate the resistors to give maximum allowable base current according to datasheet for AVR. But darlingtons or power mosfet may be a better choice. But even then, keep the resistors, a value of 1k or more should fit.

2) Most AVR GPIO's are specified to 40mA max, but the entire chip may have a max. of say 75mA. Look at the datasheet. Dont ever put more than 5.5V on any AVR pin except from reset pin. You will kill the AVR with 18V.

3) Its perfectly possible to have the load on the emitter side. But voltage emitter to ground will be limited to between 4 and 4.5V no matter collector voltage. But in this configuration you dont need the resistor on base.

4) A logic level mosfet is the perfect solution, but keep the resistor to limit transient current. A mosfet has a fairly big "builtin" gate capacitor that the GPIO needs to charge/discharge.

5) Reason is that electrolytic capacitors mostly have a fairly high inductance. They may form an LC oscillator and possibly toast the AVR with high ripple voltage. Ceramic capacitors cures. Personally I would prefer to use two 1uF ceramic capacitors across in/out terminals of the 7805 and perhaps not have an electrolyte on the output. Wires between 7805 and capacitors should be as short as possible.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
1) When do I need to use a resistor on the output of a GPIO to a transistor? I connected one of the rings up to a transistor that was connected directly to GPIO and it worked fine with <2mA of current from the GPIO (I'm not suggesting I /don't/ need one, I just want to understand!)

When you use an NPN transistor it needs a base resistor. The voltage drop across the base resistor determines the base current, which then determines the collector current. Putting an NPN transistor's base directly on the I/O pin is almost like loading the I/O pin with a short to ground.
When you use an NFet transistor as the switch it doesn't need the gate resistor connected to the I/O pin. NFets are voltage driven, not current driven devices.
One might want to put a high value, (say 10K) resistor from the NFet gate to ground. This is because when the circuit first powers up the I/O pin is configured as an input, until your software kicks in and defines it as an output and sets its level. When it is an input the resistor holds the pin at ground, and keeps the NFet from being biased into the linear range, where the current drawn and heat produced could destroy it. When the NFet is not fully ON, its internal resistance is much higher than when it is fully On, hence it generates a lot of heat.

Quote:
3) Must the load on a transistor always be on the Collector side, or could it instead be on the Emitter side?

When using the transistor as a simple switch, either fully on or fully off, and using an NPN transistor, leave the load in on the collector.

Quote:
short of putting a resistor on every LED.

Your circuit is fine in this regard. You have a series resistor in series with each "string" of LEDs. This sets the current flow for that particular string. The same current then flows through each LED in that string.
If you put Leds in parallel with each other then their forward voltages will not be quite the same. One will draw more current than the other. This gives different intensities from each of the two Leds, but more importantly the one drawing more current may then be out of spec.
Having a resistor in each string, and the strings with the resistor in parallel, lets the resistor do "load balancing" across the multiple stirngs. The actual voltage across just the Led strings may be a bit different from string to string, but the resistor makes up for this small difference, and they can all be driven from the same voltage source.

You may wish to check the value of your resistor in each Led string.
Look for the forward voltage drop of the LEDs you are using. If, for example, it was 1.2 V, then the voltage drop across the string would be 5 x 1.2 V = 6 V. Ignore the V drop across the transistor for now, just assume you had the Led strings tied to ground. The voltage across the series resistor for a given string is 18 V - 6 V = 12 V. The current through the resistor, and hence through that string of Leds, is I=V/R --> 12 / 100 = 120 mA. This is the current htat EACH Led ahs flowing through it.

There are LEDs which can handle 120 mA. Most small, general purpose Leds use 10 - 20 mA, or these days even less. Make sure you have the corrent Leds if you want to drive them at that level. Otherwise you will need to recalc your series resistor value.

Quote:
two caps on both sides of the 7805

One often ahs multiple caps in parallel. At first glance one wonders why one needs a 0.1 uF cap in parallel to a 220 uF cap, for example; and the answer is that they have different response characteristics to transients on the line. Check the Data Sheet for the 7805 and look at the recommended usage schematic(s).

Good luck with yoru project.

Post a photo when it is done!

JC

Edit: Cross post with Peter...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Torby wrote:
Forget bjt's, now that there are logic level mosfets. Much easier to design with, though I've never tried to make a linear amplifier with them.

Agree completely. For the OP, here's a nice logic-level fet that is physically small but not too small, and has very nice characteristics. And cheap!

These are the same device, but -1G has long leads and -35G has short leads.

http://www.mouser.com/ProductDet...

http://www.mouser.com/ProductDet...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Everyone, this helps tremendously! Putting a resistor on the base of a transistor makes total sense now. I saw some Darlington Pair ICs that caught my eye because they were 7 to a package, but based on everyone's recommendations I think I'll spec out MOSFETs (the part recommendations really help).

Likewise, the ceramic caps on the regulator makes sense now.

Question: If I want to do simple PWM @ ~25kHz would that affect my transistor choice (between the MOSFETs recommended, Darlington Pairs, or TIP41? I came up with 25kHz based on having ~256 levels in order to apply a gamma correction and get ~32 perceived levels. So to avoid seeing blinking at the minimum intensity (1/256) I'm thinking it'd need to be around 100Hz, so 256*100 = 25.6kHz.

FYI, the LEDs I'm using from Adafruit have a voltage drop of 3.2-3.8V @20mA, so with 5 I calculate 16-19V drop. The current going in to the ring is about 80mA, yielding ~27mA that's hitting each LED, and since they're rated at 30mA I /think/ I'm safe.

I will definitely post completed project schematics, code, photos, and hopefully video! Deadline for me is last week of May for a festival, so I'm scrambling to put it all together in time for me to add extra features!

The truly evil murder with style.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also, is that the appropriate connection for the ISP header so that I can re-program the chip without removing it if I need to? Are there any caveats to also using the MOSI, MISO, or SCK pins for part of my application? I'm just thinking about current flow to/from the ISP programmer to whatever else I hook those pins up to (e.g., PIR motion detector, buttons with debounce cap and pull-up resistor, etc.)

Or should I just dedicate those MCU pins to ISP use? (Not a critical issue.)

The truly evil murder with style.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are making an assumption that the LED's will all have the exact Vf, so they will share the current equally. This might NOT be the case, so you might want to keep that in mind.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quickly switching (High frequency PWM) mosfets could be tricky if you are driving them directly from GPIO pin.
Since they work on voltage any stray capacitance on the gate will eat up tons of current at switching.

Why do you need such a high frequency ? Why not settle for a couple of hundred hertz ?
I think you got the calculation wrong, why did you multiply 256 by 100 ? If you want 100Hz refresh rate just pwm at 100Hz.

Also, if you are planning on having RGB leds or something like that. Note that different led types (colors) have different Vf.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:

MOSFET NFET IPAK 30V 44A 9.6MOHM

Is this an alternative for a 100R series resistor + bjt at 80mA?

kk6gm wrote:
And cheap!

Reassuming, OP needs 6 constant current drivers rated at 80mA each, I would suggest a SOT-23 bjt x 6 (BC847C-single or BCV27-pair). The price is 1,2 cent for BC847C transistor when you are buying 100 of those.

I suppose this is not a mass product but a single quantity so the unit price really does not matter.

vaxocentric wrote:
because they were 7 to a package,

or 8. ULN200X (0,2$) is a 0,5A overkill - it is not a current source but you can pretend it is a low end source when it has a resistor at its collector. But it is as inaccurate as a unipolar transistor in this role.

It is really hard to make an accurate constant current source with unipolar transistors. It is doable, but requires much more circuitry. With bipolar that is much easier/cheaper.

For a high accuracy current source (~1% current matching) there are also bipolar current mirrors available(two matched transistors in one package). Those cost about twice as much as regular transistors, but are not very popular (difficult to buy at low quantities cheaply).

If you do not have the experience/knowledge in analog electronics or this is a single quantity - buy an integrated current source.

No RSTDISBL, no fun!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vaxocentric wrote:
Question: If I want to do simple PWM @ ~25kHz would that affect my transistor choice (between the MOSFETs recommended, Darlington Pairs, or TIP41? I came up with 25kHz based on having ~256 levels in order to apply a gamma correction and get ~32 perceived levels. So to avoid seeing blinking at the minimum intensity (1/256) I'm thinking it'd need to be around 100Hz, so 256*100 = 25.6kHz.

25kHz!! All you need is about 50Hz. That will get you well past the fusion flicker threshold. See link here:

http://en.wikipedia.org/wiki/Flicker_fusion_threshold

You control the brightness by varying the duty cycle.

Greg

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

vaxocentric wrote:
Also, is that the appropriate connection for the ISP header so that I can re-program the chip without removing it if I need to? Are there any caveats to also using the MOSI, MISO, or SCK pins for part of my application? I'm just thinking about current flow to/from the ISP programmer to whatever else I hook those pins up to (e.g., PIR motion detector, buttons with debounce cap and pull-up resistor, etc.)

Or should I just dedicate those MCU pins to ISP use? (Not a critical issue.)


Some remarks about several topics:

- MOSI, MISO, or SCK: You can use these pins for other stuff provided that the other stuff does not impose too much load and that the other stuff survives the impulse train when programming. For example you can share these pins with the PWM output. Your lights will flash like crazy while programming, but I think you can live with that.

- PWM speed: You don't need more that 100Hz. You will also not get much more if you do software PWM due to CPU load. I suspect you confuse the PWM speed with the clock you need to drive the PWM timer. If you have 100Hz PWM and use 256bits resolution, then you need a 25kHz clock to drive the PWM. If you do PWM in software and use the internal 8Mhz clock you'll have around 300 cycles per tick for the software PWM.

- Pins used: I'd connect the PWM to the OCxx pins on portb. You may not use it, but this allows you to use the hardware PWM for three PWM channels. If you do all of it in software it does not matter, but at least you can play with the PWM hardware.

I understand your pain with Eagle, when through it too. But most of it is to get used to the job creating a proper schematics and PCB. This is not easy and there is a learning curve.

As inspiration I include a small RGB-driver I've done with an 8-pin AVR to drive a three channel LED strip.

Attachment(s): 

Markus

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Woa! Now that's a nice FET for 19 cents! I've been using a 3A one in a SOT-89 pack for a while, but I think I'll change.

kk6gm wrote:
Torby wrote:
Forget bjt's, now that there are logic level mosfets. Much easier to design with, though I've never tried to make a linear amplifier with them.

Agree completely. For the OP, here's a nice logic-level fet that is physically small but not too small, and has very nice characteristics. And cheap!

These are the same device, but -1G has long leads and -35G has short leads.

http://www.mouser.com/ProductDet...

http://www.mouser.com/ProductDet...

The largest known prime number: 282589933-1

In my humble opinion, I'm always right. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brutte wrote:
kk6gm wrote:

MOSFET NFET IPAK 30V 44A 9.6MOHM

Is this an alternative for a 100R series resistor + bjt at 80mA?

The OP is now planning on using a TO-220. I offered an alternative in the same realm. There are certainly SMT devices that would do the job more cheaply and more compactly. I use the BSS138 a lot myself. If using a BJT, one can then also decide between an external bias resistor or a "digital transistor" with internal biasing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
All you need is about 50Hz.

Note that if you do 50Hz when you have the Light in the edge of your view, then you will see it flickering, specially if you also have 50Hz lights on. If you use 100Hz that is gone, so better stick to 100Hz. I did a 50Hz flickering LED once and when it was just in the edge of your sight it was terribly annoying, when you then moved to get it fully into sight the view was normal.
I do not know if the same thing happens in a 60Hz environment though, but it is one to keep an eye on...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The frequency needs to be higher near the edge of vision, but of course even that depends on how sensitive a person is to see the flicker. I'd say 100Hz or as fast as sensible, but there is a limit how fast those big fat transistors can be switched on and off anyway with pure resistance between AVR and base. Sometimes there is a speedup capacitor of few picofarads over the resistor.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KK6GM: I'm going with the NTD4963N-1G - thanks so much for pointing me in this direction! Performance looks great, and price is even better!

ItsMike: Thanks for calling out the different voltages on the RGB LEDs - that's something I didn't even think of. Will keep that in mind for future projects.

Markus_b: Doh! You caught my mistake. I was indeed calculating clock speed, when I meant to calculate refresh. Thanks to you and everyone calling me out on this. I'll stick with 100Hz refresh to start with and tweak as necessary! Good to know that I can use the MOSI/MISO/SCK pins. I connected the LEDs on my STK500 to PORTB and saw what you're referring to. I'm not certain how to use the OCx pin as part of the PWM (I was going to implement via timer interrupt) but I'll do some digging and I'm sure I'll figure it out. Thanks for the direction!

The truly evil murder with style.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A box of those was at my doorstep yesterday. Actually, I got the smd variety. Been paying like a dollar each for the little fets I'd been using.

The largest known prime number: 282589933-1

In my humble opinion, I'm always right.