An analog joystick typically has:
2 potentiometers (one for X-axis, one for Y-axis)
1 push button (built-in, press down on the joystick)
X-axis → Analog voltage (0V to Vcc) depending on horizontal tilt.
Y-axis → Analog voltage depending on vertical tilt.
SW (Switch) → Digital LOW when pressed, HIGH when idle (pull-up resistor inside).
ESP32 (DevKit v1 or any compatible board)
Analog Joystick Module (usually with 5 pins)
Jumper wires
Breadboard (optional)
USB cable to program ESP32
| Joystick Pin | Function | Connect to ESP32 |
|---|---|---|
| GND | Ground | GND |
| +5V / VCC | Power Supply | 3.3V or 5V |
| VRx | X-axis analog | GPIO 34 (or any ADC pin) |
| VRy | Y-axis analog | GPIO 35 (or any ADC pin) |
| SW | Button | GPIO 25 (or any digital input) |
β οΈ Use ADC-only pins for analog (e.g., GPIO34–GPIO39). These are input-only.
π― What Do the Values Mean?
Analog range: 0 to 4095 (ESP32 is 12-bit ADC)
Center position: ~2048
Tilt Left/Down: Closer to 0
Tilt Right/Up: Closer to 4095
Button: Reads LOW (0) when pressed, HIGH (1) when idle
π οΈ Applications:
Controlling robot car direction/speed
Manual control of servo motors
Gamepad for ESP32 projects
Navigating on OLED displays
Triggering actions (via push button)
π Bonus: Using
map()FunctionYou can convert X/Y readings into -100 to +100 range like this:
Useful for:
Speed control
Servo angles
UI navigation
HOW TO OPERATE
const int xPin = 12; // VRxconst int yPin = 13; // VRyconst int buttonPin = 14; // SW (built-in joystick button)
void setup() {Serial.begin(115200);pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor for button}
void loop() {int xValue = analogRead(xPin);int yValue = analogRead(yPin);bool buttonPressed = digitalRead(buttonPin) == LOW; // Pressed = LOW
// Convert analog values to voltage (ESP32 has 12-bit ADC = 4095 max)float xVolt = (xValue * 3.3) / 4095.0;float yVolt = (yValue * 3.3) / 4095.0;
// Print joystick valuesSerial.print("X: ");Serial.print(xVolt, 2);Serial.print(" V\tY: ");Serial.print(yVolt, 2);Serial.print(" V\tButton: ");Serial.println(buttonPressed ? "Pressed" : "Released");
delay(200);}