LED and PWM

Hello Everybody

I am a complete newbie to Arduino and infact programming :P. I bought the starter kit and have been tinkering with it for quite sometime now and I have a basic question which has been puzzling me like anything.

I read about PWM and tried to replicate the dimming of a LED using arudino. The question I have is: What is the difference between the 2 codes I have written. I can clearly see the difference but not able to fully understand how it is actually happening.

  1. Lighting a LED to full brightness
int led =2 ;

void setup {
pinMode(2,OUPUT);
}

void loop {
digitalWrite(led,HIGH);
delay(1);
digitalWrite(led,LOW);
delay(1);
}
  1. Lighting a LED ,dimmed
int led =2 ;

void setup {
pinMode(2,OUPUT);
}

void loop {
digitalWrite(led,HIGH);
delay(1);
digitalWrite(led,LOW);
}

Ok, after racking my brain I came to this conclusion: In the first case, the led is switched on at t=0 and then after being lit for 1 ms, it turns off and after 1 ms is turned on again and the cycle repeats. In the second case, the LED is lit at t=0 and then after being lit for 1 ms is turned off but since the delay is missing (as compared to the first case) and the loop repeats, it is again turned on quickly and thus this repeats. Attached is a voltage vs time graph for the two cases that I could draw.

I have no idea whether my reasoning is correct or not, but I still could not figure out how does the LED get dimmed. Although I understood PWM, but the fact that LED brightness could be controlled by limiting its time for lighting is something I cannot comprehend.

Would be glad to get any help on this. I am confused totally :confused: I am new to this, please be polite :stuck_out_tongue:

Thanks!

What happens is that your eye integrates the flashing into a brightness equal to the average time the LED is on. So what you are missing is the way a human eye works. The ratio between the on time and the off time determine the brightness. On code 1 try making only one of the delays 5 and the other 1, then swap them over, can you predict the result?

With code 1 what you call full brightness is not as bright as if you never turn it off.

PS. How is the LED wired up? There are two ways to do it. One lights up the LED on a HIGH and the other on a LOW.

Hello Grumpy_Mike

Thanks for the reply.

I am trying to understand your statement and I actually read about human eye and its working (persistence of vision etc..), a couple of questions cropped up:

  1. Does it take some time for a LED to reach its full brightness (whatever that value be)?

  2. The fact where I am getting confused is that there are two ways (as far as I have read) to control the brightness of a LED - you either change the current or you change the duration for which it glows. Now I think that if by changing the time for LED to glow, you can vary the brightness, then that means LED achieving its full brightness has a time factor involved, so thus you reduce the time available for it to glow fully and thus reduce its brightness. However, I guess, its not the case and its something to do with eye, like you are saying. Pretty confusing :/

  3. Yes, actually I did fumble with LED wiring too. When I just connect the LED and use the digitalwrite(led,LOW), it glows, and when it is digitalwrite(led,HIGH), it doesnt.. So I am working to resolve this now.

Apologies, these might be basics, but I just cannot move ahead with such doubts, hence struggling :)

Thanks!

It takes time for an LED to a achieved full brightness, but this is way way shorter than any time we are talking about here so for practice purposes you can think of it coming on the instance power is applied to it.

Yes you can change the brightness of an LED by those two methods. But the in off method has nothing to do with the LED coming up to brightness, it is purely a function of the eye.

Yes there are two ways to wire the LED and resistor, from the output to +5 or from output to ground. There is nothing to resolve here both work. The connection to 5V is called sinking current and the othe called sourcing current.

Hi Grumpy_Mike

I guess, I understood now what you are trying to say. So its like this:

  1. As you said, neglecting the time taken by the LED to reach its full brightness (its instant), the reason why the on-off times for LED affects the brightness is because of the time to which our eyes are exposed to the light. Lesser the time, less brightness as perceived by the eye and vice versa. Am I right?

  2. I did what you told me, make one delay 5 and the other 1 and swap them, In one case, I have the LED brighter and the other case its dimmer. So, the difference between the two cases is that in the first one, when the first delay is 5 and the other is 1, the ratio of on and off is 5 and hence brighter and in the second case after swapping, the ratio is 0.2, thus dimmer. Is this the correct interpretation ? And considering PWM also, then this interpretation should be valid because, the duty cycle relates the average times (ratio) for on and off (through average voltage) and hence the brightness can be adjusted?

  3. And yes, I got that LED wiring thing. Earlier, I had the output on negative and now it works fine (with HIGH it is on) as I have the output on positive.

Could you please clarify if my conclusion is correct.

Thanks! :)

  1. ..... Lesser the time, less brightness as perceived by the eye and vice versa. Am I right?

Yes.

  1. ...... Is this the correct interpretation ?

Yes.

I think you've got it. :)

Thank you so much Grumpy_Mike! :slight_smile: :slight_smile:

And exploring and learning with Arduino continues … :smiley:

And exploring and learning with Arduino continues ............... :D

And in truth it never ends and is always most satisfying.

Grumpy_Mike: And in truth it never ends and is always most satisfying.

Agreed Sir ;) :)