ESP32 Potentiometer Simulator: Wiring, Code and a Live Circuit
This is a 10 k potentiometer wired to an ESP32 DevKit, read live: the serial monitor prints the raw reading and a percent five times a second. Open it in the editor and turn the knob to see both change.
The ends go to 3V3 and GND and the wiper to GPIO 34 (D34). The ESP32’s twelve-bit ADC has a quirk at the top of its range, explained below.
- 3.3 V logic
- No library: analogRead()
- Wiper on GPIO 34 (D34)
- 0 to 4095
Wiring the Potentiometer to the ESP32
Every connection in the circuit above, with the board’s own pin names.
| Potentiometer pin | ESP32 pin | Why |
|---|---|---|
| Pin 1 (one end) | 3V3 | The top of the ADC’s range is 3.3 V, so the pot spans 0 to 3.3 V. |
| Pin 2 (wiper) | GPIO 34 (D34) | The middle leg: a voltage between the two ends, set by the shaft. |
| Pin 3 (other end) | GND | The bottom of the range. |
Run the pot from 3V3, never from VIN (5 V from USB): turned to the top, the wiper would put 5 V on an analog input rated for 3.3 V. The ESP32’s ADC is twelve bits, 0 to 4095, but its full scale at the default attenuation is about 3.9 V, so a pot across the 3.3 V rail tops out near 3466.
Pins to leave alone on the ESP32
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
On a real bench
The thirty-pin DevKit straddles a breadboard with one row of holes free on each side, which is where the Potentiometer’s jumpers go. Many ESP32 boards are wider and cover both rows; then use two breadboards side by side. Hold BOOT while uploading if the IDE cannot connect.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int POT_PIN = 34; // the wiper, GPIO 34 (D34)
void setup() {
Serial.begin(115200);
}
void loop() {
int raw = analogRead(POT_PIN); // 0 to 4095
int percent = map(raw, 0, 4095, 0, 100);
Serial.print("Raw: ");
Serial.print(raw);
Serial.print(" Percent: ");
Serial.println(percent);
delay(200);
}
analogRead(POT_PIN) converts the wiper’s voltage to a number. The ESP32’s ADC is twelve bits, 0 to 4095, but its full scale at the default attenuation is about 3.9 V, so a pot across the 3.3 V rail tops out near 3466.
map(raw, 0, 4095, 0, 100) rescales it to a percent with whole-number arithmetic. For a PWM duty use map(raw, 0, 4095, 0, 255), or divide.
Serial.begin(115200) opens the serial monitor. Serial is UART0 on the USB bridge. ESP32 sketches usually run it at 115200 baud, which this one does.
Common mistakes with the Potentiometer on the ESP32
Expecting 4095 at the top
At the default attenuation the ESP32’s full scale is about 3.9 V, so the 3.3 V rail reads about 3466, not 4095. Map from the highest value you actually see, or use analogReadMilliVolts().
Wiring the wiper to an end
The middle leg is the wiper. With an end leg on the analog pin the reading never moves, or jumps from 0 to the top at one end of the travel.
Running the pot from VIN (5 V from USB)
At the top of the travel the pin would see 5 V, above the ESP32 (Xtensa LX6)’s rating. The pot belongs on 3V3.
Questions and answers
Can I simulate a potentiometer with an ESP32 DevKit?
Yes. The circuit at the top of this page is an ESP32 DevKit with a potentiometer, running in your browser on a simulated ESP32 (Xtensa LX6). Drag the knob and the analog pin sees the divided voltage, converted the way the board’s own ADC converts it. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
What range does analogRead() give on the ESP32?
The ESP32’s ADC is twelve bits, 0 to 4095, but its full scale at the default attenuation is about 3.9 V, so a pot across the 3.3 V rail tops out near 3466.
Which way should the pot turn?
Swap the two end wires to reverse it. The wiper stays in the middle either way.
How do I dim an LED with the pot?
Read the pot, then analogWrite(ledPin, map(raw, 0, 4095, 0, 255)) on any output pin.
Build your own Potentiometer project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.