NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Support Forum » Dimming an LED
August 03, 2009 by pedroh96 |
Hi Everyone, How can I slowly dim an LED? What I need is to do this: an animation with one sec. duration from brightness 0% to brightness 100% in the LED connected to port PC0 on the MCU. How can I simply do it? The code that I have now is here: http://pastie.org/private/zqzgxxtdecy6z87zfywyeq Thanks! |
---|---|
August 04, 2009 by hevans (NerdKits Staff) |
Hi pedroh96, The easiest way to do what you desire is to PWM the pins that the LEDs are connected to. By blinking the led on and off really quickly it will appear to the eye as if it is dimmer. If you have a duty cycle of 50% on the led it will be about half as bright, and a duty cycle of zero will be completely off. You cam use the PWM pins on the chip like we did in the Servo Squirter Tutorial. What you want to do is set the duty cycle of the pin, and then slowly decrease the value in your compare register to create a dimming effect. I would try it with 1 LED first just to make sure you get how it works. We used this idea of PWM on an led to simulate dimming in the LED Heart Video. In that video we do not directly use the PWM pins instead we use timers and interrupts to generate the effect, however the idea is exactly the same. Humberto |
August 05, 2009 by pedroh96 |
Hi Humberto, Thanks for the reply. I've been trying to do the dimmer with this, but I'm not having the dim effect:
Can someone help me doing that code? Thanks, pH. |
August 05, 2009 by wayward |
pH, that loop keeps the laser on most of the time, and blinks it quickly every 15 milliseconds. If you "unroll" the loop, you'll see what's happening:
As you can see, the laser stays on during the delay, then it is switched off only until the loop counter increments, and is switched on again immediately after. You would not be able to see this with the naked eye (please don't look directly at the laser beam, anyway!) See the LED Valentine Heart for a good solution that has variable light intensity and doesn't get you locked in a loop. In essence, you'll use a timer interrupt to trigger periodically. Each time the interrupt triggers, you increment a counter variable, flipping it over to 0 when it oversteps a certain upper limit, say 64 (as in the LED Heart); then you compare the counter's value to the stored "intensity" of the laser, which may be between 0 (fully off) and 64 (fully on). If the stored "intensity" is below or equal to the current counter's value, then you switch the laser on; otherwise, you switch it off. Intensity is actually the "duty cycle" of the laser, meaning what proportion of a cycle (64 interrupts) the laser stays "on". A duty cycle of 0/64 = 0 means it's completely off, while a duty cycle of 64/64 = 1 means the laser is on. A duty cycle of 32/64 = ½ means that the laser will be switched on for 32 interrupt events, and off for the remaining 32. That will be perceived as being half-bright. LED Heart contains everything you need to make a good, robust duty cycle control. Try to make the code work with your laser and let us know how it's going! Zoran |
August 05, 2009 by pedroh96 |
Zoran, I understand what you told me, but I didn't get WHERE in the CODE of the LED Valentine Heart the duty is setted. Can you tell me the function it is setted (and actives the LEDs with PORTC)? This will help me a lot. Thanks! |
Please log in to post a reply.
Did you know that an electroluminescent backlight for an LCD panel requires hundreds of volts AC to run? Learn more...
|