Smooth LED fading via PWM


Closed Thread
Results 1 to 40 of 55

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Smooth LED fading via PWM

    Ok, so I'm dabbling with LEDS...fading an LED smoothly until its completely off is proving to be a little irksome for me.

    Up until now, I've been using 8 bit PWM, but now I'm starting to think 8 bit PWM just isn't gonna cut it - why? Becuase even with a duty value of just 1, the LED albeit very dull, is still quite visible...I'd have thought it being switched on only 1/256th of the time would have resulted in a very hard to see LED? (or have I likely got some setting wrong?)

    Next...since we all see logarithmically, apparently a linear LED fade will just not do, I found some values on the net for a log fade (256 values), but such relatively lowish resolution makes for some rather large step jumps...

    Code:
    Smooth_fade:    
    Lookup count1, [ _
      0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, _
      1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3, _
      3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,5,5,5, _
      5,5,5,5,5,6,6,6,6,6,6,6,7,7,7,7,7,7,8,8,8,8,8,8,9,9, _
      9,9,10,10,10,10,10,11,11,11,11,12,12,12,13,13,13,13, _
      14,14,14,15,15,15,16,16,16,17,17,18,18,18,19,19,20,  _
      20,20,21,21,22,22,23,23,24,24,25,26,26,27,27,28,29,  _
      29,30,31,31,32,33,33,34,35,36,36,37,38,39,40,41,42,  _
      42,43,44,45,46,47,48,50,51,52,53,54,55,57,58,59,60,  _
      62,63,64,66,67,69,70,72,74,75,77,79,80,82,84,86,88,  _
      90,91,94,96,98,100,102,104,107,109,111,114,116,119,  _
      122,124,127,130,133,136,139,142,145,148,151,155,158, _
      161,165,169,172,176,180,184,188,192,196,201,205,210, _
      214,219,224,229,234,239,244,250,255], duty
      return
    ....how do I work out the log values for say 512 entries or 1024 entries (ok, so this is more a maths question tan a PICBASIC, but I suck at maths!)

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Oter than the math part, I can say two things about some LEDs.

    I have regular & cheap 120degree ultra-LEDs and datasheet says they are 15mA. These new era ultra-LEDs are quite bright even at 0.1mA.
    Diriving these LEDs with a serial resistor and with PWM may not give you a nice smooth fade effect as they are so much sensitive to current.
    So, I would look at their datasheet and see what voltage they are driven.
    Then, I would run the PIC at the same voltage, and drive them directly by the pin with no resistor.

    Say, the LED is 3.2V, then run the PIC at 3.2V.

    This way, you will see the full fade effect.

    However, if you are using different LEDs with different voltage levels on the same circuit, you will have to work on the circuit to get the same method work.
    Usually, Red LEDs are 2.10V and Green ones are 3.20V (depending on the brand some are up to 3.70V).


    If you are using Power LEDs, then the world is completely different.
    Last edited by sayzer; - 4th September 2011 at 17:40. Reason: typo
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    These are high brightness blue leds (quoted at about 25mA)

    I'm running the PIC at a voltage supply of 3.0Vand driving the LEDs directly from a PIC output pin (ie slightly less than the LED's 3.2 to 3.4V fwd voltage - so as to under drive them a little & protect the PIC - no series limiting resistor...yeah, I know contentious subject!)

    With a PWM value of 1 at 8 bits, that oiught to be very dull to the poiunt of not being able to see the LED, bit that's not the case...sure it's dull;, but it's clear that the LED is on...so I'm thinking if I up the number of steps to say 512 or even 1024, then that should allow much more granularity at the lower end of the PWM (where the LED remains rather perky still)
    Last edited by HankMcSpank; - 4th September 2011 at 17:53.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,560


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    So why not turn it off (0% dutycyle) when you want it to be off? Why do want to drive current thru it and NOT have it light up at all?

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Quote Originally Posted by HenrikOlsson View Post
    So why not turn it off (0% dutycyle) when you want it to be off? Why do want to drive current thru it and NOT have it light up at all?
    Because I seek a smooth fade from max brightness through completely off.

    What I'm observing is that a duty cycle of 1 seems too discernibly 'on' still (I'd have thought you'd have to get up real close to it at such a duty cycle to see if it is on or not?), the 'jump between 0 & 1 from a brightness perspective, whilst small, is still too big which is why I'm thinking more resolution may help?

    (before you think why don't I just try it myself ...well, firstly I want to ask others if when using 256 steps of pwm, whether a duty value of 1 normally easily visibly shows the LED to be on.....and secondly, I've got to wrap my head around 10 bit HPWM bit mapping - the pesky registers are spread a little).
    Last edited by HankMcSpank; - 4th September 2011 at 18:40.

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Smooth LED fading via PWM

    Just to give you a flavour, this is a duty setting of 1...



    Now LED3 & LED6 happen to be faulty (I can't be bothered to swap them out right now - they're way duller than others and clearly have an issue), but I reckon with a duty cycle value of 1 the led brightness should actually be more their level!

    Now LEDS are notoriously difficult to photograph...the LEDS don't actually as bright as they do in the photo 'in the flesh', and these being high brightness have a lens that accutely magnifies any LED light but nevertheless I think they're too bright for a duty value of 1!
    Last edited by HankMcSpank; - 4th September 2011 at 19:12.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts