Overview
Learn how to read X-axis, Y-axis, and button values from a KY-023 analog joystick using an ESP32. This project is useful for robot control, menu navigation, and simple game controllers.
Instructions
Step by step
1. Connect the joystick VCC pin to 3.3V.
2. Connect the joystick GND pin to GND.
3. Connect the VRX pin to D34.
4. Connect the VRY pin to D35.
5. Connect the SW pin to D15.
6. Run the code and open the Serial Monitor.
7. Move the joystick sliders in the simulator to change X and Y values.
8. Click the joystick button to change the SW value.
Code
Source code
const int vrxPin = 34;
const int vryPin = 35;
const int swPin = 15;
void setup() {
Serial.begin(115200);
pinMode(swPin, INPUT_PULLUP);
Serial.println("KY-023 Joystick Test");
}
void loop() {
int xValue = analogRead(vrxPin);
int yValue = analogRead(vryPin);
int buttonValue = digitalRead(swPin);
Serial.print("X: ");
Serial.print(xValue);
Serial.print(" Y: ");
Serial.print(yValue);
Serial.print(" Button: ");
Serial.println(buttonValue);
delay(500);
}
Related