Control in Your Hands: ESP32 with 2-Axis Joystick
🔧 What is an Analog Joystick?
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)
📤 Joystick Outputs:
- 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).
🧰 Components Required:
- ESP32 (DevKit v1 or any compatible board)
- Analog Joystick Module (usually with 5 pins)
- Jumper wires
- Breadboard (optional)
- USB cable to program ESP32
⚙️ Pinout of Joystick:
| 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() Function
You can convert X/Y readings into -100 to +100 range like this:
int xMapped = map(xValue, 0, 4095, -100, 100);
int yMapped = map(yValue, 0, 4095, -100, 100);
Useful for:
Speed control
Servo angles
UI navigation
HOW TO OPERATE
const int xPin = 12; // VRx
const int yPin = 13; // VRy
const 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 values
Serial.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);
}

Leave a Reply
You must be logged in to post a comment.