How to wire a joystick to an Arduino Nano
To wire a joystick to an Arduino Nano, connect VCC to 5V, GND to GND, VRX to pin A0, VRY to pin A1 and SW to pin 2. The module is two potentiometers and a switch, so it runs from 5 V and each axis gives a reading across the converter’s whole range. The sketch below uses no library beyond what the board includes. It reads both axes and prints X, Y and the direction the stick is pushed.
- 5 V logic
- Library: none
- 5 wires
Joystick to Arduino Nano wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Joystick pin | Arduino Nano pin | Wire color | Why |
|---|---|---|---|
| VCC | 5V | red | Powers both tracks. |
| GND | GND | blue | The other end of both tracks. |
| VRX | pin A0 | amber | The X wiper, an analog input. |
| VRY | pin A1 | green | The Y wiper, an analog input. |
| SW | pin 2 | violet | Shorts to GND when the stick is pressed; the sketch turns on the internal pull-up. |
Power and logic levels for joystick and Arduino Nano
Logic level
The Arduino Nano’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.
Supply
The joystick takes 5 V from 5V. That is the Arduino Nano’s 5 V rail, so the part shares the current the board can supply.
This pair
The module is two potentiometers and a switch, so it runs from the Arduino Nano’s 5V and each axis gives 0 to 1023. Ten bits against 5 V, 0 to 1023, the same as an Uno. A6 and A7 are extra analog inputs that cannot be digital pins at all.
Pins to leave alone
A6 and A7 are analog only: pinMode(A6, OUTPUT) does nothing and digitalRead(A7) is not there to call. D0 and D1 are the USB serial port.
The working code for the Arduino Nano
Library: none, the sketch reads both axes with analogRead(). This is the sketch the circuit above runs.
const int X_PIN = A0; // VRX, pin A0
const int Y_PIN = A1; // VRY, pin A1
const int SW_PIN = 2; // the button, pin 2
void setup() {
Serial.begin(9600);
pinMode(SW_PIN, INPUT_PULLUP); // LOW while the stick is pressed
}
void loop() {
int xr = analogRead(X_PIN); // 0 to 1023
int yr = analogRead(Y_PIN);
bool pressed = digitalRead(SW_PIN) == LOW;
const char* dir = "Center";
if (xr < 1023 / 4) dir = "Left";
else if (xr > 1023 / 4 * 3) dir = "Right";
else if (yr < 1023 / 4) dir = "Up";
else if (yr > 1023 / 4 * 3) dir = "Down";
Serial.print("X: ");
Serial.print(xr);
Serial.print(" Y: ");
Serial.print(yr);
Serial.print(" ");
Serial.print(dir);
Serial.println(pressed ? " pressed" : "");
delay(250);
}
What you should see
Open the serial monitor at 9600 baud. Each line shows the X and Y readings and a word for the direction: Center when the stick rests, Right, Left, Up or Down when it is pushed.
The push button under the stick is a separate switch on its own pin; it reads low when pressed.
The serial monitor prints this, recorded from the circuit above with the stick pushed right and then released:
X: 512 Y: 512 Center
X: 1023 Y: 512 Right
X: 512 Y: 512 CenterCommon mistakes wiring a joystick to an Arduino Nano
No pull-up on the button
Without INPUT_PULLUP the SW pin floats and reports presses nobody made. Most modules have no resistor of their own.
Expecting an exact center
The rest position is a few counts either side of the middle and differs per axis. Use a dead zone, or read the rest position at startup and measure from it.
An axis that runs backwards
Modules are mounted in different directions. Swap the two pins in the code, or flip a reading with 1023 - reading.
Build your own joystick project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.