 How to use Pulse Width Modulation (PWM) to change the brightness of a LED

Lighting an LED with a digital output pin is one of the simplest things you can do with a microcontroller, but how do you use pulse width modulation (PWM) to change the brightness of a LED. The answer to this question is very simple, so we’ll show you how to do this using the Arduino platform further down the article.

Luckily the microcontroller (ATmega328) that the Arduino uses has several built in pulse width modulators (PWM) which we can use to adjust the brightness of the LED programmatically without the need to change the LED’s current limiting resistor or even doing it manually with a variable resistor.

In this tutorial we will look at what PWM is and how it can be used to vary the brightness of an LED with coding and how to set up a PWM pin on an Arduino Uno.

The video below shows the LED ramping up brightness in a continuous loop.

What is Pulse Width Modulation (PWM) ?

PWM stands for Pulse Width Modulation which is a signal technique where the width of a pulse is varied. The pulse has a set frequency and in the case for Arduino Uno it is set at 500Hz. The width of the pulse is varied in time so you can have a short or long pulse.

At the two extremes we have the pulse high all the time which is effectively a DC signal or a pulse with low all the time with no amplitude at all which is equivalent to 0V or ground.

The diagrams below show some examples of pulse widths that have duty cycles of 100%,  50% and 0%.   How can Pulse Width Modulation vary the brightness of a LED ?

To put the arduino microcontroller’s PWM to practical use, we can connect one of the PWM pins to an LED and use it to adjust it’s brighness with only the use of code and no external components with the exception of a current limiting resistor.

The current limiting resistor is placed in series with the LED to limit the maximum amount of current that is available to it at full power. Every LED has different specifications, but the white LED we are using can handle a maximum current of 20mA. To be on the safe side we will limit the current to 15mA which is still very usable.

Looking at the diagrams above of different PWM signals, we look at the 100% duty cycle, it is high 100% of the time, so this will be full power for the LED. At 50% duty cycle, the signal is high for 50% of the time delivering 50% of the power to the LED. And lastly when there is 0% duty cycle, the signal is low 100% of the time so the LED has no power at all so remains off.

Coding the Arduino’s pulse width modulator to adjust the brightness of the LED.

To use the pulse width modulator in the Arduino Uno all we need to do is setup one of the PWM pins as an output then we call up the analogWrite command and set a level to output. The frequency of the PWM is set to about 500Hz so we don’t need to worry about this part.

We select pin3, set it as an output and analogWrite a value to it. When selecting the PWM output, it is bits so we have 256 levels to choose from. The duty cycle level can be set between the numbers of 0 and 255, where 0 is 0% duty cycle and 255 is 100% duty cycle.

The output of the microcontroller’s pin on the Arduino Uno board is 5V. To set the LED we have chosen to full brightness we need to supply the voltage of 3.3V and 15mA of current. To do this we drop down the voltage with a 100 ohm resistor.

PWM Arduino Schematic PWM Code The example code above sets up pin 3 as an output and then writes a value to the analogWrite() command. The value can be any where from 0-255. We start the PWM at 0 and increment the value by 1 for every loop cycle with a delay of 3mS between each cycle. When running (as can be seen on the video above) the LED starts in the off position and gains brightness until it is at full power then the loop resets the brightness variable to 0 then ramps back up to 255 in an endless loop.

How to use Pulse Width Modulation (PWM) to change the brightness of a LED | Summary

So pulse width modulation (PWM) is an easy way to vary power to a component using code. However if more power is needed than what the microcontroller’s pin can supply, an external driver circuit would be required.

I hope this tutorial was useful to you.

Thank you for spending your time to read this article, I really appreciate it !