Vary Brightness of LED using PWM
Todd Decker
Posts: 7
Hello All,
I have an LED (actually, one anode pin of a tri-color common cathode LED) connected to P8 of a BS2 (Rev J)·on a Board of Education (USB Rev D).· The conection is through a 75 ohm resistor for the LED has a forward voltage of 3.5V and a forward current of 20mA (Vdd = 5V).· This works wonderfully and I can easily turn the LED on and off by toggling P8.· No problem.
The next step I would like to take is to vary the brightness of this LED using PWM.· I understand that I will need to connect·a capacitor between ground and the connection between the resistor and the LED to·hold the voltage from the PWM output.· And, I understand that I'll have to periodically 'refresh' the voltage by using PWM again every now and then.
With that set-up, I have several questions:
P. Todd Decker
I have an LED (actually, one anode pin of a tri-color common cathode LED) connected to P8 of a BS2 (Rev J)·on a Board of Education (USB Rev D).· The conection is through a 75 ohm resistor for the LED has a forward voltage of 3.5V and a forward current of 20mA (Vdd = 5V).· This works wonderfully and I can easily turn the LED on and off by toggling P8.· No problem.
The next step I would like to take is to vary the brightness of this LED using PWM.· I understand that I will need to connect·a capacitor between ground and the connection between the resistor and the LED to·hold the voltage from the PWM output.· And, I understand that I'll have to periodically 'refresh' the voltage by using PWM again every now and then.
With that set-up, I have several questions:
- First, how do I calculate the size of capacitor needed?· I have the resistor size determined for me based upon what I need for the LED--mainly, the 75 ohm value.· I know I'll need to use the 'Charge Time = 5 * R * C' formula.· Solving for 'C', I would get 'C = Charge Time / (5 * R)'.· Since my 'R' is 75 ohm, I would then get 'C = Charge Time / 375'.· Simple enough.·· But, what 'Charge Time' do I need to pick?
- Second, once I have the 'Charge Time', I assume that I automatically have the duration since the book says that on the BS2 each 'cycle' is about 1 ms.· Is this accurate?· I assume if I move to a different BASIC STAMP with a faster clock rate, I'll need to adjust this so I should store everything as constants in my code to allow easy portability.
- Finally, how often will I need to refresh the charge, e.g. how much time do I have to do other things?· I am unsure how to calculate the discharge time given the current draw of the LED (20mA) and the capacitance calulcated above (plus the leakage current of the BS2 itself).· I would like the LED to be perceived as fairly constant, so I really don't want it to dim that much before being refreshed.
P. Todd Decker
Comments
I guess A "tri-color led" would need some added hardware support???
Do You have some spec.s on the led ????? like what it needs to be this color,that color, polarity, etc.?????
_____________________$WMc%______________________________________
75 ohms is a little small.· Assuming, say, 1.5v across the LED when it's on, there will be 3.5v across the resistor.· The current, then, will be 46 ma, about half again what the output pins on a Stamp are rated for (30 ma, max).· I wouldn't go below 150 ohms.
Hook it up with 150 ohms in series, vary the duty cycle, and see what you get.· You'll find it plenty bright, and you'll find it much more controllable than it would be with a capacitor.· Why will be more controllable?· A LED's intensity doesn't vary at all linearly with voltage, especially at the low end of the range --·but its average intensity will vary with perfect linearity with duty cycle, all the way down to zero.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· -- Carl, nn5i
Just put a 14 millisecond pause on each side
of the LED high as seen here in my dimmer
program. You can vary the brightness by
timing. The Homework board has a 220 ohm
resistor in series to port.
humanoido
For example, to set a duty cycle of 50% for one second on a BS2:
PWM 1,128,1000
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· -- Carl, nn5i
______JUST TRUST ME ON THIS ONE___200OHM's_____________________________________$WMc%_____________
My apology for not posting which tri-color I am using. I am using part number RL5-RGB-D from http://superbrightleds.com. I've posted a link below to the data sheet. It is a 4-pin LED with one lead for each of the three colors and another lead for the common cathode. Two of the colors (green and blue) have a 3.5V forward voltage drop and the red element has a 2.5V drop. All three colors require 20mA of current. The resistors I am using are calculated based upon the standard way of calculating resistors for LEDs and also was confirmed by Parallax tech support. So, I'm a little confused by all the different advice around this point. The resistors I'm using are working just fine and the color are keeping the color brightness consistent between each of the three elements.
http://www.superbrightleds.com/TriColor LED.htm
P. Todd Decker
Thank you for the advice.
P. Todd Decker
One of the problems with relying on a cap to hold your voltage against a 20mA current draw is that it has to be a big one. If you want to go this route, you will be better off incorporating an op-amp in a current regulator configuration, such as the one illustrated here:
For the lowpass filter, try 4.7K and 0.1uF, then adjust as needed. The transistor can be any general-purpose NPN, such as a 2N3904. The emitter resistor shown will yield an 18mA current when the input to the op-amp is 5V. To achieve an even 20mA, you need a 250-ohm resistor, which may be harder to find, although you could parallel two 510s to get close. Any single-supply general-purpose op-amp should work here, so long as it can tolerate a positive supply voltage as high as your Vin, and so long as its input voltage range includes ground. (Check the datasheet!)
-Phil
P.S. BTW, I'm assuming your Vin is at least 9V. If not, there are alternatives.
Post Edited (Phil Pilgrim (PhiPi)) : 11/10/2008 4:36:17 AM GMT
and 1 code line.
humanoido
Your RGB is common cathode, so the drive will have to come from three sources. The circuit Phil posted could be duplicated x3, but only for a common anode RGB.
The attached is a circuit that (in triplicate) could drive your common cathode RGB.
It is just an op-amp buffer with a gain of x1.5, so when the input goes from 0 to 5 volts, the output goes from 0 to 7.5 volts (and note the Vin~9 volt supply). With a Vf=3.5 volt LED and a 200ohm resistor, the current at full scale would be 20 mA. But the output won't turn on at all until the PWM brings the voltage input to (3.5 / 1.5 = 2.3 volts). So the PWM range for brightness control would be about 115 to 255.
The second circuit is one that regulates current over the full range, with the power being taken up by the op-amp instead of an external transistor. But it is only suitable for an LED where both terminals are accessible. Not common anode or common cathode.
P.S.: The third diagram is like first, except the gain is unity, a simpler circuit. But with a Vf=3.5 LED, the PWM control range will be narrow, from about 180 to 255. That is probably enough.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Post Edited (Tracy Allen) : 11/10/2008 6:46:01 PM GMT
Thanks! I neglected to consider his common-cathode configuration which, as you point out, complicates things. An alternative would be to use a current mirror, which will convert the op-amp's current sink into a current source that's compatible with a grounded cathode, viz:
At some point, though, one has to question the amount of complexity this adds.
-Phil
Addendum: One hidden advantage of this technique is that Vin doesn't have to be as high as before, since the LED's VFWD is no longer in series with the 5V (max) across the current monitor resistor.
Post Edited (Phil Pilgrim (PhiPi)) : 11/11/2008 4:31:25 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!