# Fading an LED with hardware

Hi guys,

I am trying to get an LED (attached to the i/o port of a microcontroller) to fade in and fade out without using PWM.  I hooked up a resistor and LED pair in parallel with a large-ish polarised capacitor, thinking that as the cap charged it would prevent some current getting to the LED, resulting in a fade up to full brightness.  When power was removed the cap would then slowly empty it's charge through the LED, resulting in a fade out.  It worked for the fade out but not the fade in; it simply went to full brightness when power was applied.

I realised I would need another resistor between power and the cap to limit the speed at which he cap could charge.  I set this up (see figure below) and it is kind of working; I get both a fade in and a fade out.  But the fade in speed is not the same as the fade out speed.

I'm wondering if anybody knows if there is a way to calculate what resistor/capacitor values will give different fade in/out speeds?  I'm also curious if it's possible to calculate how much current is passing through the LED?  I used a 220 ohm to set a current of 20mA when supplied with 5V, but I'm guessing this all goes out the window now that the cap and extra resistor are attached.

## Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Diodes?

Charge throught D1 + R1.
Discharge through D2 + R2.
Fiddle the R-values.

Almost sounds like a 555 PWM circuit.

A schematic say more than a thousand words.

This is what I tried to suggest, but now I think it won't work. Look at this:

It's the equivalent schematic. Current will flow directly through diodes and resistors to the LED. Sure, the discharge and charge times will be different, but the required resistors will also resist the LED current. So let's try to isolate R1 and R2 from the LED current.

This one might work. I have no idea. R1 and R2 could be combined into one potmeter. Give it a try! And while you're at it...

Replace D1 and D2 with LEDs. These will glow only shortly after a change in the switch position. But wait. How is C1 ever gonna discharge? Oh right!

Don't forget to model your output pin as a double throw switch connected to either V+ or GND. Your MCU will sink approx. as much current as it will source. There's our discharge path!

But will it work? You might have to use a much bigger capacitor! Or smaller LEDs.

your problem is that the voltage does not drop linearly with time

see this link for details http://www.technologyuk.net/electronics/electrical_principles/rc_circuits.shtml

I think the most important graph on that page is this: