LED Dimming with Potentiometers

Learn how to combine potentiometers with LEDs to create smooth dimming effects.

ARDUINO LESSONSElectronicsLED ProjectsPWM
S
Shaun Sosi
How to Dim an LED using the Potentiometer

Combining Analog Input with PWM Output

In this project, we'll combine what we've learned about potentiometers and LED dimming to create a smooth, adjustable LED brightness control system.

LED Dimming with Potentiometer Setup
Creating an adjustable LED brightness control using a potentiometer

Think of This Project Like:

Imagine creating your own custom light dimmer switch, where turning the potentiometer knob smoothly adjusts the LED brightness from completely off to full brightness.

Get Your Arduino Kit

To build this LED dimming project:

Understanding the Components

Key Concepts:

This project combines analog input (potentiometer) with PWM output (LED) through a value conversion process.

  1. Potentiometer Input

    • Acts as a variable resistor
    • Provides analog values from 0 to 1023
    • Connected to analog pin A0
  2. LED Output

    • Requires PWM pin for dimming (pin 9)
    • Accepts values from 0 to 255
    • Creates smooth brightness transitions

Value Conversion Formula

Understanding the Math:

We need to convert the potentiometer's 10-bit range (0-1023) to the LED's 8-bit range (0-255).

The conversion formula:

ValueConversion.txt
LED Value = (255/1023) × Potentiometer Value This formula is derived from: 1. Two points method: (0,0) and (1023,255) 2. Using the gradient formula: (y2-y1)/(x2-x1) 3. Slope = 255/1023 Example: - Pot at middle (511) → LED = (255/1023) × 511 ≈ 127 - Pot at max (1023) → LED = (255/1023) × 1023 = 255 - Pot at min (0) → LED = (255/1023) × 0 = 0

    Implementation

    Here's the complete code for the LED dimming project:

    PotentiometerLEDDimmer.ino
    // Pin and variable declarations const int potPin = A0; // Potentiometer input pin const int ledPin = 9; // LED output pin (must be PWM) const int wait = 500; // Delay between readings int potValue; // Store potentiometer reading int ledValue; // Store calculated LED brightness void setup() { // Configure pins and serial communication pinMode(potPin, INPUT); // Set potentiometer pin as input pinMode(ledPin, OUTPUT); // Set LED pin as output Serial.begin(9600); // Initialize serial monitor } void loop() { // Read potentiometer value potValue = analogRead(potPin); // Convert to LED brightness (0-255) ledValue = (255.0/1023.0) * potValue; // Display the calculated LED value Serial.println(ledValue); // Update LED brightness analogWrite(ledPin, ledValue); // Small delay for stability delay(wait); }

      Important Considerations:

      • PWM Pin - LED must be connected to a PWM-capable pin (~)
      • Current Limiting - Don't forget the LED current-limiting resistor
      • Smooth Motion - Reduce delay for smoother dimming response
      • Floating Values - Ensure potentiometer is properly connected

      Circuit Setup

      Follow these steps to build the circuit:

      1. Potentiometer Connections

        • Outside pin 1 → 5V
        • Outside pin 2 → GND
        • Middle pin (wiper) → A0
      2. LED Connections

        • Anode (+) → 220Ω resistor → Pin 9
        • Cathode (-) → GND

      Troubleshooting Tips

      Common issues and solutions:

      • LED Not Dimming - Verify PWM pin connection and resistor value
      • Erratic Behavior - Check potentiometer connections and wiring
      • Delayed Response - Reduce the delay value in the code
      • Flickering - Add a small capacitor across the LED

      Pro Tip:

      For even smoother dimming, you can implement a moving average for the potentiometer readings:

      SmoothDimming.ino
      const int numReadings = 5; int readings[numReadings]; int readIndex = 0; int total = 0; // In loop(): total = total - readings[readIndex]; readings[readIndex] = analogRead(potPin); total = total + readings[readIndex]; readIndex = (readIndex + 1) % numReadings; int smoothedValue = total / numReadings; ledValue = (255.0/1023.0) * smoothedValue;

        Project Extensions

        Take this project further with these ideas:

        • Multiple LEDs - Control several LEDs with different brightness levels
        • Color Mixing - Use RGB LEDs for color control
        • Pattern Generator - Create automated dimming patterns
        • Interactive Display - Add an LCD to show brightness levels

        Coming Up Next:

        Stay tuned for more exciting Arduino tutorials where we'll explore more components and create even more interesting projects!

        Connect With Me

        Support Our Work

        Help us create more amazing content

        $

        Your contribution helps us create more amazing content. Thank you! 💖