LED fading with for loop

Hi to all,
First of all I must say i am very new in Arduino, and sorry for bothering with such a simple question..

I wanted to control my led brightness with changing it's voltage between 0 to 5v.
As I read in arduino website with its pwm control it can simulate the voltage between 0 and 5 v (analogwrite(255) for 5 v and analogwrite(0) for 0 v)

so I tried this as my first project
http://arduino.cc/en/Tutorial/Fade it works like charm.

Here is what i wonder..
I set my brightness (pwm) to 255 (maximum value) and
I removed the resistor and directly connected led with my arduino ( to get higher voltage and brightness and my led still works well) but when I measure the voltage between the legs of my led i get 2.75 V . Shouldn't it be 5 V??

Also I tried to make the fading program with for. I wrote this and it works well in C++ program (the output is 0 5 10 15.. 255 250 245 240... 0)
But when I tried this in arduino program my led didnt emit any light.
for(a=0;a<=255;a=a+5)
for(a=255;a>=0;a=a-5)

Does anybody knows whats wrong with this code? or can anybody rewrite the fading program with using "for".

Thanks a lot.

Does anybody knows whats wrong with this code?

We cannot see your code, attach it all.

I dont get any error message but it doesnt work. here is my code. Also have any idea about 5 v issiue?
Thanks for your time.

int a= 0; // i use this for brightness

void setup() {
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(9,a);
while(1){
for(a=0;a<=255;a=a+10);
for(a=255;a>=0;a=a-10);
}

// wait for 30 milliseconds to see the dimming effect
delay(30);
}

karadede:
I wanted to control my led brightness with changing it's voltage between 0 to 5v.
As I read in arduino website with its pwm control it can simulate the voltage between 0 and 5 v (analogwrite(255) for 5 v and analogwrite(0) for 0 v)

PWM does not "simulate" a voltage. You can use a resistor/capacitor filter on the output, to end up with a variable voltage. The way a PWM signal changes brightness on an LED is that it switches the LED on and off, and the longer it is on in each PWM cycle, the brighter it seems.

http://arduino.cc/en/Tutorial/Fade it works like charm.

Here is what i wonder..
I set my brightness (pwm) to 255 (maximum value) and
I removed the resistor and directly connected led with my arduino ( to get higher voltage and brightness and my led still works well) but when I measure the voltage between the legs of my led i get 2.75 V . Shouldn't it be 5 V??

You are trying to read a digital signal that is going on and off, and your DC meter will not give you a meaningful value.
When you send a PWM of 255, you would get a reading of 5V if you were putting it across a resistor. But you are putting it across an LED, and the LED is a non-linear device that will limit the voltage to somewhere just above 2 Volts (more or less depending on the LED (Blue and white take more voltage, red takes a little less). When your LED begins to light, any more voltage applied will cause more current to flow, keep increasing it, and the current goes VERY high while the voltage remains just above the point where it started lighting.

Because of this, it is NOT a good idea to remove the resistor, as it will cause a very large current to flow through the LED and worse, through the output circuit of the pin on the Arduino, likely a lot more than it's rated maximum current. You will be doing damage to your LED and to the AVR micro on the Arduino.

Also I tried to make the fading program with for. I wrote this and it works well in C++ program (the output is 0 5 10 15.. 255 250 245 240... 0)
But when I tried this in arduino program my led didnt emit any light.
for(a=0;a<=255;a=a+5)
for(a=255;a>=0;a=a-5)

Does anybody knows whats wrong with this code? or can anybody rewrite the fading program with using "for".

I see you finally posted your code, and yes, I do see something wrong with it.

The most glaring error is that you do an analog write of 0 to the LED's pin, then you enter an infinite loop in which ALL you do is cycle the value of a variable. You never change the PWM output on the LED's pin. Changing the variable jist.. well, changes the variable. it doesn't do anything else with tha variable

while(1){
  for(a=0;a<=255;a=a+10);
  for(a=255;a>=0;a=a-10);
  }

This while loop never ends and even if it did all the for loops do is to make a go from 0 to 255 then back down to zero again over and over again. The value of a is never even used as the while loop never ends.

There is no need for a while loop because the loop() function repeats anyway as the comment says. Take it out along with one of the for loops, change the order of the code and remove the semi-colon from the end of the for loop line, tidy up the code to make it easier to read and you get

void loop()  
{ 
  for(a = 255; a >= 0; a = a - 10)
  {
     // set the brightness of pin 9: 
     analogWrite(9, a);    
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
}

Thanks a lot for the answers.

Dear lar3ry,
I have read in here (http://arduino.cc/en/Tutorial/PWM) that pwm simulates voltage so that was why i said that.
After I read your answer I measured my open circuit voltage with pwm=255 and i measured 4.96 V (which i think we can assume 5 V)
and then I removed my led and i just put a resistor, as i understand from your answer resistor is a linear device so i must be measuring something around 5 or 4.96 V. When I did that i measured around 4.30 V. (no led only resistor). So Where is that 0.66 V goes? (4.96-4.3).

I totally understand the reason of using resistor with leds so thanks for that ( I will not remove my resistor again.)

Dear UKHeliBob,

Your code is working and I understand the usage of void loop thanks a lot. but I want it brightness to change up to down and then down to up.
With your code, brightness starts from 255 and goes down and down until 0, and then it becomes directly pwm=255 again (it does not increase up and up to 255)
when i write for(a=0;a<=255;a=a+10) this time brightness goes up and up until pwm=255 and then it directly gets into 0 (it does not decrease to 0). I totally understand that behaviour. But Since I want it to increase it from 0 to 255 and then decrease it from 255 to 0 (not directly jumping from 0 to 255 or 255 to 0) I wrote
for(a=0;a<=255;a=a+10)
for(a=255;a>=0;a=a-10)
But it didnt work! Thats where i stuck, it still jumps from 0 to 255.

I hope my english is clear enough. Thank you all again for taking your time and bothering you with such a questions.

Best wishes.

I wrote

for(a=0;a<=255;a=a+10)

for(a=255;a>=0;a=a-10)

Right idea, but you are not setting the brightness of the LED anywhere

void loop()  
{ 
  for(a = 255; a >= 0; a = a - 10)
  {
     // set the brightness of pin 9: 
     analogWrite(9, a);    
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);                            
  }
//put a second for loop, analogWrite() and delay() here to fade from 0 to 255
}

Aahh! Thanks a lot !

That was what i wanted it works!

So I have to write Analogwrite and delay function for each "for loop".

Any answer about my voltage question is also welcomed :slight_smile:

Warm regards.

Measure the voltage from the output to ground.
What is it?

So I have to write Analogwrite and delay function for each "for loop".

The way it is written at the moment, yes, otherwise the LED output will never change.

How are you measuring the voltages that you refer to ? The PWM output goes from approximately 0V to 5V and back continuously. It is the amount of on time in each on/off cycle that causes the output to simulate a varying voltage. If you are not measuring the voltage with an instrument that can keep up with and display the on/off signal, then what you will see is an apparently varying voltage. Looking at the PWM output with a 'scope would be instructive.

karadede:
I have read in here (http://arduino.cc/en/Tutorial/PWM) that pwm simulates voltage so that was why i said that.
After I read your answer I measured my open circuit voltage with pwm=255 and i measured 4.96 V (which i think we can assume 5 V)
and then I removed my led and i just put a resistor, as i understand from your answer resistor is a linear device so i must be measuring something around 5 or 4.96 V. When I did that i measured around 4.30 V. (no led only resistor). So Where is that 0.66 V goes? (4.96-4.3).

The resistor provides a path to GND. Try increasing the value of the resistor. As well, the PWM signal may not go all the way to the 5V rail.
Glad to see you got it working.

When i measure the voltage I set the pwm value to 255, so it is not varying anymore. and i measure the voltage between the ground and analog 9 output.
As I said with this method I measured 4.96 V.(which is almost 5 V as it must be).
When I connected led and measure the voltage between led legs (pwm is still set to 255) i measured around 2.75 V.
It was said that, that 2.75 V was because the LED is a non-linear device. But since resistor is an linear element i should be measuring 5V.
so then i decided the measure the voltage of my resistor (no led connected, only resistor and pwm is still 255) and I measured 4.30 V. (simply resistor is connected between analog 9 and ground), so i expected to measure someting around 5V (like 4.96 V) but I measured 4.30 V.

The only thing i do is putting a serial resistor, shouldnt I be measuring 4.96 V?

Where is that 0.66 V goes?(4.96-4.30)

At one point I believe you said you connected the led to the o/p with no resistor. This may have damaged the o/p.

For your reference, see

so then i decided the measure the voltage of my resistor (no led connected, only resistor and pwm is still 255)

What value is the resistor and how much current is flowing through it ?

My resistor is 220 ohm and around 75-76 mA current flowing on it.

Thanks a lot for answers. lar3ry and LarryD.

But even if the case is as you said. I can measure the 4.96 V (between digital 9 and ground when nothing is connected.) and when i just connect serial resistance to it i measure 4.30 V on resistance.
So simply Shouldnt i be measuring 4.96 V??
( even if my o/p is damaged or even if pwm not go all the way to 5 V)

Am I doing a very basic mistake here?

around 75-76 mA current flowing on it

In which case this page may be of interest Arduino Playground - HomePage
Particularly this part

Absolute Maximum Ratings

DC Current per I/O Pin ........... 40.0 mA

With o/p at 255.
Assuming you have R going from +5 to o/p.
Measure voltage across R.
Measure o/p to GND.
Is the total 4.96?

At 220, 5/220=22.7ma

karadede:
My resistor is 220 ohm and around 75-76 mA current flowing on it.

Actually, at a nominal 5V, your current through a 220 ohm resistor will be 22.7 mA.
If you are drawing 75 mA, you will almost certainly damage the pins on the chip.

But even if the case is as you said. I can measure the 4.96 V (between digital 9 and ground when nothing is connected.) and when i just connect serial resistance to it i measure 4.30 V on resistance.
So simply Shouldnt i be measuring 4.96 V??

If you look at Figure Figure 29-162.ATmega328P: I/O Pin Output Voltage vs. Source Current(VCC
= 5 V) in the Atmel Datasheet, you will see that the output voltage drops to just under 4.6 volts when it is sourcing 20 mA.

And, I would suspect that the missing .6 volts could well be the drop from the high-side transistors at the output of the port.