View Full Version : Dim a led

02-09-2011, 04:43 PM
How can I dim a led using microcontroller at89s52

I want it by using PWM method ,

but unabe to do it
its works perfectly to slow motor , but does not work for led

sample code to lower the brightness of led


sbit s=P1^0;

unsigned int i;

void main()







led is connected to pin p1.0;
i noticed nothing different the led glow with same brightness as without these loops when i directly glow a led

02-09-2011, 06:31 PM
If you are trying to vary the brightness of the LED, then you will have to vary the LED On and Off times. Presently they will both be equal in your program. Reduce the LED on time and increase the off time and you will see change in the brightness.

Just Google search for "LED brightness control through PWM" and you will get some good tutorials. Just make sure you have the right duty cycle and frequency, and you should be able to achieve the task.

02-09-2011, 11:14 PM
thanks pratheek its Works!!!
when we take on time is less then off time

but when i try it as:
when on time is more then off time i noticed no change i am unable find reason behind this.

02-10-2011, 08:23 AM
Something like this?


Most modern MCUs like a PIC and AVR has a dedicated PWM units that can do PWM without CPUs help.

02-10-2011, 09:23 AM
Increase the frequency of your PWM signal and you will start noticing the difference.

02-10-2011, 01:29 PM
when on time is more then off time i noticed no change i am unable find reason behind this.
Your circuit or your LED is not at fault. Its something, well, physiological!

The human eye has a logarithmic response to brightness. Which means that at high levels if you double the brightness (through PWM or wattage etc.), it doesn't necessarily mean your eye will perceive it to be twice as bright. So that's the reason behind the tiny bit of difference that you are seeing despite increasing PWM duty cycle levels drastically. However at lower levels of brightness, an equal decrease in duty cycle will result in a decrease in brightness that's more obvious. In very crude terms, the human eye is more sensitive to low intensities and less to high intensities.

Its the same with the human ear. Measure the resistance of a volume adjusting potentiometer (available at audio repair shops). Plot a curve of resistance (y-axis) versus the angle (x-axis) rotated. You'll see a logarithmic curve.

Interesting, right?!