In this article, we are going to make a dimmable LED using a potentiometer. However, first, we are going to create the blinking LED as a starting point and also read values from a potentiometer.

Blinking the Onboard LED

If you are using the Arduino UNO R3, there is a built-in LED at digital pin 13. You can turn it on by making pin 13 HIGH. We can use this to create our "hello world" program in Arduino. This will not require any connections; you just have to make sure the board is on and then upload your program. See the program below. All the program does is turn pin 13 HIGH at intervals.


void setup()
{
  pinMode(13 , OUTPUT);
}

void loop()
{
  digitalWrite(13, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(13, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

Blinking an External LED

A light-emitting diode, just like any other diode, has a cathode and an anode. The long leg of the LED is the cathode while the short leg is the anode. We will also require a current-limiting resistor to prevent the LED from burning out. The resistor should typically be a 220 Ohm or 330 Ohm resistor. This time, we will connect our LED to digital pin 9. Therefore, we will connect digital pin 9 to the long leg of the LED through the current-limiting resistor. The other leg is connected to ground. The current-limiting resistor can also be on the grounded side. The program is similar to the one used to blink the onboard LED, the only difference being that our LED is now connected to digital pin 9. Therefore, we are going to turn digital pin 9 HIGH, delay the program for a specified amount of time, and then turn it LOW.


// led is connected to digital pin 9
int ledPin = 9;

void setup() {

  // The led is an output
  pinMode(ledPin, OUTPUT);
  
}

void loop() {

  digitalWrite(ledPin, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(ledPin, LOW);
  delay(1000); // Wait for 1000 millisecond(s)
}

Reading Values from a Potentiometer

To understand the working of a potentiometer, think of 2 resistors connected in series. The current in the two resistors is constant. However, the voltage across the first or the second resistor can be varied by changing the values of the two resistors. By varying the resistor values, we can vary the output voltage (which in this case is the voltage drop across one of the resistors) from 0V to the input voltage's maximum voltage.

When a voltage is applied across a potentiometer, it can produce a fraction of the applied voltage. With an input voltage of 5V from the Arduino, we can vary the voltage from 0V to 5V. For the potentiometer connection, we need to read the analogue voltage signal from the middle terminal of the potentiometer, also known as the wiper pin. The two pins at the end need to be connected to 5V and the other to ground.

To test the potentiometer's working, we can create a simple circuit to read the output voltage from a potentiometer. For the most common types of potentiometers, the middle pin or the isolated pin is the output pin, while the other two can be used as the positive and negative pins interchangeably. Connect one end of the potentiometer to the 5V pin from the Arduino and the other to ground. For the middle pin, connect it to any of the analog pins as we are going to read an analogue voltage signal. This type of signal is represented by 0 for the lowest value and 1023 for the largest. If you read a value of 5V, it will be translated to 1023, and a value of 0V will be translated to 0. 

Creating a Dimmable LED using a Potentiometer

Next, we are going to create a dimmable LED using a potentiometer. This is a simple project in which we are going to control the LED brightness by decreasing the power into the LED by using a potentiometer. By gradually decreasing the voltage across the potentiometer, we can control the power into the LED and hence the brightness. Simply put, when the voltage is at its maximum, we provide maximum power to our LED (VI) since the current is constant. When the voltage is 0V, no power is being fed into our LED, and it will be in the off state (0*I). We've already seen how to connect an LED and how to read values from a potentiometer. All that's left is to combine what we've done so far by reading the voltage value from the potentiometer and writing that to the LED. What's different is that we are not just merely turning the LED on and off but we are giving it a certain voltage based on the potentiometer output. Therefore, we will be writing an analogue signal to the LED and not a digital one. The LED will, therefore, need to be connected to a PWM-enabled Arduino pin. See the circuit below.

Dimmable led


//Led is connected at digital pin 9
int ledPin = 9;

//delay time for the program to allow time for adjusting and reading from potentiometer
int delay_time = 100;
//declare the value red from the analogRead and the converted value to be written to the led
int readVal;
float realVal;

// The analogRead pin is connected at A0
int readPin = A0;
void setup() {

  // ledPin is an output pin while readPin is an input pin
  pinMode(ledPin, OUTPUT);
  pinMode(readPin,INPUT);
  Serial.begin(9600);
}

void loop() {

  //Read the value from the potentiometer
  readVal = analogRead(readPin);
  
  //Write the read value to the led
  analogWrite(ledPin, readVal*(255./1023.));
  delay(delay_time);

}