ArduFix

LED dimming using a potentiometer

LED dimming with potentiometer

To control LED dimming using a potentiometer with Arduino, you will need the following components:

  1. Arduino board (e.g., Arduino Uno)
  2. LED (Light-Emitting Diode)
  3. Resistor (appropriate value for the LED)
  4. Potentiometer (e.g., 10k ohm)
  5. Breadboard
  6. Jumper wires

Here are the step-by-step instructions to control LED dimming using a potentiometer with Arduino:

  1. Connect one leg of the potentiometer (usually the left or right leg) to the 5V pin on the Arduino board.
  2. Connect the other leg of the potentiometer (usually the right leg) to the ground (GND) pin on the Arduino board.
  3. Connect the middle leg of the potentiometer (the wiper) to an analog input pin on the Arduino. For example, connect it to analog pin A0.
  4. Connect the positive (longer) leg of the LED to a digital pin of Arduino with PWM capabilities, For example, connect it to digital pin 11.
  5. Connect the negative (shorter) leg of the LED to a current limiting resistor. For example, connect it to a 220 Ohm resistor.
  6. Connect the other end of the resistor  to the ground (GND) pin on the Arduino.

Now let’s tackle the programming part:

  1. Open the Arduino IDE on your computer.
  2. Write the following code:
 
const int potPin = A0;    // Analog input pin connected to the potentiometer
const int ledPin = 11;     // Digital output pin connected to the LED

void setup() {
  pinMode(ledPin, OUTPUT);   // Set the LED pin as output
}

void loop() {
  int brightness = analogRead(potPin);    // Read the potentiometer value (0-1023)
  brightness = map(brightness, 0, 1023, 0, 255);   // Map the potentiometer value to LED brightness range (0-255)
  analogWrite(ledPin, brightness);   // Set the LED brightness using PWM
}
 
  1. In the Arduino IDE, select the correct board type and port from the “Tools” menu.
  2. Click on the “Upload” button to upload the code to the Arduino board.
Now, when you rotate the potentiometer, the LED brightness will change accordingly. The analogRead function reads the value from the potentiometer (0-1023), which represents the position of the potentiometer’s wiper. The map function scales this value to the LED brightness range (0-255) suitable for analogWrite. Finally, the analogWrite function sets the LED brightness using Pulse Width Modulation (PWM). As you rotate the potentiometer, the LED will dim or brighten based on the position of the potentiometer’s wiper.
Scroll to Top