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 wil 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);
  Serial.begin(115200);
  showHeading();
}

void loop() {
  rows++;
  if (rows > 20) {
    rows = 0;
    showHeading();
  }
  Serial.print(digitalRead(SWITCH));
  Serial.print("          ");
  Serial.print(analogRead(X_AX));
  Serial.print("          ");
  Serial.println(analogRead(Y_AX));

  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.

Leave a Reply

Your email address will not be published. Required fields are marked *