Using a potentiometer to control LED

You can easly control the current running through your LED by adding a potentiometer as part of your circuit.

To control the LED with Arduino programming you attach the potentiometer to your analog in and let your Arduino program decide how much to dim the LED depending on the input you get from the potentiometer.

The input from analogRead returns a value between 0 and 1023. The analogWrite takes values between 0 and 255. The code below show you have to convert your analog in value to make your LED shine as bright as possible when the potentiometer is fully on.

  1. int ledPin = 3;
  2. int potentiomenterInput = 0;
  3.  
  4. void setup()  {
  5.   pinMode(ledPin, OUTPUT);
  6. }
  7.  
  8.  
  9. void loop()  {
  10.  
  11.   potentiomenterInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
  12.  
  13.   analogWrite(ledPin, potentiomenterInput);  
  14.   // The delay can be change to get the desired dimming effect
  15.   delay(20);                            
  16. }

4 thoughts on “Using a potentiometer to control LED

      • A LED needs a certain voltage to work.
        The intensity of a LED is to be controlled by adjusting its current, not its voltage, yet in your first sketch you are controlling voltage.
        The 220 Ohm resistor will control current, the potentiometer just controls voltage.
        You will see that your first sketch will work very poorly with al large dead zone in your potentiometer.
        The second one however will work beautifull with a nice linear intensity of that LED.
        (Our eyes are better served with a logaritmic intensity, but never mind that).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>