Let's Make Robots!

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.

 

LED fade circuit

Comment viewing options

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

This may be a dumb question, but for less than 50 cents you can get LEDs that fade up and down by themselves so why tie up memory doing it?

Can you put up a link to these magical auto fading LEDs.  I have never heard of them before.

I get them off Ebay....just search.

All I could find on ebay were RGB LEDs that fade between colours at a fixed speed.  It's not really what I'm after.  I'd like to be able to control the fade speed and I only need a single colour.

They are pretty kewl though so I'll keep them in mind for other projects.

the colour-fading LEDS?

not quite the same function... rather different I think?

I'm not quite sure how this idea popped into my head but I started toying around with using a transistor.  Doing so meant that I could isolate the current for the LED from the current to the capacitor.  I came up with this circuit...and it works!  I'm so pumped!

When I press the button, the charge starts filling the cap.  As it does, more and more gets into the base of the transistor which in turn allows more and more of the collector current to pass throught to the emmitter and light the LED.  At steady state everything just hums along nicely with the LED at full brightness.  Then when I release the button, the current slowly drains out of the cap and reduces the current at the base, causing the LED to fade away.

The fade in and fade out are not exactly the same, but I think I can get them close by playing with the values of R1, R2, and the size of the cap.

 

LED fade circuit

 

 

 

 

 

 

 

 

 

Thanks rogue, I'll try to pick up one of the 8 pin op amps and have a play around (though I'll go to Jaycar, not Radio Shack, since I live in Australia).  I really should learn about how to use them.

Glad its working better.  I noticed you've added D1 and R1, along with the ground I suggested connecting to the switch.  The diode blocks Ground as a discharge path so it can be left out.  It looks like your discharge path is R1+ R2 or 11kohm.  I assume its charging a bit faster than its discharging. Anyhow its nice to see your circuit evolving.

I originally left out D1 and R1 so that it would charge and discharge though the 1k resistor.  However it seemed to discharge much faster that it was charging.  Come to think of it though, I may not have given the cap enough time to fill right up - so it was discharging from only half full!  I might give that another go.

A capacitor should charge and discharge at the same rate (throught the same amount of resistance) shouldn't it?

A capacitor should charge and discharge at the same rate assuming the charge path and discharge path see equal resistance, which is what you are aiming for.  The reason I suggested an op amp is so it wouldn't load your circuit, and when the switch was flipped the discharge path would have the same resistance to ground as it did while charging.  Hope thats clear enough.  I will have to brainsorm some more if you need to do it using a transistor.  The most important thing to do when designing your circuit will be to look at your capacitor discharge path to charge path and compare the resistance.  Tis a fun exercise in designing a circuit to do what you want.

I tried without the diode and extra discharge path, and the LED does indeed fade in and out at the same speed as long as I give the cap enough time to saturate.  So this is the current state of my circuit - with the switch representing the i/o line from a microcontroller.

LED fade circuit

 

 

 

 

 

 

 

 

I'm quite happy with it all really.  Unless you can see any major issues with it, or ways to improve it?