fading leds with PWM

Hi there all, a new one here,

I am busy with this project to have multiple RGBW leds in series, controlled via an LM2596. Set to an constant current of 650mA (yet they are on 200mA while testing).
Now the next step was dimming/fading these leds, preferable with an variable resistor (potmeter) via PWM.
I found several different code’s. I tried some of them and modified them but I can’t get the right ‘frequency’ of pwm.
First i connected it to pin 13 of my nano and it was more a kind of stroboscope, only the very end was useable, but the frequency hearts the eyes and makes you fatigue. Then I connected it to 9, but still not the desired effect. Later on I changed the frequency to 33Khz with some help of google. But that still doesn’t work >:( .

Has anyone suggestions, maybe different code or other wiring? I will post the code when I am behind my desktop.
perhaps the pull-up resistor is too low, I thought it was 1K, I could try one 10K or two 10K parallel.

I use this scheme to shortcut the leds, with IRFZ44N mosfets:

http://www.rotormind.com/blog/2013/Dimming-a-LED-Lamp-with-an-Arduino/

I also found this post: http://forum.arduino.cc/index.php?topic=104268.msg1436767#msg1436767
But then I don’t know how to write the code. How do I know what for potentiometer is on the LM2596, though it seems very logic…
With this option I could as well solder the old potmeter loose, connect a new one and move it to the ‘control panel’…

To use PWM you have to connect to the pins that support it. These are usually marked on the board with a ~ next to the pin number. You will need to use a resistor between the Arduino pin and the MOSFET base pin. You should not need the 'optional' resistor on the GND line. You seem to be attempting to do PWM by shorting VCC to GND.

Riva: To use PWM you have to connect to the pins that support it. These are usually marked on the board with a ~ next to the pin number. You will need to use a resistor between the Arduino pin and the MOSFET base pin. You should not need the 'optional' resistor on the GND line. You seem to be attempting to do PWM by shorting VCC to GND.

I saw that, and pin 13 has no PWM indeed. So I now use the other pins. I will change the resistor the tonight!

Thats right.

Please do not implement that circuit, it will blow things up. The guy that wrote that page was an idiot. Shunting current from a constant current supply is just plain ignorant.

Also I would not recommend the circuit using the LM2596 in that link. That is way too advanced for a beginner to make and requires good layout.

tried some of them and modified them but I can't get the right 'frequency' of pwm.

Do you know what PWN does? Why would you want to change the frequency?

http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Grumpy_Mike: Please do not implement that circuit, it will blow things up. The guy that wrote that page was an idiot. Shunting current from a constant current supply is just plain ignorant.

Also I would not recommend the circuit using the LM2596 in that link. That is way too advanced for a beginner to make and requires good layout. Do you know what PWN does? Why would you want to change the frequency?

http://www.thebox.myzen.co.uk/Tutorial/PWM.html

Thanks. Ok I won't use it anymore, but then I am still stuck... I already got pre-made lm2596, as in the link. When I wired it up, it was more of a blinking program, and i could change the frequency with my potentiometer. But it wasn't the right speed for dimming. It was really a stroboscope...

But it wasn't the right speed for dimming. It was really a stroboscope...

Post the code because it sounds like you are going about things in totally the wrong way.

I believe i used this code:

int potPin = A0;    // select the input pin for the potentiometer
int ledPin = 9;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val );                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val );                  // stop the program for some time
}

I used a slight variation in this code to really make a stroboscope with white leds which i still had :p . That works good though. Except the capacitors from the lm2596 get really hot since I used 6 white leds (10 leds wouldn't even light up due to the voltage...).

So why do you think that will fade anything? Try this:-

int potPin = 0;    // select the input pin for the potentiometer
int ledPin = 9;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  // no need to declare the ledPin as an OUTPUT if you are using it to analogWrite to
}

void loop() {
  val = analogRead(potPin) >> 2;    // read the value from the sensor & scale it
  analogWrite(ledPin, val);  // write the brightness to the LED
}

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11.

To fade you have to apply a different duty cycle to the LED, your code simply turned it on and off at different rates but at the same 50% duty cycle.

Grumpy_Mike:
So why do you think that will fade anything?
Try this:-

int potPin = 0;    // select the input pin for the potentiometer

int ledPin = 9;  // select the pin for the LED
int val = 0;      // variable to store the value coming from the sensor

void setup() {
  // no need to declare the ledPin as an OUTPUT if you are using it to analogWrite to
}

void loop() {
  val = analogRead(potPin) >> 2;    // read the value from the sensor & scale it
  analogWrite(ledPin, val);  // write the brightness to the LED
}




To fade you have to apply a different duty cycle to the LED, your code simply turned it on and off at different rates but at the same 50% duty cycle.

Indeed that was the effect haha.
Have to find another code, and when then works. For 4 colors.
I used this code:

//LED fade

int ledPin = 9;//the Arduino pin that is connected to the LED

void setup() {                
  pinMode(ledPin, OUTPUT);// initialize the pin as an output
} 

void loop() {
  //ramp LED up to full brightness (0 to 255)
  for (int brightness=0;brightness<256;brightness++){
    analogWrite(ledPin,brightness);
    delay(50);
  }

  delay(100);// wait one second

  //ramp LED down to no brightness (255 to 0)
  for (int brightness=255;brightness>=0;brightness--){
    analogWrite(ledPin,brightness);
    delay(25);
  }

  delay(100);//wait one second
}

But the fading was way to fast, while i thought this gradually brightend the leds…

The fading is too fast because you misunderstand the number in delay. A one second delay needs a number of 1000 not 100. Increase the delays and it will get slower.

Grumpy_Mike: So why do you think that will fade anything? Try this:-

int potPin = 0;    // select the input pin for the potentiometer
int ledPin = 9;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {   // no need to declare the ledPin as an OUTPUT if you are using it to analogWrite to }

void loop() {   val = analogRead(potPin) >> 2;    // read the value from the sensor & scale it   analogWrite(ledPin, val);  // write the brightness to the LED }




To fade you have to apply a different duty cycle to the LED, your code simply turned it on and off at different rates but at the same 50% duty cycle.

I am goanna give your code a try, maybe tonight. Not sure yet, cause I have my birthday, but it would be great if the leds worked for my party tonight :p . The stroboscope already worked hehe, although the 6 leds ain't that much.

The code didn't worked for the fading... It responded very strange to turning the pot meter. The electronics on my breadboard have been destroyed since it fell and somebody stood on it -.- . So I have a slight delay in the project...

The code didn't worked for the fading..

Sounds like you had your hardware wired up wrong then.