Wiring guide

How to wire a joystick to an Arduino Uno

To wire a joystick to an Arduino Uno, 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
Arduino Uno · joysticklive0.000 s 0.00x
Drag the stick and press it
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

Joystick to Arduino Uno 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 pinArduino Uno pinWire colorWhy
VCC5VredPowers both tracks.
GNDGNDblueThe other end of both tracks.
VRXpin A0amberThe X wiper, an analog input.
VRYpin A1greenThe Y wiper, an analog input.
SWpin 2violetShorts to GND when the stick is pressed; the sketch turns on the internal pull-up.
Voltage

Power and logic levels for joystick and Arduino Uno

Logic level

The Arduino Uno’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 Uno’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 Uno’s 5V and each axis gives 0 to 1023. The Uno’s ADC is ten bits against its 5 V rail, so analogRead() gives 0 to 1023.

Pins to leave alone

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

The sketch

The working code for the Arduino Uno

Library: none, the sketch reads both axes with analogRead(). This is the sketch the circuit above runs.

Joystick on Arduino Uno pin A0 and pin A1
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);
}
Output

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  Center
Pitfalls

Common mistakes wiring a joystick to an Arduino Uno

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.