Controlling RGB Led using PWM of Microcontroller

John P

Joined Oct 14, 2008
2,025
The two subjects are not mutually exclusive.

Keeping in mind I have another imaginary unspecified circuit that can sink or source billions of amps without any voltage drop at all...

It is the TS who stated his intent to use an LM317 as his constant current source. That would not be my first choice for this application and I stated my reasons for this.
Actually I was the one who first mentioned the LM317 and electronic (as opposed to resistive) current control generally. But then BramLabs came back saying he'd considered it and that the LM317 needs a large capacitor (I'm dubious) and you said there'd be too much voltage drop, which is harder to answer. I don't think the LM317 is the way to go here.

However, the two-transistor controller still seems to me like the best approach. I said that it could double as a switch for the processor to control the circuit, and I just realized that it also makes all this talk about saturation behavior irrelevant--the power transistor would be in its linear region anyway, so you don't need to figure anything out, as long as you can meet the worst-case specs. Though with only 20mA of load current, there's no need for much base drive.
 

Thread Starter

BramLabs

Joined Nov 21, 2013
98
I hope that this will give some idea.
Oh i know that circuit. It's called current limiter. Okay sir, thank you for your idea !
Thanks for replying sir !

Your calculations is fine, you need to adjust the values of power dissipation, in the situation of 100% duty cycle, the watts of resistor will be as 5 times of calculation, but you used the pwm here, so maybe you can take 2 or 3 times to try
Okay sir, big thanks for your help !


Actually I was the one who first mentioned the LM317 and electronic (as opposed to resistive) current control generally. But then BramLabs came back saying he'd considered it and that the LM317 needs a large capacitor (I'm dubious) and you said there'd be too much voltage drop, which is harder to answer. I don't think the LM317 is the way to go here.

However, the two-transistor controller still seems to me like the best approach. I said that it could double as a switch for the processor to control the circuit, and I just realized that it also makes all this talk about saturation behavior irrelevant--the power transistor would be in its linear region anyway, so you don't need to figure anything out, as long as you can meet the worst-case specs. Though with only 20mA of load current, there's no need for much base drive.
You mean the "two-transistor controller" is a current limiter ?
I don't know the 'two-transistor controller' circuit you mean. ahahaha...
Sorry sir ! But thanks for your information
 

John P

Joined Oct 14, 2008
2,025
Yes, when I first mentioned it I said "a constant-current circuit in place of the resistor and transistor. You could use the 2-transistor type" which I thought explained it. If you need to look it up, it's in any number of places:
https://en.wikipedia.org/wiki/Current_limiting

Look under the heading "Single power-supply circuits". And what do you know, it says "Due to its simplicity, this circuit is sometimes used as a current source for high-power LEDs"!

To provide a switching function, you'd want to drive R1 from a processor pin, not directly off the power supply.
 

dannyf

Joined Sep 13, 2015
2,197
I hope that this will give some idea.
The schematic is more complicated than necessary: it can be done with two transistors / mosfets.

1) put the two transistors current sink to the bottom, and connect the led string to its collector / drain;
2) find the resistor that connects the collector of one transistor to the base of the other transistor and reroute it to your mcu pin.
3) when the mcu pin goes high, the led is turned on at a set current level; when the mcu goes off, the led is turned off.
 

Thread Starter

BramLabs

Joined Nov 21, 2013
98
I'm not sure why you didn't use the 3.2V(or 3.0V) Red Led?
As i already attached in my first post as a thread starter, i'm using SMD 5050 RGB LED.
Or you can see here sir :

data.PNG

I'm just using a 'middle' value of the Vf.
Btw, thank you for your concern and your help sir !
I really appreciate it !

Yes, when I first mentioned it I said "a constant-current circuit in place of the resistor and transistor. You could use the 2-transistor type" which I thought explained it. If you need to look it up, it's in any number of places:
https://en.wikipedia.org/wiki/Current_limiting

Look under the heading "Single power-supply circuits". And what do you know, it says "Due to its simplicity, this circuit is sometimes used as a current source for high-power LEDs"!

To provide a switching function, you'd want to drive R1 from a processor pin, not directly off the power supply.
So, you mean a circuit like this sir ?

CC.PNG

Thank you very much sir for your help !

The schematic is more complicated than necessary: it can be done with two transistors / mosfets.

1) put the two transistors current sink to the bottom, and connect the led string to its collector / drain;
2) find the resistor that connects the collector of one transistor to the base of the other transistor and reroute it to your mcu pin.
3) when the mcu pin goes high, the led is turned on at a set current level; when the mcu goes off, the led is turned off.
Okay sir, i'll try it later ^^
Big thanks to you :)
 
Last edited:

Thread Starter

BramLabs

Joined Nov 21, 2013
98
Yes, when I first mentioned it I said "a constant-current circuit in place of the resistor and transistor. You could use the 2-transistor type" which I thought explained it. If you need to look it up, it's in any number of places:
https://en.wikipedia.org/wiki/Current_limiting

Look under the heading "Single power-supply circuits". And what do you know, it says "Due to its simplicity, this circuit is sometimes used as a current source for high-power LEDs"!

To provide a switching function, you'd want to drive R1 from a processor pin, not directly off the power supply.
Oh yeah sir, i want to make a difference approach. I'm using a 5 Volt supply that can gives current up to 1 A.
So, my circuit would be like this :

CC_2.PNG

So this is my calculation for my 11 PARALLEL RED LED RGB :
1). For 1 LED RGB : Vf = 2.1v ; If = 20mA
I just need the information of If = 20 mA at first.
So for 11 parallel red led RGB, my Ic would be = 11*20mA = 0.22 A
2). So my Rsense would be :
0.65 ( In my case, i use Vbe 0.65 as written in google wikipedia for current limiter ) / 0.22 A = 2.9545 ohm ( i will use at least more than 2.9545 ohm, because in datasheet of my transistor the Vbe would vary from 0.6V to 1.2V, for the record, i'm not using 2N2222A, but instead the SMD's version of it, MMBT2222A )
3). For my R1, because i already know my Ic, my Ib based on datasheet ( i'm using hfe = 10 ), Ib = Ic/hfe = 0.22/10 = 22mA
So my R1 would be = 3.3v - 0.65v (Vbe of Q1) - 0.65v (Voltage at Rsense, in this case also 0.65v) / 22mA = 90 ohm
4). For my R3, will be :
V at parallel node would be 5v - 0.2v (saturation region of Q1) - 0.65 (voltage drop across the Rsense) = 4.15v
So the R3 would be : 4.15v / 20 mA = 207.5 ohm

For the power dissipation.
If short circuit happened at the load :
5). The transistor Q1 needs at least : P = V*I = (5v - 0.65v)*0.22A = 0.957 Watt ( in my case, my transistor only can handle up to 0.25 Watt ).
I have a problem with it. Can you suggest me sir, should i use a 5 branch of parallel circuit of load so that my current would be less than what is needed ( so my LED would be 10, not 11 and at every branch, there will be 2 RED LED RGB in series every branch ) instead of 11 parallel circuit ?

And also how about my calculation ?
Am i correct ?

Thank you very much sir for your help !
 

Attachments

Last edited:

ebeowulf17

Joined Aug 12, 2014
3,307
If you want to run the LEDs in parallel, you'll need to put a resistor in series with each LED, and then hook those 11 series combos up in parallel.

If you simply connect the LEDs in parallel without corresponding resistors, more current will flow through some LEDs than others. This can lead to premature LED failure and cascading failures.
 

Thread Starter

BramLabs

Joined Nov 21, 2013
98
If you want to run the LEDs in parallel, you'll need to put a resistor in series with each LED, and then hook those 11 series combos up in parallel.

If you simply connect the LEDs in parallel without corresponding resistors, more current will flow through some LEDs than others. This can lead to premature LED failure and cascading failures.
Oh yeah i forgot sir.
Let me correct it first !
 

John P

Joined Oct 14, 2008
2,025
O BramLabs, please reconsider. Your shift to a 5V supply makes the circuit much less fun, and there's no point in using electronic current control. You'll need one resistor per LED, and each one will simply control the current through that part of the circuit. You only need one transistor, and it has to switch the current for 11 LEDs in parallel, not a single string of them in series. That would mean you do need to think about saturation current etc, but you can deal with that by using a logic MOSFET instead. The efficiency of your overall circuit will be a lot less, because now each LED will get less than half the voltage from the supply, and the rest will be lost as heat. How can this be an improvement? If you stick with this design, I'm not going to help any more.

p.s. The answer to the question "So, you mean a circuit like this sir?" is yes. But my answer to "So, my circuit would be like this" is "No, don't do it".
 

Thread Starter

BramLabs

Joined Nov 21, 2013
98
O BramLabs, please reconsider. Your shift to a 5V supply makes the circuit much less fun, and there's no point in using electronic current control. You'll need one resistor per LED, and each one will simply control the current through that part of the circuit. You only need one transistor, and it has to switch the current for 11 LEDs in parallel, not a single string of them in series. That would mean you do need to think about saturation current etc, but you can deal with that by using a logic MOSFET instead. The efficiency of your overall circuit will be a lot less, because now each LED will get less than half the voltage from the supply, and the rest will be lost as heat. How can this be an improvement? If you stick with this design, I'm not going to help any more.

p.s. The answer to the question "So, you mean a circuit like this sir?" is yes. But my answer to "So, my circuit would be like this" is "No, don't do it".
Okay sir, i get your point and the 'logical approach'.
What i understand from all of your statement is that :
1). For a higher supply voltage, let's say more than 24V. And with the series configuration, i should use a 'current limiter'. It's good to use current limiter because the current always be the same which is if i use 11 LED in series with the vary of voltage drop across the LED, it would give us not a CONSTANT CURRENT. And also, the problem of using a 'resistor' as a current source without a current limiter circuit, is solved.

2). For a low supply voltage, let's say Low supply voltage means 5V or 9V or 12V. And with the parallel configuration, i shouldn't use a 'current limiter'. I should use just a simple circuit contain 1 transistor, 11 LED IN PARALLEL, 1 resistor per LED. And because of this, i should consider the saturation current etc. And i can just use MOSFET instead.

Am i correct sir ?
Sorry if i become a pain in your a** ( ahahahaha )
I'm not a quick learner sir.

Thank you very much :)
 

ScottWang

Joined Aug 23, 2012
7,397
It's okay sir to use 10 or 12 or 13 or 14. But at least 9 LED. ahahaha...
I just need to make sure the brightness is enough for a single room in the house.
I see and be careful of the light of leds, they could be hurt your eyes and never recover, so don't see them directly.
 

Thread Starter

BramLabs

Joined Nov 21, 2013
98
I see and be careful of the light of leds, they could be hurt your eyes and never recover, so don't see them directly.
Wahahahahaha..... roger that sir !
Btw, thanks for your short lecture in my post :)
I really learn so much from it.
Big thanks to you sir !

God bless you !
 

MMcLaren

Joined Feb 14, 2010
861
If these RGB LEDs are the type with a common cathode or a common anode and separate R, G, and B pins, wouldn't it be impossible to connect them in series in a way that allows controlling the R, G, and B separately?
 

Thread Starter

BramLabs

Joined Nov 21, 2013
98
If these RGB LEDs are the type with a common cathode or a common anode and separate R, G, and B pins, wouldn't it be impossible to connect them in series in a way that allows controlling the R, G, and B separately?
As you can see on my first post, i'm using SMD 5050 RGB type of LED. And i think it's possible to control R,G and B separately using this IC.
 
Top