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.

What is the function of R3?

That is a good question.  Most transistor circuits that I have seen put a resistor in that position.  Given that I have R4 I was not sure I needed R3 but put it in anyway.  You think I can get rid on R3?

As far as I can see, the only function for R3 is to limit power disspiated by the transistor/LED, which you can do with R4 anyhow.

 

Excellent - that's one less part I'll need then :)

Thanks for your help

You might want to try a 1uF cap and 22Mohm resistor, you can then use a 2Mohm pot to dial in the time constant you want.

I did a little bit of experimenting and found that a 470uF cap and 10k resistor seems to give a nice fade of around the speed I'm looking for.

I'll order some parts soon and eventually get around to writing a blog post about it :)

Thanks for the further help guys.

Rik, I did have a similar thought to you.  I thought if I moved R1 to be inline with the capacitor (see below) it would allow the steady state current through the LED to be 20mA while also charging the cap.  Unfortunately it does not work.  The LED does not fade in because the majority of the current takes the path of least resistance and heads for the 220 ohm pathway.  I assume the same thing would happen with the setup in your third diagram (I'll give it a try though if you think the diodes may change things?).

The sink/source from the microcontroller pin adds even more complexity.  I was aware of it but wanted to keep things simple to start with.

With a large enough cap it would probably be possible with my original circuit, but it's not really feasable to use a 1F cap to fade a single LED :)

Rogue, I'm trying to avoid the use of an IC if possible.  I'd give your method a go (just to see if it works) but I'm very green when it comes to op amps.  That is to say I'm not quite sure what you mean and I also don't have any laying around.

LED fade circuit

 

 

 

 

 

 

 

 

 

I think this one might be headed for the too hard basket - or maybe even the impossible basket.  For what I'm trying to do, using PWM wont be a big deal.

A buffer amplifier can use an 8 pin dip IC with some wire (no resistors), very easy and straightforward to setup if you get your hands on one which is really easy since Radio Shack stocks these for 99 cents.  Here is a short page on what a buffer amplifier is and how to wire it up:

http://www.facstaff.bucknell.edu/mastascu/elessonshtml/OpAmps/OpAmp3Note1Buffer.html

Don't be scared by operational amplifiers, there much easier than good ol' transistor circuits!

You might try using a buffer amplifier (unity gain op amp configuration) using something like an LM741 between C1 and your LED.  Then you can use the switch to alter between R1 connected to Vcc and Ground.  Your time constat tau = R1*C, and 99% of charging/discharging should take about 5*tau.  Be interested to know if this solution works for you.

Cheers

Thanks for that.

I think that what I'm trying to achieve is impossible.  To have the capacitor charge slowly requires a large value at R1, but then when the system is at steady state the current running through the LED is defined by R1+R2 and thus it's not very bright.  I've tired to think of a way around this but I reckon I might be bashing my head against the proverbial brick wall.

If anybody knows a solution, please let me know.

In the mean time...back to PWM!