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.

 

your problem is that the

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:

 

Thanks

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!

Two way lane - split up into two one way lanes

Diodes?

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

Almost sounds like a 555 PWM circuit.

But would it work?

A schematic say more than a thousand words.

ledglow-rc-wontwork.png

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

ledglow-rc-wontwork-eq.png

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.

ledglow-rc-mightwork.png

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...

ledglow-rc-wth.png

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!

ledglow-rc-wth-better.png

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.

A thought

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 = R1C, and 99% of charging/discharging should take about 5tau.  Be interested to know if this solution works for you.

Cheers

Thanks

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 :slight_smile:

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.

 

 

 

 

 

 

 

 

 

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.

the basic op amps are straightforward

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!

IT WORKS!

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.

 

 

 

 

 

 

 

 

 

 

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.

Awesome

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.

Thanks

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

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.

Even better

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.

 

 

 

 

 

 

 

 

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

 

You might want to try a 1uF

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.

470uF and 10k

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 :slight_smile:

Why make it hard?

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?

you can?

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

What is the function of R3?

What is the function of R3?

good question

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

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.