Arduino Tutorial # 17 How to control brightness of LED with Potentiometer.

How to control brightness of LED with Potentiometer.

Hi guys, in this tutorial we will see how to control brightness of LED with potentiometer. So let’s get started.

For this you will need

  1. Arduino,
  2. 3 pin Potentiometer,
  3. Breadboard,
  4. Jumper wire,
  5. LED,
  6. 1k ohms resistor.

A potentiometer is an electronic component in which we can adjust current manually by increasing or decreasing resistance. This is used in many application such as fan regulator, control brightness of light, volume control knob in music system.  Do connection as shown in diagram.


Circuit diagram:


Control brightness of LED with Potentiometer
Control brightness of LED with Potentiometer


int LED = 11;

void setup() {

pinMode (LED, OUTPUT);

void loop() {

int potentioValue = analogRead(A1);
int writevalue = (255.0/1023.0) * potentioValue;

analogWrite(LED, writevalue);



Now let’s come to the programming part. Define LED pin number to 11. Make sure which ever you choose pin number it has to be PWM pin. In setup function pinmode of LED is output. In loop function we are defining integer potentioValue and assigning it to analogRead A1. Whatever A1 get input from potentiometer. Its value will be assign to potentioValue. In next line, here is also we are defining integer writevalue and assigning it to a formula for mapping analog input to PWM output.


Analog and PWM range
Analog and PWM range


Analog input ranges from 0 to 1023 and PWM output ranges from 0 to 255. So we should match this value in one scale. Therefore we have divided 255 with 1023 and multiply with potentionValue. Its maximum value will be 1023 so it means we get 255 as an output. If we don’t map this value what happen??

without mapping analog to PWM output
without mapping analog to PWM output


You can see its brightness increase up to 255 value then it is off for 256 value and again increase brightness repeatedly until potentiometer knob reaches at its end position. Analogwrite will take writevalue and make LED fade on or off. If we forget to take .0 after our formula (255/1023), LED will not On because it is giving zero value for any position of knob we can see on serial monitor. (int writevalue = 255.0/1023.0 * potentioValue) instead of this formula we can use map function, map(potentioValue, 0, 1023, 0, 255), parameter of map function are map(value, fromLow, fromHigh, toLow, toHigh) “value” which we want to map and we know low value for analog function is 0 and high is 1023 and we want to map it for PWM output. So Low value for PWM output is 0 and high value is 255. You can see when I rotate knob of potentiometer it control  brightness of LED. If you rotate knob in direction of 5v it will glow LED and opposite will make it fade off.



LIST OF COMPONENT BUY ONLINE:   (Arduino) (Potentiometer)  (Breadboard)   (Jumper wire)  (Resistor)   (LED)