# How to Make Control the Light Intensity of a LED Using PWM

In this tutorial, we will cover the basic principles behind PWM and how it can be used to control the light intensity of a LED rather than just turning them on and off.

A technique used to encode a message into a pulsating signal is pulse-width modulation (PWM) or pulse-duration modulation (PDM). It is a type of modulation. While this modulation technique may be used to encode data for transmission, its primary use is to provide control over the power supplied to electrical devices, especially inertial loads like motors.

Turning the switch between supply and load on and off at a fast rate regulates the average value of voltage (and current) supplied to the load. The total power supplied to the load increases when the switch is on for longer periods than it is off.

The load (the device that uses the power) must perceive the resultant waveform as smoothly as possible; hence, the PWM switching frequency must be substantially greater than what would impact the load.

The term duty cycle describes the proportion of “on” time to the regular interval or “period” of time; a low duty cycle correlates to low power since the power is off for the majority of the time. The duty cycle is expressed in percent, with 100% fully on. Hence, if the duty cycle for an Arduino is 50%, that implies that it is supplying 0.5*5V = 2.5V.

The Arduino can also give a PWM output; however, not all of the pins can be used for providing a PWM output. PWM may only be used with pin numbers that are preceded by a tilde (“). There are a total of six such pins on an Arduino UNO: 11, 10, 9, 6, 5, and 3. In our project, we use pins.

## How To Connect a Potentiometer to a LED?

### Potentiometer

A potentiometer measuring instrument is essentially a voltage divider used for measuring electric potential (Potentiometer); the component is an implementation of the same idea, thus its name.

Node A is connected to 5V of the Arduino, node B is connected to GND, and node W is connected to analog input A0. For current to flow, the Arduino supplies a 5 V potential difference. The wiper serves as the potential, and the current that passes through the divider is what is measured.

### LED

With the Arduino's 1K ohm resistor, which generates a PWM output, the cathode is connected to GND and the anode is connected to pin 9.

## Program

To read the potentiometer value, I used the analog read. They range from 0 to 1023 since the Arduino's ADC has a 10-bit resolution. My analog read input ranged from 116 to 924 in my project, I discovered. So I used the function x=map(x,100,1000,0,255) to map the values from 0 to 255. I then gave my LED this as an output.

## Conclusions

Although it is completely feasible to control the light intensity of a LED using PWM, calculating the required optimal values enables more effective use of the processor's resources, enabling you to accomplish more with the microcontroller and/or control more LEDs simultaneously from the same chip. Motors, analog meter displays, incandescent light bulbs, and other devices may all be controlled using the same techniques as those used to control LEDs.