Arduino Joystick Module Example

Arduino Joystick Module Example

In one of my rather frequent eBay visits, I came across a nifty little joystick module, much similar to the analog thumb-stick on the PlayStation 2 controllers. The module is very easy to use with an Arduino uno and only costs a few dollars. Several different versions are available from eBay, Adafruit, Sparkfun and other vendors, but they essentially work the same.

Overview

The module has 5 pins: Vcc, Ground, X, Y, Key. Note that the labels on yours may be slightly different, depending on where you got the module from. The thumbstick is analog and should provide more accurate readings than simple ‘directional’ joysticks tat use some forms of buttons, or mechanical switches. Additionally, you can press the joystick down (rather hard on mine) to activate a ‘press to select’ push-button.

We have to use analog Arduino pins to read the data from the X/Y pins, and a digital pin to read the button. The Key pin is connected to ground, when the joystick is pressed down, and is floating otherwise. To get stable readings from the Key /Select pin, it needs to be connected to Vcc via a pull-up resistor. The built in resistors on the Arduino digital pins can be used. For a tutorial on how to activate the pull-up resistors for Arduino pins, configured as inputs, see this..

Arduino Joystick Module Demo

Wiring Diagram

Arduino Joystick / Thumbstick module wiring diagram

Sample Code

int xPin = A1;
int yPin = A0;
int buttonPin = 2;

int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);

  //activate pull-up resistor on the push-button pin
  pinMode(buttonPin, INPUT_PULLUP); 
  
  // For versions prior to Arduino 1.0.1
  // pinMode(buttonPin, INPUT);
  // digitalWrite(buttonPin, HIGH);
  
}

void loop() {
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  
  Serial.print("X: ");
  Serial.print(xPosition);
  Serial.print(" | Y: ");
  Serial.print(yPosition);
  Serial.print(" | Button: ");
  Serial.println(buttonState);

  delay(100); // add some delay between reads
}
Tagged with: , , ,

11 Comments on “Arduino Joystick Module Example

  1. My serial look like that :
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 15 | Y: 28 | Button: 1
    X: 81 | Y: 92 | Button: 1
    X: 151 | Y: 164 | Button: 1
    X: 236 | Y: 252 | Button: 1
    X: 353 | Y: 373 | Button: 1
    X: 489 | Y: 510 | Button: 1
    X: 647 | Y: 677 | Button: 1
    X: 808 | Y: 832 | Button: 1
    X: 950 | Y: 972 | Button: 1
    X: 1023 | Y: 1023 | Button: 1
    X: 1023 | Y: 1023 | Button: 1
    X: 1023 | Y: 1023 | Button: 1
    X: 1023 | Y: 1023 | Button: 1
    X: 1023 | Y: 1011 | Button: 1
    X: 903 | Y: 881 | Button: 1
    X: 771 | Y: 745 | Button: 1
    X: 621 | Y: 590 | Button: 1
    X: 447 | Y: 416 | Button: 1
    X: 281 | Y: 252 | Button: 1
    X: 158 | Y: 138 | Button: 1
    X: 61 | Y: 45 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1
    X: 0 | Y: 0 | Button: 1 //loop i dont know why
    It isnt responding when i move joystick

  2. does your joystick top out at maybe 50% of its mechanical travel, and read the maximum value for the rest of the journey to the edge? do you have any advice to avoid this, I could do with a bit more flexibility

    • I have this exact same problem. The strange thing is that both X and Y act the same way. I’ve measured the resistance across the range of motion and it does indeed go to a limit value around 50% of the range of motion. I’ve found that the middle is around 4k ohm, max is 5k and min is 0. My guess (but I’m new to all this) is that the wiper should be reoriented so that the middle would be at 2.5k, but I’m not sure if that’s possible without breaking it.

  3. Thank you, the first part has worked. I do not have that adapter for the 8×8 matrix, but later I would like to try that part too. Keep up the good work

  4. How can I apply map() for this joystick module ?

    I was added this to my joystick arduino sketch
    int xValue = analogRead(joyX);
    xValue = map(xValue, 0,1023, 0, 255);

    but still on the serial monitor I see values between 0 and 1023; instead of between 0 and 255. Do you know how to fix this problem ? thank you.

    • You are stating that your xValue is = analogread, but also stating that is it = map.

      X_pinValue = analogRead(X_pin);
      XVal = map (X_pinValue, 0, 1023, 0 ,255);

      works

  5. You are stating that your xValue is = analogread, but also stating that is it = map.

    X_pinValue = analogRead(X_pin);
    XVal = map (X_pinValue, 0, 1023, 0 ,255);

    works

Leave a Reply to RogerRabbit Cancel reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.