How to connect and use Analog Joystick with Arduino

//How to connect and use Analog Joystick with Arduino

Using an Analog Joystick with an Arduino


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.


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.


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);

void loop() {
  Serial.print("Switch:  ");
  Serial.print("X-axis: ");
  Serial.print("Y-axis: ");

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|


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

    hi 🙂 bross 🙂

  2. jhen Tan August 14, 2019 at 2:49 am - Reply

    ahhh ok… i get it.. thanks for the info Nate

  3. 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)

  4. 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

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

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

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

    is not working
    %I⸮⸮Q⸮U⸮⸮NPY⸮⸮⸮I⸮I⸮⸮u⸮N ⸮u

    • 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