Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
2.4 Analog Input¶
This lesson explores the use of a potentiometer as an analog input device to adjust the brightness of an LED. By simply turning the knob of the potentiometer, you can vary the light intensity of the LED, similar to the way you might adjust the brightness of a desk lamp. This straightforward setup demonstrates the direct impact of analog input on real-world applications, offering an intuitive understanding of how changes in input can control electronic components.
Available Pins
Available Pins
Here is a list of available pins on the ESP32 board for this project.
Available Pins
IO14, IO25, I35, I34, I39, I36
Strapping Pins
The following pins are strapping pins, which affect the startup process of the ESP32 during power on or reset. However, once the ESP32 is booted up successfully, they can be used as regular pins.
Strapping Pins
IO0, IO12
Required Components
In this project, we need the following components.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Breadboard |
|
Several Jump Wires |
|
Resistor |
|
LED |
|
Potentiometer |
Schematic

When you rotate the potentiometer, the value of I35 will change. By programming, you can use the value of I35 to control the brightness of the LED. Therefore, as you rotate the potentiometer, the brightness of the LED will also change accordingly.
Wiring

Code
Download this code or copy this code to the Arduino IDE directly.
After the code is uploaded successfully, rotate the potentiometer and you will see the brightness of the LED change accordingly. At the same time you can see the analog and voltage values of the potentiometer in the serial monitor.
How it works?
Define constants for pin connections and PWM settings.
const int potPin = 35; // Potentiometer connected to GPIO35 const int ledPin = 26; // LED connected to GPIO26 // PWM settings const int freq = 5000; // PWM frequency const int resolution = 12; // PWM resolution (bits)
Here the PWM resolution is set to 12 bits and the range is 0-4095.
Configure the system in the
setup()
function.void setup() { Serial.begin(115200); // Configure PWM ledcAttach(ledPin, freq, resolution); }
In the
setup()
function, the Serial communication is started at a baud rate of 115200.The
ledcAttach()
function is used to setup the LEDC pin frequency and resolution. It will returnfrequency
configured for LEDC pin.
Main loop (executed repeatedly) in the loop() function.
void loop() { int potValue = analogRead(potPin); // read the value of the potentiometer uint32_t voltage_mV = analogReadMilliVolts(potPin); // Read the voltage in millivolts ledcWrite(ledPin, potValue); Serial.print("Potentiometer Value: "); Serial.print(potValue); Serial.print(", Voltage: "); Serial.print(voltage_mV / 1000.0); // Convert millivolts to volts Serial.println(" V"); delay(100); }
uint32_t analogReadMilliVolts(uint8_t pin);
: This function is used to get ADC value for a given pin/ADC channel in millivolts.pin
GPIO pin to read analog value.
The potentiometer value is directly used as the PWM duty cycle for controlling the LED brightness via the
ledcWrite()
function, as the range of values is also from 0 to 4095.