Trimmer Potentiometer Controlling LED Brightness with ESP8266

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
- Potentiometer β ESP8266
- Middle pin (wiper) β A0
- One side pin β 3.3V
- Other side pin β GND
- 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.