How to wire a joystick to an ESP32
To wire a joystick to an ESP32, connect VCC to 3V3, GND to GND, VRX to GPIO 34 (D34), VRY to GPIO 35 (D35) and SW to GPIO 4 (D4). Power the module from 3.3 V: pushed to the end of its travel, a wiper would otherwise put 5 V on an analog pin rated for 3.3 V. The sketch below uses no library beyond what the board includes. It reads both axes and prints X, Y and the direction the stick is pushed.
- 3.3 V logic
- Library: none
- 5 wires
Joystick 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.
| Joystick pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | 3V3 | red | The stick’s two tracks span 0 to 3.3 V. |
| GND | GND | blue | The other end of both tracks. |
| VRX | GPIO 34 (D34) | amber | The X wiper, an analog input. |
| VRY | GPIO 35 (D35) | green | The Y wiper, an analog input. |
| SW | GPIO 4 (D4) | violet | Shorts to GND when the stick is pressed; the sketch turns on the internal pull-up. |
Power and logic levels for joystick 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 joystick takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.
This pair
Power the module from 3V3, never from VIN (5 V from USB): pushed to the end of its travel, a wiper would put 5 V on an analog pin rated for 3.3 V. Both axis pins here are input-only pins with an ADC. 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 both axes with analogRead(). This is the sketch the circuit above runs.
const int X_PIN = 34; // VRX, GPIO 34 (D34)
const int Y_PIN = 35; // VRY, GPIO 35 (D35)
const int SW_PIN = 4; // the button, GPIO 4 (D4)
void setup() {
Serial.begin(115200);
pinMode(SW_PIN, INPUT_PULLUP); // LOW while the stick is pressed
}
void loop() {
int xr = analogRead(X_PIN); // 0 to 4095
int yr = analogRead(Y_PIN);
bool pressed = digitalRead(SW_PIN) == LOW;
const char* dir = "Center";
if (xr < 4095 / 4) dir = "Left";
else if (xr > 4095 / 4 * 3) dir = "Right";
else if (yr < 4095 / 4) dir = "Up";
else if (yr > 4095 / 4 * 3) dir = "Down";
Serial.print("X: ");
Serial.print(xr);
Serial.print(" Y: ");
Serial.print(yr);
Serial.print(" ");
Serial.print(dir);
Serial.println(pressed ? " pressed" : "");
delay(250);
}
What you should see
Open the serial monitor at 115200 baud. Each line shows the X and Y readings and a word for the direction: Center when the stick rests, Right, Left, Up or Down when it is pushed.
The push button under the stick is a separate switch on its own pin; it reads low when pressed.
The serial monitor prints this, recorded from the circuit above with the stick pushed right and then released:
X: 1892 Y: 1892 Center
X: 4095 Y: 1892 Right
X: 1892 Y: 1892 CenterCommon mistakes wiring a joystick to an ESP32
No pull-up on the button
Without INPUT_PULLUP the SW pin floats and reports presses nobody made. Most modules have no resistor of their own.
Expecting an exact center
The rest position is a few counts either side of the middle and differs per axis. Use a dead zone, or read the rest position at startup and measure from it.
An axis that runs backwards
Modules are mounted in different directions. Swap the two pins in the code, or flip a reading with 4095 - reading.
Build your own joystick project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.