Capacitor fading led on a digital pin

Hello folks!
I am trying to make a primitive sound reactive LED. I want the led to fade down nicely after it is activated. I have been doing this programmatically using analogWrite but decided to try it out using a capacitor instead so i might be able to free up a spare analog pin.
When i connect my capacitor to the positive foot of the led and then to ground and excite it from the 5v supply it fades down just as i would like, however when i try and excite it from the digital out pin there is no fade which causes me to think that when the pin is LOW it is grounded which is causing it to "suck up" all my spare juice. Is there any way around this little conundrum? Many thanks!

Aha! I just did it as per:

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(7, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  pinMode(7, INPUT);   // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Are there any problems which could arise from changing the status of your pins on the fly like this? Thanks!

pinMode(7, INPUT); // turn the LED off by making the voltage LOW

I don't know if it relates to your problem, but that comment is wrong. An input is effectively open, that is hi-z (many megohms input impedance). Not LOW, not HIGH.

Changing pinmode on the fly is done often. See the Ping sensor library.

groundFungus:
I don't know if it relates to your problem, but that comment is wrong. An input is effectively open, that is hi-z (many megohms input impedance). Not LOW, not HIGH.

Changing pinmode on the fly is done often. See the Ping sensor library.

No i was lazy with the commenting there, i just modified the blink example. thanks for your help!

gonadgranny:
Hello folks!
I am trying to make a primitive sound reactive LED. I want the led to fade down nicely after it is activated. I have been doing this programmatically using analogWrite but decided to try it out using a capacitor instead so i might be able to free up a spare analog pin.
When i connect my capacitor to the positive foot of the led and then to ground and excite it from the 5v supply it fades down just as i would like, however when i try and excite it from the digital out pin there is no fade which causes me to think that when the pin is LOW it is grounded which is causing it to "suck up" all my spare juice. Is there any way around this little conundrum? Many thanks!

You have no current limiting resistor in series with the LED? You should use one.

You are connecting an OUTPUT pin directly to a large capacitor? Don't do that, again use a
series capacitor to limit the pin's current to a safe level (below 40mA).

Yes an output pin is either LOW or HIGH, you need to set pin mode to INPUT after charging the capacitor:

void loop ()
{
  pinMode (pin, OUTPUT) ;
  digitalWrite (pin, HIGH) ;
  delay (1000) ;  // charge via resistor (220 ohms is reasonable
  pinMode (pin, INPUT) ;
  delay (4000) ;  // discharge via LED and its resistor (330 ohms and up is reasonable)
}

Hi,
You can use a diode in series with the arduino output pin so when you turn it low it will isolate the circuit. Attached it is the schematic showing how to do it.

led-delay.pdf (16.2 KB)

1 Like

@tauro0221, why on earth would you do so if you can just do that in software?

And switch the digitalWrite( ,HIGH) and the pinMode() :wink: Now the pin is LOW very (very) briefly which will sink extra current and you might see it.

And yeah, it's a very crude way. Not very much control over the fading this way.

Hi,
You like to do it software and I like to do it hardware. Just a simple diode will do the job. You can adjust the led fading by changing the capacitor or the resistor. Just because you want to do it software no mean that you can imposed your way of thinking.

1 Like

@tauro0221, you're also a fan of external pull up resistors? And external BOD? And external debounce? ::slight_smile: Simply code what you actually want to do and that's letting the pin float...

I once made a little function to make it easy

//! Enum to easy set the floating state of digital3State()
enum digital3State_t{FLOAT = 2};

void digital3State(byte pin, byte mode){
  switch(mode){
    case 0:
      digitalWrite(pin, LOW);
      pinMode(pin, OUTPUT);
      break;
    case 1:
      pinMode(pin, OUTPUT);
      digitalWrite(pin, HIGH);
      break;
    case 2:
      pinMode(pin, INPUT);
      break;
  }
}

//possible calls
digital3State(pin, LOW);
digital3State(pin, HIGH);
digital3State(pin, FLOAT);

Hi,
You do not need to explain me what it is the problem. That it is why the diode suggestion. He needs to isolate the pin from the Arduino output so the led discharge the capacitor. That it is what the diode does. Again the hardware solution it is as good as the software. The problem it is how you tell somebody that there it is a better solution. That does not means that the use of the diode is not. No?

1 Like

Yes it works, but I do think the software solution is better :wink: Why add something external that's already in there under software control? Plus it does give you the extra option to turn it off quick if you ever want to. Same for external pull resistors. I still like the old "Don't fix something in hardware that can be fixed in software" :slight_smile:

Hi,
That it is the right answered for my suggestion. Thank you.

Hi all. MarkT, i did have a current limiting resistor attached to the leds negative foot as i always do but did not think to put one between the capacitor and the output pin. It makes sense to do this as i can imagine that they suck up current pretty quick. Thanks.

Tauro0021 - thanks for the idea with the diode. Its always good to see alternative approaches to solving a problem.

septillon - were you saying that by changing the pinMode to input it achieves the same effect as putting a diode in series? also you mentioned that it i a crude way. would you personally rather use software to fade your leds? My main reason for trying this out is my program is getting bigger and bigger, i have a bunch of servos and leds connected i wanted to try and offset some of the work to hardware. Is looping through 255 - 0 to change analogwrite much of a burden on the cpu? i have become aware from a previous post that the actual analogWrite function is not a burden at all but the looping is something which i can imagine could take its toll?

Thanks again all!

gonadgranny:
were you saying that by changing the pinMode to input it achieves the same effect as putting a diode in series?

Almost, switching between a HIGH output and a INPUT (aka floating) does the same as having a diode connected (with anode to the Arduino) and driving the pin (as output) HIGH and LOW. In both cases the pin can one source current, not sink current. (Presumed that the voltage isn't higher then Vcc.)

gonadgranny:
would you personally rather use software to fade your leds?

Definitely :slight_smile: Way more control and the option to make the dimming more linear to our eyes by implementing gamma correction. See my footer :slight_smile:

gonadgranny:
Is looping through 255 - 0 to change analogwrite much of a burden on the cpu?

No, not if you don't use delay() anywhere in you code :slight_smile:

If yoou really want a hardware fade the enclosed will do it . A negative-going edge will start it.

But PWM gives you better control with less components

Allan

ledfade.pdf (15.2 KB)

Thanks for this septillion, I shall check out your link and Its nice to know that those little chips are so hardy! My main reason for looking away from software was due to my servos going all shaky when i started up some PWM pins. I now know that this was not due to the cpu being overburdened(as i originally thought) but by a timer conflict. I still think that an RC circuit could be useful for when you run out of PWM pins as i dont believe that i am incorrect in thinking that softPWM is indeed a burden on the CPU?

AlanHirst - thanks I'll take a gander at this later!

softPWM is indeed a lot harder on the uC. But depending on the application, that might even be a fine solution.

And if you need to drive more leds you could also use an external PWM IC :slight_smile: Same (or better) control as the on-board PWM and will take a whole less space then a dozen of resistors and capacitors. And costs about the same or less.

And if you need to drive more leds you could also use an external PWM IC :slight_smile: Same (or better) control as the on-board PWM and will take a whole less space then a dozen of resistors and capacitors. And costs about the same or less.

That did cross my mind. I thought about using an attiny85 as they are pretty cheap and economy is important. Could you recommend any others?

The PCA9685 is very common. Or the TLC5940. Both can drive 16 channels with 10 or 12-bit.

Great thanks!