Hot!I have a question about fading out an LED.

Page: 123 > Showing page 1 of 3
Author
CVRIV
Senior Member
  • Total Posts : 125
  • Reward points : 0
  • Joined: 2017/09/06 13:49:25
  • Location: 0
  • Status: offline
2017/12/24 04:54:03 (permalink)
0

I have a question about fading out an LED.

School is out! I'm doing my own experimenting now. I got an A- in uComputers. I only got an A- because I burned myself out so badly that when I took the final... I screwed it up royally.
 
Anyways, I'm writing some code to fade a LED in and out. I was under the impression that if I toggled a LED, on and off, with a 50% duty cycle and no time restrictions that it wouldn't even light up, but that's not what's happening. I'm using a PIC16F883, which operates at 4MHz.
 
How fast would you need to toggle a LED, with a 50% duty cycle to have it not even light up?
 
To get this LED to fade in and out I would have to mess with the duty right? 
 
OH, and MERRY CHRISTMAS EVERYONE!!!
#1

58 Replies Related Threads

    bblessing
    Super Member
    • Total Posts : 690
    • Reward points : 0
    • Joined: 2008/12/04 06:44:21
    • Location: Cincinnati, OH
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 05:12:21 (permalink)
    0
    Merry Christmas to you too and great job earning As in your coursework. PWM is the best method to achieve what you're trying to do, but yes, varying the duty cycle will do the trick.
    #2
    Ian.M
    Super Member
    • Total Posts : 13114
    • Reward points : 0
    • Joined: 2009/07/23 07:02:40
    • Location: UK
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 05:26:15 (permalink)
    0
    If you pulse a LED, its going to turn on, brightness determined by duty cycle.  The only exception is if you pulse it significantly faster than the corner frequency of the low pass filter formed by its junction capacitance and its series resistor, (which for a typical LED is likely to be a few tens of MHz - so well out of range of what an 8 bit PIC can do) *AND* its Vf is significantly greater than the average voltage across it due to the low pass filtered waveform duty cycle.
     
    To fade a LED effectively you need to vary the duty cycle while keeping the pulse frequency well above 60Hz (>100Hz is good).   As the eye's response to light intensity is quasi-logarithmic you need much finer steps at the low end of the duty cycle range to get smooth dimming.

    --
    NEW USERS: Posting images, links and code - workaround for restrictions.
    I also support http://picforum.ric323.com because this forum is sometimes too broken to use!
    #3
    Antipodean
    Super Member
    • Total Posts : 1645
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 05:56:28 (permalink)
    0
    I cannot remember if a 16F883 has a PWM module, but if it has, as already mentioned, then this makes doing what you want ridiculously easy.
     
    I did a similar thing on a PIC24 using a PWM module for a heartbeat LED. I had an interrupt every time the PWM output changed state, then in the interrupt routine I incremented the value loaded into the PWM holding register until it reached a predetermined high value, after which I decremented it until it reached a predetermined low value. I experimented with the high and low threshold values to produce a nice fade up and down. I think I had it pulsing the LED at about a kHz, and was using a 16 bit PWM. You may need to vary the pulse frequency down a bit (but don't go below about 100Hz as already mentioned) if you have less PWM bits.
     
    If you haven't written an interrupt routine before then this will give you an interesting exercise in including a flag so you know if it should be increment or decrement.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #4
    1and0
    Access is Denied
    • Total Posts : 8396
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 06:00:40 (permalink)
    0
    Anti-podean
    I cannot remember if a 16F883 has a PWM module, but if it has, as already mentioned, then this makes doing what you want ridiculously easy.

    It does have a PWM module.
    #5
    CVRIV
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2017/09/06 13:49:25
    • Location: 0
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 06:01:31 (permalink)
    0
    Thank you:)
    The PIC16F883 does have built in PWM, but I don't want to use it because I want to learn to do it on my own first. 
    I understand the concept, I just wanted to make sure before I give it a go. 
    Thanks everyone!
    #6
    bblessing
    Super Member
    • Total Posts : 690
    • Reward points : 0
    • Joined: 2008/12/04 06:44:21
    • Location: Cincinnati, OH
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 06:08:11 (permalink)
    0
    CVRIV
    Thank you:)The PIC16F883 does have built in PWM, but I don't want to use it because I want to learn to do it on my own first. I understand the concept, I just wanted to make sure before I give it a go. Thanks everyone!

    That's fair. However, learning the peripherals will help you more in the long run. Find a good project like creating a side wave.

    I spent some time bit banging a UART. It's nice to get it working, but if you have to do something like that in your project, other than for school, then the wheels have come off the bus during the design phase.
    #7
    crosland
    Super Member
    • Total Posts : 1255
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 07:13:25 (permalink)
    0
    You can getaway with a surprisingly low frequency as the eye will "fill in the gaps" and the LED will appear brighter than the calculations would suggest. This is the principle of some LED bicycle lights. The PWM drive saves power but doesn't reduce the perceived  brightness too much (I am talking about the ones that do not obviously flash).
    #8
    NKurzman
    A Guy on the Net
    • Total Posts : 16437
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: I have a question about fading out an LED. 2017/12/24 09:32:57 (permalink)
    0
    If you apply a 50% duty cycle to an led at any frequency that you pic will produce it will light up at half brightness. Not that human eyes are not linear. So that it will appear dimmer, but not by half.
    #9
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2645
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 10:25:08 (permalink)
    0
    Fading out an led without pwm.
                   D1       R1 1K  LED1
    MPU OUT >|----/\/\/\ --->|---  GND
                        |                    |
                        ------+| |-------
                         C1 elect 22uf

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #10
    PStechPaul
    Super Member
    • Total Posts : 2039
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 14:44:48 (permalink)
    0
    I thought that circuit may have a rather short fade time, as the brightness (current) versus voltage has a sharp "knee". However, a simulation shows that it actually extends the time a the current drops.       
     

     
    PWM will provide a much wider range of brightness, and a 1000:1 range is not unusual. You can see a faint glow in an LED with only a few microamps if you are in a dark room.
     
    I thought a better linear fade circuit might be done with an inductor and a commutating diode. But it actually it just causes a linear decrease in current for a short time, even with a 1H inductor.

     
    #11
    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 15:22:58 (permalink)
    0
    CVRIV
    ...
    I was under the impression that if I toggled a LED, on and off, with a 50% duty cycle and no time restrictions that it wouldn't even light up, but that's not what's happening. I'm using a PIC16F883, which operates at 4MHz.
    ...

    I know this has already been discussed a bit above, but you seem to have made a bad assumption at the start.
    Possibly you were thinking about the characteristics of an incandescent bulb, not an LED.
    LEDs are EXTREMELY fast. They are used in fibre optics to transmit information at hundreds of MHz.
    Garden variety LEDs are a bit slower, but as Ian said, you can still switch them at tens of MHz.
     
    So, for switching speeds less than 10MHz, for a 50% square wave, the average current through the LED will be close to half the 100% current. The perceived brightness will be much more than half though.
     
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #12
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 2645
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/24 16:37:14 (permalink)
    0
    That circuit just discharges the cap. when the latch port is made low.
    It makes leds smooth when they switch off.  Diode voltage loss not important, leds are bright enough.
     
    You get used to seeing fades on anything with a screen nowadays.
    post edited by Gort2015 - 2017/12/24 16:38:27

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #13
    SylviaElse
    Starting Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2014/11/14 04:54:17
    • Location: 0
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/25 18:46:42 (permalink)
    0
    I think that to prevent it from lighting up, you have to toggle it at a rate such that its capacitance together with our voltage drop resistor would form an RC filter that prevented the voltage across the diode from reaching its conduction threshold. Not a very realistic proposition, so, yes, you'll need to alter the duty cycle to change its apparent brightness.
    #14
    PStechPaul
    Super Member
    • Total Posts : 2039
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/25 20:25:09 (permalink)
    +1 (1)
    Hi, Sylvia. I've seen your posts on s.e.d. Hope you had a great Christmas down under!
     
    I think PWM will be most effective without a capacitor. Otherwise the capacitor will charge to a voltage that will be above or below the threshold. PWM directly will turn on the LED for each ON cycle and apparent brightness will be according to the duty cycle (but not a linear relationship). PWM into an inductor as a buck regulator will adjust the current without dealing with the conduction threshold.

     
    #15
    NKurzman
    A Guy on the Net
    • Total Posts : 16437
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: I have a question about fading out an LED. 2017/12/25 21:03:15 (permalink)
    0
    Microchip has a appnotes on led dimming.
    You are touching on many related topics.
    Frequencies of around 100 hz and over will not appear to blink to the human eye. If you want brightening and dimming to appear linear to the human eye you will need minimum of a square function.
    White LEDs have phosphor emitters. This makes blinking them at frequency impossible.
    #16
    laurie21
    Junior Member
    • Total Posts : 103
    • Reward points : 0
    • Joined: 2009/07/21 12:59:44
    • Location: Vancouver B.C. Canada
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/25 23:49:08 (permalink)
    0
    Merry Xmas to you to. I may be wrong but i think you are with the belief leds take time to come up to full brightness. Like the incandescent lights in a house. Nop. A LED is a solid state device which emits full output when powered. (Your TV/CD/DVD/MP3 remote-control is flashing it's IR led at 40,000 times every second a button is being pushed. And that is just to establish a primary frequency between TV and remote.) They will also pass max voltage at max current and evaporate before your eyes if one does not use a current limiting resistor placed in series. With  5 v supply a red led would use 470 ohm. I use 2.2K with blue leds and they are still bright to my eye's.
      Ok back to the 50% duty cycle. I would say separate   |50%|  |duty|   and  |cycle| then look at each one. Also start each cycle with the led <on>.
        CYCLE  A cycle is a period of time which has a beginning and an end. Say a period of one second. For one second the led <could> be on or off.
        DUTY Refers to the amount of time the led <is> <on> during each cycle. The higher the percentage, the longer the led is ON.
        50% Means the led will be on for 1/2 of the cycle and off for the second 1/2 of the cycle.
     
     So if the cycle is 10Hz or 10 cycles per second, the led will be on off on off on off 10 times in one second. Yes you will see the led flicker. But if the number of cycles is increased to 100 per sec, the eye/brain will no longer see the flicker. It WILL however see the led as being at 1/2 brightness. If the duty cycle is 10% (led is on for one 10th of each and every cycle) and therefore looks to be dim. So it is not the light <source> we are dimming, but our perception of the <light's> brightness based on the length of time the led is <on> during each cycle.
      I hope this helps. Happy New Year to you.
     
     
    post edited by laurie21 - 2017/12/26 00:24:50
    #17
    PStechPaul
    Super Member
    • Total Posts : 2039
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/26 01:17:04 (permalink)
    0
    A while ago I worked with a friend to design a bright LED underwater flashlight with actual power of up to 50 watts with about 7or 19 high power LEDs. Above a certain amount of power, it was very difficult to ascertain changes in brightness, and it was far too bright to look at directly without pain and eye damage. It was more noticeable at a significant distance, and underwater. But that notwithstanding, above a certain power level, there was little change in actual brightness with an increase of power.
     
    The human eye has a remarkable range of perception of brightness, so in a dark room, it maybe possible to see a faint glow at a few microamps, while the LED may be rated at 30 mA or more. PWMbrightness range of 1000:1 and even 10,000:1 is achievable and useful. The eye has different sensitivities to various colors, and I think green is where sensitivity is greatest. Red light has less effect on night vision, so is preferred for low light conditions. The eye takes much longer to recover after exposure to blue and white light.
     
    Some more information:
     
    https://electronics.stackexchange.com/questions/1983/correcting-for-non-linear-brightness-in-leds-when-using-pwm
     
    https://www.jameco.com/Jameco/workshop/TechTip/light-brightness-intensity.html
     
    http://www.ti.com/lit/ds/symlink/tps61161-q1.pdf
     
    http://rohmfs.rohm.com/en/products/databook/datasheet/ic/power/led_driver/bd60a00nux-e.pdf
     
    http://www.archlighting.com/technology/leds-a-deep-dive-in-dimming_o
     


     
    #18
    CVRIV
    Senior Member
    • Total Posts : 125
    • Reward points : 0
    • Joined: 2017/09/06 13:49:25
    • Location: 0
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/31 20:36:18 (permalink)
    0
    Uhhh. So help me understand this. Maybe Im just super tired.

    I setup my TMR0 to interupt every 1ms. I named a register TimeOff, which is set for an amount of time that the LED is off. I wrote some code that basically assigns a value to a counter based on whether the LED is ON or OFF. If the LED is ON, assign the value of TimeOff to the counter. If the LED is OFF, assign the value of 0x0A - TimeOff. The code toggles the LED after the counter reaches 0x00.

    It works just fine. The LED was very dim. I want to see how dim I could get it, so I started playing around with it. I've come to the conclusion it can only get so dim before it starts flashing. Am I right?
    #19
    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: I have a question about fading out an LED. 2017/12/31 21:11:00 (permalink)
    0
    No. If it's flashing, it's a bug in your code.
    As several posters have already mentioned, PWM is the better way to do this.
    If it's being pulsed at 100Hz or faster, your eye cannot see the pulsing.
    You can make the pulses narrower and narrower until it's too dim to see.
    (Less than 1:1024 would require switching the PWM off and on regularly.)

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2018 APG vNext Commercial Version 4.5