# Controlling RGB Led using PWM of Microcontroller

#### ErnieM

Joined Apr 24, 2011
8,353
You ensure a low Vce by driving the base hard. Since "hard" is a relative term most engineers start with a value of 10, or 1/10th the collector current as the base current. I have had cases where there was a light collector current so I found 1/20 was ok.

Using 1/10 is referred to as a forced beta. If you need more than that current in the base your transistor isn't really giving you any gain at all.

Transistors only get specified at a few typical places unless there is a nice graph to follow, but the graph will give you typical numbers, where the listed data gives you min and/or max limits.

Also, remember that something must supply this base current so make sure it can handle that load.

• BramLabs

#### BramLabs

Joined Nov 21, 2013
98
Yes.
The resistor has 5% tolerance, so if you use 1.2K is ok.

You could treat it as the Imax for safety.

The calculation is right.
Vce = 0.3V, that is the Vce max, normally it is less than 0.2V, you can count it as 0.2V or 0.15V

All calculations just calculation, you need to measure their real values at the static status not in the scan mode, like as Vb, Vce...

If you review my posted then you will find the answer.
1. find the V/I of parts.
2. Calculate from the rightest one as Ic, Ib, Vce, if there are two stages also using the same way.
Woah, thank you sir for your help.
As you said, Vce i'm using is the Vce max. Yeah, i think i should use resistor less than 630 ohm. Because as you said, the Vce can be 0.2V or 1.5V and i should meassure that in reality, not only based on calculation. Thanks for reminding me this. ahahaha...

If you review my posted then you will find the answer.
1. find the V/I of parts.
2. Calculate from the rightest one as Ic, Ib, Vce, if there are two stages also using the same way.
Okay sir ! Roger that Thank you for your help ! I really appreciate it !

#### BramLabs

Joined Nov 21, 2013
98
You ensure a low Vce by driving the base hard. Since "hard" is a relative term most engineers start with a value of 10, or 1/10th the collector current as the base current. I have had cases where there was a light collector current so I found 1/20 was ok.

Using 1/10 is referred to as a forced beta. If you need more than that current in the base your transistor isn't really giving you any gain at all.

Transistors only get specified at a few typical places unless there is a nice graph to follow, but the graph will give you typical numbers, where the listed data gives you min and/or max limits.

Also, remember that something must supply this base current so make sure it can handle that load.
Hmm... so basically, you said that for my circuit :
1). In my case, i need to set Ic = 20 mA
2). Then i need Ib = Ic / hfe
Because you said that it should be 1/20, then :
Ib = Ic / hfe = 20 mA / 20 = 1 mA
3). After i get Ib = 1 mA, it means that i should set the Rb :
Rb = 3.3v - 0.7v ( i assume Vbe = 0.7v ) / 1 mA = 2.6 kOhm

Is that what you meant sir ?

Also, remember that something must supply this base current so make sure it can handle that load.
Yeah sir, i used my microcontroller digital pin output using PWM, and i think it can drives current up to 40 mA at the base of transistor ( assume i'm using 100% duty cycle of PWM, so if 100% duty cycle of PWM is applied, it will give 3.3V ).

I really appreciate it !

Last edited:

#### ScottWang

Joined Aug 23, 2012
7,327
You ensure a low Vce by driving the base hard. Since "hard" is a relative term most engineers start with a value of 10, or 1/10th the collector current as the base current. I have had cases where there was a light collector current so I found 1/20 was ok.

Using 1/10 is referred to as a forced beta. If you need more than that current in the base your transistor isn't really giving you any gain at all.

Transistors only get specified at a few typical places unless there is a nice graph to follow, but the graph will give you typical numbers, where the listed data gives you min and/or max limits.

Also, remember that something must supply this base current so make sure it can handle that load.
Using hFe = 10, it's more easy to calculate the Ib, if you don't use hFe = 10 then you will need to try which value is better for Ib case by case, if you using hFe = 10 then the most of transistors can be used.

• BramLabs

#### ebeowulf17

Joined Aug 12, 2014
3,307
Yeah sir, i was thinking about constant-current at first using LM317. But i think the problem is the size of capacitor that needs before and after LM317 circuit for constant-current. Let's just say, i'm using 10 uF of capacitor. The size of 10 uF doesn't fit my criteria sir.
Honestly, i want to build circuit for making IoT project. And i want to design my own lamp RGB. So the size should be considered. I want to fit the circuit.
Just say that i want to fit the circuit into a lamp like this :

View attachment 97896
And because of the capacitor i need for constant current using LM317, i think the capacitor is not small enough on the size to fit to the lamp's case.

Oh yeah sir, just assume i can make a constant voltage supply that will exactly gives 36v Thank you sir for your help ^^
The potential problem isn't so much your supply voltage, which you could easily regulate, but rather the forward voltage of the LEDs, which could easily vary from part to part and is hard to account for.

• BramLabs

#### BramLabs

Joined Nov 21, 2013
98
The potential problem isn't so much your supply voltage, which you could easily regulate, but rather the forward voltage of the LEDs, which could easily vary from part to part and is hard to account for.
Yeah, i completely agree with you. If i'm using constant current, it doesn't matter the vary of the forward voltage of LED. But because of i'm not using constant current, the current that will go through the LED will be vary also, because the vary of the forward voltage of LED.
Let's say, i make a constant current and i combine this circuit. So my supply would be 36v, and i'm using constant current that can gives me 20 mA precisely. What i want to ask is that, can LM317 maintain my input voltage up to 36v ? Thank you sir for your help !

#### ebeowulf17

Joined Aug 12, 2014
3,307
Yeah, i completely agree with you. If i'm using constant current, it doesn't matter the vary of the forward voltage of LED. But because of i'm not using constant current, the current that will go through the LED will be vary also, because the vary of the forward voltage of LED.
Let's say, i make a constant current and i combine this circuit. So my supply would be 36v, and i'm using constant current that can gives me 20 mA precisely. What i want to ask is that, can LM317 maintain my input voltage up to 36v ?
View attachment 97898

Thank you sir for your help !
I believe so. If not, there are other constant current options, including dedicated LED drivers. I think the one below would be a viable option, just as an example:

• BramLabs

#### ScottWang

Joined Aug 23, 2012
7,327
• BramLabs

#### ErnieM

Joined Apr 24, 2011
8,353
First, a base current of 1/10 the collector current should be your starting point. I only used 1/20 when I had a very light load; 20 ma is not a light load.

A constant current source will have more problems than the resistor. Your 11 diodes in series add to 35.2 volts, very close to the 36v you supply. The problem is there is very little room for the voltage on the resistor to change without a drastic change in the current.

example: say the voltage on the diodes is 0.050 volts lower than spec. Then the total voltage is 34.65 so instead of 0.8 volts on the resistor you have 1.35 for a 69% increase in current. That could burn your devices out fast.

The LM317 needs a few volts across to maintain regulation, so it would starve out and only pass a small current.

Is the 36 volt supply cast in stone? Would it be possible to use a higher voltage? It wastes more power in the resistors but it helps even out the current. You could even use a constant current source or sink if you had a few more volts to work with.

Alternatively, break the chains and put say 5 LEDs in one chain and 6 in another.

• BramLabs

#### ScottWang

Joined Aug 23, 2012
7,327
Alternatively, break the chains and put say 5 LEDs in one chain and 6 in another.
Yes, this will be a good idea, if insist to use 11 leds then I will use that way.

• BramLabs

#### dannyf

Joined Sep 13, 2015
2,197
when i applied KVL equation, 36 Volt - 11*(2,1V) - 0,3V = 12.6V ( the transistor is in the saturation region ). Where the 12.6 V will be dissipated ?
On the resistor, as you had accounted the voltage drop over the transistor.

However, it is advisable if you calculate the power dissipation over both the resistor and the transistor and make sure that they don't overheat.

• BramLabs

#### John P

Joined Oct 14, 2008
1,995
I'm puzzled by Ernie's claim that "A constant current source will have more problems than the resistor." He then goes on to agree with me that there are some pretty severe problems with the pure resistor approach! But with a constant current circuit, you do need enough headroom above the sum of all the LED voltages to make it function.

The two transistor constant current circuit can operate on less than 1V drop. And the power transistor could double as the control element for turning the string of LEDs on.

• BramLabs

#### ErnieM

Joined Apr 24, 2011
8,353
I'm puzzled by Ernie's claim that "A constant current source will have more problems than the resistor." He then goes on to agree with me that there are some pretty severe problems with the pure resistor approach!
The two subjects are not mutually exclusive.

But with a constant current circuit, you do need enough headroom above the sum of all the LED voltages to make it function. The two transistor constant current circuit can operate on less than 1V drop. And the power transistor could double as the control element for turning the string of LEDs on.
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.

• BramLabs

#### BramLabs

Joined Nov 21, 2013
98
Alternatively, break the chains and put say 5 LEDs in one chain and 6 in another.
Yes, this will be a good idea, if insist to use 11 leds then I will use that way.
Okay sir, if i'm gonna break the chains, let's say i'm going to use 6 LED and 5 LED.
So, let's review a little bit :
Red LED needs Vf = 2,1V, If = 20mA ;
Green LED needs Vf = 3,2V, If = 20mA ;
Blue LED needs Vf = 3,2V, If = 20mA ;

RED LED RGB circuit in series First node :
36 Volt - 6*(2.1V) - 0.2V (assume when the 2N222A transistor or in reality, i'm using MMBT2222A, is in saturation region ) / 20 mA = 1160 ohm
RED LED RGB circuit in series Second node :
36 Volt - 5*(2.1V) - 0.2V (assume when the 2N222A transistor or in reality, i'm using MMBT2222A, is in saturation region ) / 20 mA = 1265 ohm

GREEN LED or BLUE LED RGB circuit in series First node :
36 Volt - 6*(3.2V) - 0.2V (assume when the 2N222A transistor or in reality, i'm using MMBT2222A, is in saturation region ) / 20 mA = 830 ohm
GREEN LED or BLUE LED RGB circuit in series Second node :
36 Volt - 5*(3.2V) - 0.2V (assume when the 2N222A transistor or in reality, i'm using MMBT2222A, is in saturation region ) / 20 mA = 990 ohm

Then :
1). In my case, i need to set Ic = 40 mA ( because splitting the node, Ic becomes 40 mA )
2). Then i need Ib = Ic / hfe ; ( let's say, i'm using hfe = 10 based on the datasheet : Vce sat for 300 mV : Ib = 15 mA, and Ic = 150 mA )
Ib = Ic / hfe = 40 mA / 10 = 4 mA
3). After i get Ib = 4 mA, it means that i should set the Rb :
Rb = 3.3v - 0.7v ( i assume Vbe = 0.7v ) / 4 mA = 650 Ohm

This circuit will be looked like this : And the Power resistor i need for 1155 ohm is : P = I*I*R = 20mA * 20mA * 1160 = 0.464 Watt ( i should use 0.5 watt at least )
And the Power resistor i need for 1260 ohm is : P = I*I*R = 20mA * 20mA * 1265 = 0.506 Watt ( How about if i use 0.5 watt also ? Because in my town, it's really hard to find SMD resistor that can have more than 0.5 watt )
And the Power resistor i need for 825 ohm is : P = I*I*R = 20mA * 20mA * 830 = 0.332 Watt ( i should use 0.5 watt at least )
And the Power resistor i need for 985 ohm is : P = I*I*R = 20mA * 20mA * 990 = 0.396 Watt ( i should use 0.5 watt at least )

How about my calculation sir ?
I really appreciate your help sir !
Thank you very much !

Oh yeah, as an additional to Sir ErnieM :
A constant current source will have more problems than the resistor. Your 11 diodes in series add to 35.2 volts, very close to the 36v you supply. The problem is there is very little room for the voltage on the resistor to change without a drastic change in the current.

example: say the voltage on the diodes is 0.050 volts lower than spec. Then the total voltage is 34.65 so instead of 0.8 volts on the resistor you have 1.35 for a 69% increase in current. That could burn your devices out fast.

The LM317 needs a few volts across to maintain regulation, so it would starve out and only pass a small current.

Is the 36 volt supply cast in stone? Would it be possible to use a higher voltage? It wastes more power in the resistors but it helps even out the current. You could even use a constant current source or sink if you had a few more volts to work with.
You said the problem if i'm using constant current is when the voltage on diodes is lower than spec. So, i'll get more power dissipation on the resistor. And next, you said that how about if i use a higher voltage. And you state that it wastes more power in the resistor, but helps the current.
I don't understand your statement sir. What i understand from your statement is that, both of them ( constant current, and use a higher voltage ), will give more power which is the dissipation power on the resistor. But in the higher voltage's case, it will help the current. But in the constant current, it won't help.
Constant current is a circuit to maintain the current whatever the load is required isn't it ?
So you said on your example that i'll get 69% increase of current for 1.35v on the resistor. How can this possible for constant current ? The constant current always flow 20 mA, and won't increase isn't it ?

I'm sorry sir for my bad question. I'm not a quick learner Thank you before, i really appreciate your help !

#### Attachments

Last edited:
• ScottWang

#### Picbuster

Joined Dec 2, 2013
1,041
Sorry not looking carefully to your schematic.
Your calculations are correct however; We always use FETs ( in saturation the resistance is in milliOhms hence heat i^2 * R remails low.
This is applicable to the serial resistors too.
To avoid a current source; ( a current source will produce heat also)
Feed via a voltage divider the 36V into the controller this to compensate led current change as result off 36V chances resulting in a PWM correction to maintain the light at one level. assuming that 36V fluctuations are no more that 10% approx..
The PWM frequency is an issue when leds are used in a TV recording environment.

• BramLabs

#### BramLabs

Joined Nov 21, 2013
98
Sorry not looking carefully to your schematic.
Your calculations are correct however; We always use FETs ( in saturation the resistance is in milliOhms hence heat i^2 * R remails low.
This is applicable to the serial resistors too.
To avoid a current source; ( a current source will produce heat also)
Feed via a voltage divider the 36V into the controller this to compensate led current change as result off 36V chances resulting in a PWM correction to maintain the light at one level. assuming that 36V fluctuations are no more that 10% approx..
The PWM frequency is an issue when leds are used in a TV recording environment.
What's your definition of current source sir ?
Because i don't really understand about current source.
Is that the current that flow through the source of transistor ( in case of FET ) ?
Or you mean the current source is the result of applying Voltage and a resistor in series, so there will be a current that flow through the circuit ? ( In this case, the current source you mean is the current that flow from 36v to the resistor to the LED and to the transisor ).

#### Picbuster

Joined Dec 2, 2013
1,041
I hope that this will give some idea.

#### Attachments

• 20.4 KB Views: 9
• BramLabs

#### ScottWang

Joined Aug 23, 2012
7,327
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

• BramLabs

#### ScottWang

Joined Aug 23, 2012
7,327
I hope that this will give some idea.
The output voltage of pwm from uC only 3.3 Vmax, if you want to use N mosfet then you should pick up the mosfet can be provide the Vgs for 3.3V could get into the saturation region or to use one bjt plus one P mosfet ... 