Potentiometers and joysticks: turn a position into a number
A potentiometer (pot) is a resistor track with a sliding contact, the wiper. Connect the two ends of the track to 3V3 and GND. The wiper then gives a voltage between 0 V and 3.3 V that follows the position of the knob or slider. The ESP32 measures that voltage with its ADC (analog-to-digital converter) and gives a number from 0 to 4095. A thumb joystick module (KY-023 style) contains two 10 kΩ pots, one for X and one for Y, and a push switch under the stick. This page shows the wiring, the limits of the ESP32 ADC, and how to get smooth, stable values.

Photo: Iainf, CC BY 2.5
Drag around the knob (or use the mouse wheel). Drag the joystick cap. Release it and it springs back. Set the dead zone to 0 or clear “Calibrate center” and look at the cursor.
analogRead() with noise
after the filter
ideal: V ÷ 3.3 V × 4095
ADC transfer curve for the selected attenuation (knob = pink dot, dashed = ideal)
How it works
Voltage divider
The pot track and the wiper make a voltage divider. The wiper voltage is V = 3.3 V × position. The total resistance does not change the voltage. Use 5–50 kΩ. A 10 kΩ pot is a good choice: it draws only 0.33 mA and the ADC input can charge quickly. A linear pot (often marked “B”) gives equal voltage steps per degree. A log pot (“A”) is for audio volume.
12-bit ADC
The ESP32 ADC gives 4096 steps (0–4095) by default. analogReadResolution(bits) changes the scale of the result.
Each step is less than 1 mV, but noise is usually 5–30 steps, so the last bits are not useful.
For more about steps and resolution, refer to Quantization.
Attenuation
The ADC itself measures about 0–1 V. An attenuator in front of it extends the range. ADC_11db (the default) gives about
0.1–3.1 V. Use analogSetAttenuation() for all pins or analogSetPinAttenuation(pin, …) for one pin.
With a 3.3 V pot, keep 11 dB. Select 0 dB in the simulator: most of the knob travel then reads 4095.
Nonlinear near the ends
The raw ESP32 ADC is not a straight line. Below about 0.1 V it reads 0. Above about 2.8 V the slope decreases, and it reaches 4095 before 3.3 V.
Look at the transfer curve in the simulator. analogReadMilliVolts() uses the calibration data in the chip and
gives a better voltage value. Do not expect exact readings at the two ends of the knob travel.
Noise and smoothing
The reading jumps by some counts, also when nothing moves. A moving average uses the mean of the last N samples.
An exponential moving average (EMA) uses y = y + α × (x − y). It needs only one variable. Both filters
add delay. You can also take several readings and average them (oversampling).
Use ADC1 pins
ADC2 (GPIO 0, 2, 4, 12–15, 25–27) does not operate while Wi-Fi is on. Use the ADC1 pins GPIO 32–39 (36 = VP and 39 = VN on most DevKits). GPIO 34–39 are input only, which is good for analog inputs. Refer to GPIO.
Joystick center and dead zone
At rest, the stick is near mid-scale. The value is usually a little different from 2048, because each pot has a tolerance and the ADC is not linear.
Measure the center in setup(). Then ignore a small dead zone around it, so the noise does not move your robot or cursor.
Then scale each side separately to −1…+1.
map() and constrain()
map(x, inMin, inMax, outMin, outMax) scales a number with integer math, so it rounds down. It does not limit the result.
Use constrain(x, lo, hi) after it. Example: constrain(map(raw, 100, 4000, 0, 180), 0, 180) for a
servo angle.
5 V modules
Many joystick modules have a pin marked “+5V”. The module only contains pots and a switch, so it operates at any voltage. Connect that pin to 3V3. Do not connect it to 5 V, because the wiper can then go above 3.3 V and damage the ESP32 pin.
Wiring
INPUT_PULLUP on GPIO 27.
The order of the module pins is different on some boards. Read the labels.Connections
| Part pin | Connect to | Notes |
|---|---|---|
| Pot end 1 | 3V3 | Swap the two ends to reverse the direction. |
| Pot wiper (middle) | GPIO 34 | ADC1, input only. |
| Pot end 2 | GND | |
| Joystick +5V | 3V3 | Pots only: 3.3 V is correct. |
| Joystick GND | GND | |
| VRx | GPIO 32 | ADC1. |
| VRy | GPIO 33 | ADC1. |
| SW | GPIO 27 | INPUT_PULLUP. LOW when pressed. Refer to Buttons. |
Build steps
- Connect the potConnect the outer pins to 3V3 and GND. Connect the middle pin to GPIO 34.
- Upload sketch 1Open the Serial Plotter at 115200 baud. Turn the knob from one end to the other.
- Examine the endsWrite down the raw values at the two ends. Use them as the input range of
map(). - Connect the joystickConnect +5V to 3V3. Connect VRx, VRy and SW as shown in the table.
- Upload sketch 2Do not touch the stick during the first second. The sketch measures the center at that time.
- Set the dead zoneIncrease
DEAD_ZONEuntil the output stays at 0.00 with the stick at rest.
Code
1 · Potentiometer with smoothing
pot_read.ino
const int POT_PIN = 34; // ADC1
const float ALPHA = 0.2; // EMA filter: 0.05 = very smooth, 1 = off
float smooth = 0;
int readAveraged(int pin, int n) { // oversampling: mean of n readings
long sum = 0;
for (int i = 0; i < n; i++) sum += analogRead(pin);
return sum / n;
}
void setup() {
Serial.begin(115200);
analogReadResolution(12); // 0..4095 (default)
analogSetPinAttenuation(POT_PIN, ADC_11db); // about 0.1..3.1 V (default)
smooth = readAveraged(POT_PIN, 8);
}
void loop() {
int raw = readAveraged(POT_PIN, 8);
smooth += ALPHA * (raw - smooth); // exponential moving average
uint32_t mv = analogReadMilliVolts(POT_PIN); // calibrated voltage
// The raw ends are not exactly 0 and 4095: use measured limits.
int percent = constrain(map((int)smooth, 40, 4050, 0, 100), 0, 100);
// Serial Plotter format: name:value pairs
Serial.printf("raw:%d smooth:%.0f mV:%lu percent:%d\n", raw, smooth, mv, percent);
delay(20);
}
2 · Joystick: center, dead zone, −1…+1
joystick.ino
const int X_PIN = 32, Y_PIN = 33, SW_PIN = 27;
const int DEAD_ZONE = 150; // counts around the center
int centerX, centerY;
int readAvg(int pin) {
long sum = 0;
for (int i = 0; i < 16; i++) sum += analogRead(pin);
return sum / 16;
}
// Scale each side of the center separately, so both ends reach +-1.
float axis(int raw, int center) {
int d = raw - center;
if (abs(d) <= DEAD_ZONE) return 0.0f;
float out = (d > 0) ? float(d - DEAD_ZONE) / (4095 - center - DEAD_ZONE)
: float(d + DEAD_ZONE) / (center - DEAD_ZONE);
return constrain(out, -1.0f, 1.0f);
}
void setup() {
Serial.begin(115200);
pinMode(SW_PIN, INPUT_PULLUP);
delay(500); // do not touch the stick now
centerX = readAvg(X_PIN);
centerY = readAvg(Y_PIN);
Serial.printf("Center: %d, %d\n", centerX, centerY);
}
void loop() {
float x = axis(readAvg(X_PIN), centerX);
float y = -axis(readAvg(Y_PIN), centerY); // flip the sign if up is negative
bool pressed = digitalRead(SW_PIN) == LOW;
Serial.printf("x:%.2f y:%.2f sw:%d\n", x, y, pressed);
delay(20);
}
Use x and y to set the speed of two DC motors or the angle of
servos. For a wireless gamepad, refer to Bluepad32.
3 · Moving average with a ring buffer
moving_average.ino
const int POT_PIN = 34;
const int N = 16; // window size
int buf[N];
int idx = 0;
long sum = 0;
void setup() {
Serial.begin(115200);
int first = analogRead(POT_PIN);
for (int i = 0; i < N; i++) buf[i] = first;
sum = (long)first * N;
}
void loop() {
int raw = analogRead(POT_PIN);
sum += raw - buf[idx]; // remove the oldest sample, add the newest
buf[idx] = raw;
idx = (idx + 1) % N;
Serial.printf("raw:%d avg:%ld\n", raw, sum / N);
delay(10);
}
Troubleshooting
Always 0 or always 4095
Make sure that the wiper (middle pin) goes to the GPIO. If you connected an end pin, the value does not change. Examine the attenuation. Make sure that the pin is an ADC1 pin and that Wi-Fi does not block ADC2.
The value jumps around
Add a filter or average several readings. Add a 100 nF capacitor from the wiper to GND. Keep the wires short. Wi-Fi transmissions and a noisy USB supply increase the noise.
The joystick drifts at rest
Calibrate the center in setup(). Increase the dead zone. Make sure that the module gets 3.3 V. Do not use 5 V.
A worn joystick can have a different rest position after each movement.
Resources
- Arduino-ESP32: ADC API
analogRead,analogReadMilliVolts, attenuation and resolution. - ESP-IDF: ADC oneshot driverInput ranges per attenuation, ADC1 and ADC2 limits.
- Random Nerd Tutorials: ESP32 ADCPotentiometer example and a plot of the ADC nonlinearity.
- QuantizationSteps, resolution and quantization error.
- PhotoresistorAnother voltage divider on an ADC pin.
- Rotary encodersA knob that turns without an end stop and gives digital pulses.
- ButtonsRead the joystick push switch with debounce.