Circuit Creation

Potentiometer Control Project using Arduino

Learn how to read analog values from a potentiometer and use them to control other components like LEDs, servos, or displays. This project introduces you to analog input and variable control in Arduino programming.

Beginner

Components Required

Circuit Connections

Follow the steps below to connect the potentiometer and LED with the Arduino board. Make sure the Arduino is not connected to USB while wiring.

  1. Insert the potentiometer into the breadboard. It has 3 legs: left, middle (wiper), and right.
  2. Connect the left leg of the potentiometer to 5V on Arduino.
  3. Connect the right leg of the potentiometer to GND on Arduino.
  4. Connect the middle leg (wiper) to analog pin A0.
  5. Insert the LED into the breadboard.
  6. Connect a 220Ω resistor to the shorter leg (cathode) of the LED.
  7. Connect the other end of the resistor to GND on Arduino.
  8. Connect the longer leg of the LED to digital pin 9 (PWM pin).
Potentiometer LED Circuit Diagram

Here's a simple circuit diagram to visualize the connections. The potentiometer acts as a variable resistor, allowing you to adjust the voltage read by the Arduino on pin A0. The LED brightness will change based on the potentiometer's position, demonstrating how analog input can control an output device.

Arduino Code

                
/*
   Project: Potentiometer Control with Arduino
   description: Learn how to read potentiometer values and control LED brightness using Arduino. 

   Components:
     - Arduino Uno 
     - 10kΩ Potentiometer 
     - LED 
     - 220Ω Resistor 
     - Breadboard 
     - Jumper Wires
     - USB Cable 
   Author: Circuit Creation RN

*/
                    
// This code reads the potentiometer value and controls the brightness of an LED accordingly.

int potPin = A0;      // Analog pin for potentiometer
int ledPin = 9;       // PWM pin for LED

void setup() {
  Serial.begin(9600);     // Initialize serial communication
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Read the potentiometer value (0-1023)
  int potValue = analogRead(potPin);
  
  // Convert analog value (0-1023) to PWM value (0-255)
  int ledBrightness = map(potValue, 0, 1023, 0, 255);
  
  // Set LED brightness
  analogWrite(ledPin, ledBrightness);
  
  // Print values to Serial Monitor
  Serial.print("Potentiometer: ");
  Serial.print(potValue);
  Serial.print(" -> LED Brightness: ");
  Serial.println(ledBrightness);
  
  delay(100);  // Update every 100ms
}
                
            

Upload the Code

  1. Connect the Arduino board to your computer using the USB cable.
  2. Open the Arduino IDE and paste the code into a new sketch.
  3. Select the correct board and port from the Tools menu.
  4. Click the Upload button.
  5. Open the Serial Monitor (Tools > Serial Monitor) to see the potentiometer values.

Code Explanation

Let us understand how the Arduino code works step by step. This will help you modify the project later and build more projects.

Declaring Pins

int potPin = A0; stores the analog pin where the potentiometer is connected. int ledPin = 9; stores the PWM pin for the LED.

setup() Function

The setup() function runs only once when the Arduino starts. Serial.begin(9600) initializes serial communication for the Serial Monitor. pinMode(ledPin, OUTPUT) tells Arduino that pin 9 is an output.

analogRead() Function

analogRead(potPin) reads the voltage on the analog pin. It returns a value between 0 and 1023, where 0 is 0V and 1023 is 5V.

map() Function

The map() function converts values from one range to another. Here, it converts the potentiometer value (0-1023) to PWM value (0-255). This is necessary because PWM brightness only accepts 0-255 values.

analogWrite() Function

analogWrite(ledPin, ledBrightness) sets the LED brightness using PWM. PWM values range from 0 (off) to 255 (full brightness).

loop() Function

The loop() function runs continuously. It reads the potentiometer, converts the value, and updates the LED brightness.

Common Mistakes & Troubleshooting

What You Learned

Next Steps

Now that you understand potentiometer control, try these advanced projects: