Arduino Nano Joystick Module
The Joystick Module project demonstrates how to interface a joystick with an Arduino Nano. The joystick consists of two potentiometers (for X and Y axis movement) and a push‑button. This project reads the X and Y values, along with the button state, and prints them to the serial monitor.
Components Needed
- Arduino Nano
- Joystick Module (e.g. KY‑023 or similar)
- Jumper Wires
Circuit Setup
1. Connect Joystick Module to Arduino Nano:
Connect the VCC pin of the joystick module to the 5V pin on the Arduino Nano.
Connect the GND pin of the joystick module to the GND pin on the Arduino Nano.
Connect the VRx pin (X axis) of the joystick module to the A0 pin on the Arduino Nano.
Connect the VRy pin (Y axis) of the joystick module to the A1 pin on the Arduino Nano.
Connect the SW pin (Button) of the joystick module to the D2 pin on the Arduino Nano.
How the Joystick Module Works
The X and Y axes are two potentiometers that output analog voltages from about 0–1023 on the Arduino when the stick is moved. The push‑button outputs a digital signal (LOW when pressed, HIGH when released) on the SW pin.
// Joystick pins
const int VRxPin = A0; // X axis
const int VRyPin = A1; // Y axis
const int buttonPin = 2; // SW pin
// Variables
int xVal = 0;
int yVal = 0;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("=== ARDUINO NANO JOYSTICK READY ===");
Serial.println("X Y Button");
}
void loop() {
// Read X and Y axes
xVal = analogRead(VRxPin);
yVal = analogRead(VRyPin);
// Read button state (LOW when pressed)
buttonState = digitalRead(buttonPin);
// Print values
Serial.print(xVal);
Serial.print(" ");
Serial.print(yVal);
Serial.print(" ");
Serial.println(buttonState);
delay(100); // Smooth reading
}
Instructions
1. Circuit Setup:
Wire the joystick module to the Arduino Nano as described in the circuit setup section.
2. Code Upload:
Connect the Arduino Nano to your computer via USB.
Open the Arduino IDE and paste the provided Joystick code.
Select the appropriate board (Arduino Nano) and port from the Tools menu.
Upload the code to the Arduino Nano.
3. Testing:
Once the code is uploaded, open the Serial Monitor (9600 baud).
Move the joystick and press the button. The X, Y values, and button state (0=pressed, 1=released) will be printed to the serial monitor.
Applications
Game Controllers: Use the joystick to build custom game controllers or arcade‑style inputs.
Robotics: Control robot movement (forward/backward, left/right) with the joystick module.
User Interfaces: Integrate the joystick into interactive projects requiring directional input and button presses, such as menu navigation or camera control.
Troubleshooting
- X/Y values stuck at 0 or 1023? Check that VRx and VRy are connected to A0 and A1, and VCC and GND to the Arduino.
- Button always shows HIGH? Make sure the
INPUT_PULLUPmode is used and the wiring is correct; if the module is active‑LOW, your code is right. - Values flicker wildly? Hold the stick more gently; avoid pressing too hard on the potentiometer limits.
Best Practices and Notes
- Mount the joystick in an enclosure that allows smooth movement without blocking the knobs.
- Use
INPUT_PULLUPfor the button pin so you don’t need an external pull‑up resistor. - For more advanced control (e.g., sending joystick data to a PC as a USB gamepad), you can use the
Joysticklibrary on compatible Arduino boards or Nano with USB‑CDC support. - Adjust the delay (100 ms) if you need faster or smoother readings for your application.
Extensions and Ideas
You can extend this project by mapping the joystick to control a servo motor, drive an LCD menu, or send X/Y data to a computer for custom games or UI control.