Shop OBEX P1 Docs P2 Docs Learn Events
Vary Brightness of LED using PWM — Parallax Forums

Vary Brightness of LED using PWM

Todd DeckerTodd Decker Posts: 7
edited 2009-01-04 04:22 in BASIC Stamp
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:
  • 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.
Thank you for your help in advance!!

P. Todd Decker

Comments

  • $WMc%$WMc% Posts: 1,884
    edited 2008-11-09 08:19
    Mr.decker

    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%______________________________________
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 10:13
    I would not use the capacitor and resistor in your application.· Your goal is not, after all, to produce some particular analog voltage.· Your goal, rather, is to produce a particular average light intensity.· You can do that by varying the duty cycle instead of varying the peak intensity.· You don't need the RC filter to average the output, because your eyes can do the averaging perfectly well.· Just hook the LED to the Stamp output pin through a resistor and vary the duty cycle.

    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
  • HumanoidoHumanoido Posts: 5,770
    edited 2008-11-09 12:38
    Why go through all that just to dim the LED?
    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

    ' ---------------------- Titles -------------------------------------
    
    ' Title      LED Dimmer
    ' Code Name  LED_dimmer.bs2
    ' Purpose    Dim the LED
    ' Author     humanoido
    ' Email      penguin(dot)robot(at)yahoo(dot)com
    ' Version    1.0
    ' Begin      09 November  2008
    ' Update
    ' Hardware   BS2 Homework Board, LED
    
    ' ---------------------- Directives ---------------------------------
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' ---------------------- Declarations -------------------------------
    
    n               VAR     Byte      ' Counter
    
    ' ---------------------- Descriptions -------------------------------
    
    ' The LED is on pin 1. The program will blink the LED from dim to
    ' bright and loop. It demonstrates a simple way to dim the LED.
    
    DO
    FOR n = 0 TO 20:LOW 1:PAUSE 14:HIGH 1:PAUSE 14 ' dim it
    NEXT
    FOR n = 0 TO 200:HIGH 1 ' bright it
    NEXT
    LOOP ' again
    
    ' -------------------------------------------------------------------
    
  • Carl HayesCarl Hayes Posts: 841
    edited 2008-11-09 12:46
    Not bad, Humanoido. But you use six lines of code. Why do all that (as you ask), when you can do it with a single line?
    For example, to set a duty cycle of 50% for one second on a BS2:

    PWM 1,128,1000

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i
  • $WMc%$WMc% Posts: 1,884
    edited 2008-11-09 22:41
    Is It just Me,,,or is Mr. Decker tryin to use the RCTIME gemick on the output PIN of the $stamp?????...I think A look in the stampwork's Manule, pg.146/Exp.#26 may help to clear some thing's up.....Also I have A few TRI-Color LED's some are 2 lead, and some are 3 lead...W/O the led Spec.'s how can You write A PULSOUT command?????...One of The 2 Lead Tri-Color LED's That I have use's TTL level voltage...."+" for RED in color, "-" for GREEN in color, "~" for YELLOW in color,...Since the $stamp can't "REV" it's polarity, or take an INPUT below Vss,,It will need some Hardware to make the TRI-COLOR led work....If and I said "IF" the LED Mr.Decker is useing is like the one I have,,,It will need some Hardware and A - 5volt P.S.................Use at least A 200OHM RES.in ser. w/ Your I/O pins........

    ______JUST TRUST ME ON THIS ONE___200OHM's_____________________________________$WMc%_____________
  • Todd DeckerTodd Decker Posts: 7
    edited 2008-11-10 00:32
    Thank you for all the input so far!

    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
  • Todd DeckerTodd Decker Posts: 7
    edited 2008-11-10 00:36
    Also, the thinking behind needing to use a cap comes straight from the Parallax reference manual (version 2.2, page 356). I'll give the straight PWM a try, but I was hoping to not to constantly have to cycle between each of the three pins driving each of the elements doing PWM so that I don't have blinking. My hope was I'd be able to perform some other tasks between refreshing the RC circuit. I'll play with it a bit and see how things come out.

    Thank you for the advice.

    P. Todd Decker
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-10 04:27
    Todd,

    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:

    attachment.php?attachmentid=56685

    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
    366 x 327 - 2K
  • HumanoidoHumanoido Posts: 5,770
    edited 2008-11-10 09:35
    Carl Hayes, very good idea. Here's the new code - 1 declaration
    and 1 code line.

    humanoido
    ' ---------------------- Titles -------------------------------------
    
    ' Title      Variable PWM LED Dimmer
    ' Code Name  variable_LED_dimmer.bs2
    ' Purpose    Dim the LED
    ' Author     humanoido
    ' Email      penguin(dot)robot(at)yahoo(dot)com
    ' Version    1.0
    ' Begin      10 November  2008
    ' Update
    ' Hardware   BS2 Homework Board, LED
    
    ' ---------------------- Directives ---------------------------------
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    ' ---------------------- Declarations -------------------------------
    
    n               VAR     Byte      ' Counter
    
    ' ---------------------- Descriptions -------------------------------
    
    ' The LED is on pin 1. The program will show full LED brightness and
    ' slowly dim the LED until off, then repeat.
    
    ' ---------------------- Credits ------------------------------------
    
    ' PWM idea by Carl Hayes
    
    ' ---------------------- Main Program -------------------------------
    
    DO:FOR n = 200 TO 0:PWM 1,n,10:NEXT:PAUSE 800:LOOP
    
    ' -------------------------------------------------------------------
    
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-11-10 18:20
    Todd, I agree with Phil, that the circuit needs the buffer if you want the persistence of the light level while your program is free to do other things. Without the buffer, the capacitor will discharge through the LED in no time. Read on to page 357 of the STAMP manual. It is also good to have a buffer so that the Stamp itself would not have to supply all the operating current for the LEDs.

    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.
    attachment.php?attachmentid=56698
    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
    223 x 116 - 4K
    213 x 114 - 3K
    216 x 116 - 3K
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-10 20:36
    Tracy,

    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:

    attachment.php?attachmentid=56702

    At some point, though, one has to question the amount of complexity this adds. smile.gif

    -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
  • metron9metron9 Posts: 1,100
    edited 2008-11-11 06:21
    You can also use a simple fet and add a capacitor and a resistor to the base of the fet. This can allow slower frequency PWM with a similar brightness control as it will give you a saw tooth pattern instead of a square wave.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • Wolverine69Wolverine69 Posts: 31
    edited 2009-01-04 04:22
    Isn't the max value for the PWM duration only 255?· (According to my copy of the Basic Stamp Manual it is anyway)
    Carl Hayes said...
    Not bad, Humanoido. But you use six lines of code. Why do all that (as you ask), when you can do it with a single line?
    For example, to set a duty cycle of 50% for one second on a BS2:

    PWM 1,128,1000

Sign In or Register to comment.