Joystick Control
Using Arduino Uno
Interface a joystick module with Arduino in just 5 wires. Read X-axis, Y-axis, and push-button values live — perfect for robots, RC cars, and game controllers.
Joystick Pin Connections (5 Wires)
Circuit Diagram
Step-by-Step Wiring
X-Axis Output
Reads horizontal joystick movement as analog voltage
Y-Axis Output
Reads vertical joystick movement as analog voltage
Push Button
Active LOW — goes to GND when joystick is pressed down. Requires INPUT_PULLUP in code.
Power Supply
Powers both internal potentiometers inside the module
Ground
Common ground for stable voltage reference
pinMode(swPin, INPUT_PULLUP) in your setup. This activates Arduino's internal pull-up resistor so the button reads HIGH normally and LOW when pressed. Without it, the pin floats and gives random values.
How the Joystick Works
Inside the module are two potentiometers (one per axis). Moving the stick changes resistance → changes voltage → Arduino reads it as 0–1023.
Joystick Direction → Value Map
Arduino Code
// ── Joystick Control | MakeMindz.com ────────────── // Reads X-axis, Y-axis, and push button from KY-023 // joystick module. Outputs live data via Serial Monitor. // ── Pin Definitions ────────────────────────────── const int xPin = A0; // VRX → Horizontal (X) axis const int yPin = A1; // VRY → Vertical (Y) axis const int swPin = 2; // SW → Push button (active LOW) // ── Setup ──────────────────────────────────────── void setup() { Serial.begin(9600); pinMode(swPin, INPUT_PULLUP); // Internal pull-up → HIGH by default Serial.println("Joystick Ready — Move to see values!"); } // ── Main Loop ──────────────────────────────────── void loop() { int xValue = analogRead(xPin); // 0 (left) → 1023 (right) int yValue = analogRead(yPin); // 0 (down) → 1023 (up) int buttonState = digitalRead(swPin); // LOW = pressed, HIGH = released // Print formatted data to Serial Monitor Serial.print("X: "); Serial.print(xValue); Serial.print(" | Y: "); Serial.print(yValue); Serial.print(" | Button: "); Serial.println(buttonState == LOW ? "Pressed" : "Released"); delay(200); // Read every 200ms }
X: 512 | Y: 512 | Button: ReleasedX: 12 | Y: 1020 | Button: Pressed
Download the Code
⬇ Download joystick_control.ino
Get the complete, commented Arduino sketch from Google Drive
Download Code (.ino)How to Test Your Circuit
Wire the module
Connect all 5 pins following the wiring table above. Double-check VRX→A0, VRY→A1, SW→D2.
Upload the code
Open Arduino IDE, paste or open the .ino file, select Board: Arduino UNO and your COM port, then click Upload.
Open Serial Monitor
Go to Tools → Serial Monitor and set baud rate to 9600. You should immediately see X/Y readings around 512.
Move the joystick
Push left/right — watch the X value drop to ~0 or rise to ~1023. Push up/down to see Y values change.
Press the joystick down
Press the stick like a button — Serial Monitor should print "Button: Pressed". Release to see "Released".
Applications
What You Learn
More Arduino Projects
Beginner
Intermediate
Advanced
Comments
Post a Comment