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;
  4. void setup()  {
  5.   pinMode(ledPin, OUTPUT);
  6. }
  9. void loop()  {
  11.   potentiomenterInput = (analogRead(0)/4); // Divides input 0-1023 to resemble to 0-255
  13.   analogWrite(ledPin, potentiomenterInput);  
  14.   // The delay can be change to get the desired dimming effect
  15.   delay(20);                            
  16. }