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.
// 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
}
Pin Setup
int potPin = A0;
int ledPin = D2;
Reading Potentiometer
potValue = analogRead(potPin);
Mapping Values to PWM
brightness = map(potValue, 0, 1023, 0, 255);
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.
Yes, both work the same way. A normal potentiometer is easier to adjust, while a trimmer is used for fine calibration.
ESP8266 has a 10-bit ADC, meaning it reads values from 0–1023 corresponding to 0–3.3V input.
PWM is supported on most GPIOs except D0 (GPIO16). Commonly used are D1, D2, D5, D6, D7.
Yes, you can connect multiple LEDs to different pins and apply the same mapped PWM value.
Yes, you can send the potentiometer value to Blynk, ThingSpeak, or MQTT and control LED brightness remotely.