Trimmer Potentiometer Controlling LED Brightness with ESP8266

adminESP82665 days ago7 Views

Introduction

A trimmer potentiometer is a small adjustable resistor used to vary voltage levels in a circuit. The ESP8266 has an ADC pin (A0) that can read the voltage from the potentiometer and convert it into values between 0 and 1023.

On the other hand, the ESP8266 supports PWM (Pulse Width Modulation) on most GPIO pins, which allows us to control the brightness of an LED. By reading the potentiometer value and mapping it to a PWM signal, we can easily adjust LED brightness.

This project demonstrates the fundamentals of analog-to-digital conversion and PWM control—a must-know for IoT and embedded systems learners.

Components Required

  • ESP8266 (NodeMCU or Wemos D1 Mini)
  • Trimmer Potentiometer (10kΩ)
  • LED
  • 220Ω Resistor
  • Breadboard & Jumper Wires
  • USB Cable

Circuit Diagram

  1. Potentiometer → ESP8266
    • Middle pin (wiper) → A0
    • One side pin → 3.3V
    • Other side pin → GND
  2. LED → ESP8266
    • LED Anode (+) → D2 (GPIO4) through 220Ω resistor
    • LED Cathode (–) → GND

Arduino Code (Potentiometer → LED Brightness)

// Potentiometer controlling LED brightness with ESP8266

int potPin = A0;      // Potentiometer wiper connected to A0
int ledPin = D2;      // LED connected to GPIO4 (D2)
int potValue = 0;     // To store potentiometer value
int brightness = 0;   // To store mapped brightness

void setup() {
  Serial.begin(115200);     // Start Serial Monitor
  pinMode(ledPin, OUTPUT);  // Set LED pin as output
}

void loop() {
  // Read potentiometer value (0 - 1023)
  potValue = analogRead(potPin);

  // Map value (0 - 1023) to PWM range (0 - 255)
  brightness = map(potValue, 0, 1023, 0, 255);

  // Write PWM signal to LED
  analogWrite(ledPin, brightness);

  // Print values to Serial Monitor
  Serial.print("Pot Value: ");
  Serial.print(potValue);
  Serial.print(" | LED Brightness: ");
  Serial.println(brightness);

  delay(100); // Small delay for stability
}

Step-by-Step Code Explanation

Pin Setup

int potPin = A0;  
int ledPin = D2;

Reading Potentiometer

potValue = analogRead(potPin);

Mapping Values to PWM

brightness = map(potValue, 0, 1023, 0, 255);
  • ESP8266 PWM accepts 0–255 as brightness values.
  • map() converts potentiometer range into PWM range.

Controlling LED Brightness

analogWrite(ledPin, brightness);

Adjusts LED brightness according to potentiometer position.

Serial Output for Debugging

Serial.print("Pot Value: ");
Serial.println(brightness);

Displays potentiometer readings and corresponding LED brightness.

Output

  • Turn the potentiometer knob → LED brightness will increase or decrease smoothly.
  • Fully left → LED OFF.
  • Fully right → LED at maximum brightness.

Real-Life Applications

  • Light Dimmers: Control room light brightness manually.
  • Fan Speed Controllers: Adjust DC fan speed in automation projects.
  • Calibration Tools: Tune sensitivity of sensors in IoT systems.
  • Volume Knobs: Used in audio amplifiers for manual sound control.
  • IoT User Inputs: Provide analog input to IoT dashboards.

Troubleshooting

  • LED not lighting up:
    • Check LED polarity (Anode to resistor → D2, Cathode → GND).
    • Verify resistor (220Ω recommended).
  • No change in brightness:
    • Ensure potentiometer middle pin is connected to A0.
    • Confirm ESP8266 is powered properly (3.3V reference).
  • Values fluctuating a lot:
    • Add a 0.1µF capacitor between potentiometer output and GND to stabilize readings.
  • ESP8266 not supporting PWM on chosen pin:
    • Try using other pins like D1, D2, D5, D6, D7 for PWM.

    Can I use a normal potentiometer instead of a trimmer?

    Yes, both work the same way. A normal potentiometer is easier to adjust, while a trimmer is used for fine calibration.

    What is the ADC range of ESP8266?

    ESP8266 has a 10-bit ADC, meaning it reads values from 0–1023 corresponding to 0–3.3V input.

    Which GPIO pins of ESP8266 support PWM?

    PWM is supported on most GPIOs except D0 (GPIO16). Commonly used are D1, D2, D5, D6, D7.

    Can I control multiple LEDs with one potentiometer?

    Yes, you can connect multiple LEDs to different pins and apply the same mapped PWM value.

    Can I extend this project to IoT?

    Yes, you can send the potentiometer value to Blynk, ThingSpeak, or MQTT and control LED brightness remotely.

    Loading Next Post...
    Follow
    Search Trending
    Popular Now
    Loading

    Signing-in 3 seconds...

    Signing-up 3 seconds...