How to wire a potentiometer to an ESP32
To wire a potentiometer to an ESP32, connect 1 to 3V3, 2 to GPIO 34 (D34) and 3 to GND. Run the pot from 3.3 V, never from the 5 V pin, so the wiper can never put more than 3.3 V on an analog input. The sketch below uses no library beyond what the board includes. It reads the wiper and prints the raw reading and a percentage.
- 3.3 V logic
- Library: none
- 3 wires
Potentiometer to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Potentiometer pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| Pin 1 (one end) | 3V3 | red | 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) | amber | The middle leg: a voltage between the two ends, set by the shaft. |
| Pin 3 (other end) | GND | blue | The bottom of the range. |
Power and logic levels for potentiometer and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The potentiometer takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.
This pair
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, and at the default attenuation it saturates at about 3.1 V, so a pot across the 3.3 V rail reaches 4095 a little before the end of its travel and reads about 1890 in the middle.
Pins to leave alone
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.
The working code for the ESP32
Library: none, the sketch reads the wiper with analogRead(). This is the sketch the circuit above runs.
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);
}
What you should see
Open the serial monitor at 115200 baud and turn the knob: each line shows the raw reading and a percentage that follow it. The ESP32’s ADC is twelve bits, 0 to 4095, and at the default attenuation it saturates at about 3.1 V, so a pot across the 3.3 V rail reaches 4095 a little before the end of its travel and reads about 1890 in the middle.
A reading that jumps around or stays at zero means the wiper is not on the analog pin, or the outer legs are not on power and ground.
The serial monitor prints this, recorded from the circuit above with the knob at its middle:
Raw: 1888 Percent: 46Common mistakes wiring a potentiometer to an ESP32
Expecting 2048 in the middle
At the default attenuation the ESP32’s reading is not a straight line: it reaches 4095 at about 3.1 V, before the knob is all the way around, and the middle of a 3.3 V pot reads about 1890. Use analogReadMilliVolts() when you want volts.
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.
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.