Arduino analog joystick module

Connecting an analog joystick module to Arduino is very simple. The analog joystick module for Arduino has five pins: Ground, Vcc, X-axis, Y-axis and Key or SW.
Connect Ground and Vcc pins to Gnd and Vcc pins of Arduino, the X pin to A0, the Y pin to A1 and SW pin to Digital pin 2 of Arduino.

The analog joysticks are made from potentiometers, so the X and Y pins will output analog values. The SW/Key pin is Digital.
Arduino has an analogRead range from 0 to 1023. When using analogRead() function, the X and Y pins should show values closed to 512, if the joystick is in standby/ middle position.

Use the following code to test the setup. Open Serial Monitor and set the Baud Rate to 115200 baud.

// joystick pins
const int SWITCH = 2; // digital pin 2 connected to SW output of JoyStick
const int X_AX = A0; // analog pin 0 connected to X output of JoyStick
const int Y_AX = A1; // analog pin 1 connected to Y output of JoyStick

int rows = 0;

void setup() {
  pinMode(SWITCH, INPUT);
  digitalWrite(SWITCH, HIGH);

void loop() {
  if (rows > 20) {
    rows = 0;
  Serial.print("          ");
  Serial.print("          ");

  delay(100); // delay in between reads for stability

void showHeading() {
  Serial.println("Switch      X axis      Y axis");

Upload the sketch to Arduino and wait for output in Serial Monitor. You should see values closed to 512 when the joystick is in the standby position.
Different joysticks will output different values, depending on the quality and the brand of the joystick.