How to connect and use Analog Joystick with Arduino

//How to connect and use Analog Joystick with Arduino
TUTORIAL


Using an Analog Joystick with an Arduino

OVERVIEW

Analog joysticks are a great way to add some control in your projects.

In this tutorial we will learn how to use an analog joystick module.

CONNECTIONS

We need 5 connections to the joystick.

The connection are : Key, Y, X, Voltage and Ground.

“Y and X” are Analog and “Key” is Digital. If you don’t need the switch then you can use only 4 pins.

THE CODE

Analog joysticks are basically potentiometers so they return analog values.

When the joystick is in the resting position or middle, it should return a value of about 512.

The range of values go from 0 to 1024.

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}
TUTORIAL VIDEO
DOWNLOAD

Just copy and paste the above code in the Arduino IDE to program your Arduino.

By | 2018-02-23T17:31:54-05:00 January 12th, 2015|Tutorials|

24 Comments

  1. CS June 6, 2020 at 7:14 pm - Reply

    For me, the joystick button works, but the joystick can’t seem to find the x or y dimensions…

    Joystick Button test…
    Switch: 1/0
    X-axis: 1
    Y-axis: 1

    Already you notice that the x and y are at one.

    X&Y Joystick Test…
    Switch: 1
    X-axis: 1
    Y-axis: 1

    It never switches to 0 or any other number in that case.

  2. Mike McRoberts June 3, 2020 at 4:00 pm - Reply

    Why did you add the line:

    digitalWrite(SW_pin, HIGH);

    When that pin is set to an INPUT?

  3. lior May 17, 2020 at 9:55 am - Reply

    what is the model of the joystick? i need to order it

  4. MixTow May 11, 2020 at 6:33 pm - Reply

    Thank you very much for the invitation :). Best wishes.
    PS: How are you?

  5. MixTow March 3, 2020 at 1:50 am - Reply

    hi guys :). I am looking for help for me and my girl. i am from France

  6. Davide February 21, 2020 at 11:26 am - Reply

    how can I use it as a controller in videogames? Do I need a library?

  7. guest February 1, 2020 at 5:18 pm - Reply

    @then, So do you want to connect your game boy with arduino board? If so, you can buy a gameboy-to-usb adapter if that’s what you want? I’m guessing you want to use your gameboy as a controller or something, ngl that’s pretty cool but I think you need a usb gameboy adapter because you have to connect the: gameboy plug into gameboy and other side of the cable should have a regular usb, and the usb has to connect to a arduino adapter, or depending on your board you might be able to plug in the usb straight into the board.

  8. guest February 1, 2020 at 5:10 pm - Reply

    Yo i got it 😊! all you have to do is run serial monitor and see the baud, the project runs on 9600 baud, ours run on 115200. I know this because I checked my serial monitor and clicked baud rate option. So all that’s left to do is rewrite 9600 to 115200 in the project code. So where ever you see 9600, remove it and write 115200 instead.

    -hope this helped bro! Also let me know if this doesn’t solve your issue.

  9. guest January 2, 2020 at 12:20 pm - Reply

    Never mind I wired my cables wrong, now the problem is fixed. But I still have a question: How would you take this arduino analog stick and connect it/make it work on a PS3? When ever I connect the analog stick using a USB port, the PS3 just says >unrecognizable device plugged in< and I just can't get the joystick to control/work on PS3 even though it works on PC. So if any you have a solution to this please let me know, thanks again.

    • then January 23, 2020 at 3:29 am - Reply

      exactly what i want to ask, but in my case I want to connect to my Gameboy device which D-Pad is a digital. All i can see is tutorial about this but never ever mention how to connect to other device
      maybe we are just nub bro

    • Michael April 16, 2020 at 1:46 pm - Reply

      how did you get it to work on PC I am trying ti use a joystick to give me a “clutch pedal” in BeamNG Drive but I can’t make my computer recognize it as a controller

  10. guest January 2, 2020 at 11:45 am - Reply

    Hi If anyone could help me it would be greatly appreciated. So when ever I run Serial Monitor my codes end up differently, this is how they look:

    Switch: 1

    X-axis: 1023

    Y-axis: 1023

    … and changing the direction of my joystick doesn’t really help either, it just changes one of the two axis to: 1021 or 1022… around that range.
    I would just like to have my joystick read inputs so that I can use this for a future game-controller project. Once again If anyone could please help me it would be greatly appreciated. -Thanks

  11. Untener Olivér December 13, 2019 at 4:47 am - Reply

    Hello!
    I’ve followed all these instructions, but the switch button doesn’t work.
    It always gives 0, either I press the joystick button, or no. Have you any idea about it?

  12. itaoccup December 7, 2019 at 12:43 am - Reply

    Ok. I use translate and will be good ok?

  13. rardBon December 6, 2019 at 1:24 pm - Reply

    i am from Italy hello. Can you help me translate? /rardor

  14. Ryan Sanchez November 29, 2019 at 2:11 am - Reply

    How can I use multiple joy stick modules?

  15. rardBon November 1, 2019 at 9:27 pm - Reply

    hi 🙂 bross 🙂

  16. Carlo July 30, 2019 at 1:56 am - Reply

    The joystick is not working correctly for me. This is what I see when the Joystick is:
    MIDDLE: X:683, Y:343 (I would expect 512, 512)
    LEFT:X:998 Y:493 (I would expect 1023, 512)
    RIGHT: X:5 Y:1 (I would expect 0, 512)
    UP:X:650: Y:650 (I would expect 512, 1023)
    DOWN: x: 650, y:0 (I would expect 512, 0)

  17. Hennerzockt January 23, 2019 at 11:55 am - Reply

    can i use this for farming simulator?

    • Sokken September 9, 2019 at 3:23 am - Reply

      of course you can

  18. Colin December 24, 2018 at 10:39 am - Reply

    Thank you, yes changing to 9600 also worked for me:)
    Great example thank you!

  19. Lit October 8, 2018 at 11:43 pm - Reply

    is not working
    %I⸮⸮Q⸮U⸮⸮NPY⸮⸮⸮I⸮I⸮⸮u⸮N ⸮u
    5&%⸮⸮%I⸮%I⸮Q⸮%⸮⸮⸮QŅ_U⸮N⸮N⸮^]Q⸮

    • Nate October 30, 2018 at 1:24 am - Reply

      You need to make sure that your computer’s serial read frequency and your serial frequency are the same. What might fix it is changing the line “Serial.begin(115200);” to “Serial.begin(9600);.” In the video he used 115200 for some reason but 9600 is the most common frequency and I think is also the default in Arduino IDE. The serial frequency is what you declare in your code, and the computer’s read frequency is the number that you select in the drop down menu of the serial monitor in Arduino IDE.

      • maxym11 November 13, 2019 at 4:51 am - Reply

        Thanks, this also fixed my problem

Leave A Comment