Interfacing Joystick with Arduino Uno

Interfacing Joystick with Arduino Uno

by Tanya Bansal

I am sure most of us have always been fascinated with remote control in our childhood days or video games. If not you might have atleast seen them. So today we will see how we can make such controllers for ourselves. For this, we will understand first how one of the main elements of these constructors i.e. a joystick actually works and how we can interface it with our Arduino Uno.

Since Arduino Uno supports both digital and analog input. So connecting it with a joystick won’t be a tough task as we can connect it directly to the microcontroller. Unlike a raspberry pi which supports only digital input and output, and to integrate with it we will require an analog to digital converter first. 

Okay so let’s get started!

We will divide this article into parts.First, we will cover the working of joystick and uno in brief. Then we will learn how to interface joystick with Arduino Uno and display values on the Serial Monitor. Further, you can apply this knowledge of joystick and toggle LED’s using your joystick. You can also control DC motors with joystick using uno and using which you can make your own remote control toy car. 

What is a Joystick?

A joystick is an input device consisting of a stick that pivots on a base and outputs its angle or direction to the device it is controlling.

Joysticks are widely used to control video games, RC toy cars, machines such as cranes, trucks, underwater unmanned vehicles, wheelchairs, surveillance cameras, and many more.

For this article, we will be using a general joystick module.

So, this joystick gives us analog output as discussed above, which is basically the values of the X-axis and Y-axis. We move our joystick in a 2D plane and hence the analog value it outputs changes according to its direction. This is done using 2 potentiometers placed on two sides of the joystick for x and y-axis outputs. Moving the joystick handle actually moves the pointer of the potentiometer. This gives the value of the voltage which is provided to the microcontroller from its VRx and VRy pins.

This joystick also has a push-button that can be used for various purposes.

Pin Configuration

A joystick module has 5 pins namely 

+5vPositive supply terminal 
VRxVoltage Proportional to X-axis
VRyVoltage Proportional to Y-axis
SWSwitch/ Push button

A few key points about this module is that-

It operates on 5V

Its operating temperature is in the range of 0 to 70 degrees celsius.

It has an internal potentiometer value of 10k ohm

Also Read How to write a Simple RISC Assembly Program?

Next, we have our microcontroller Arduino Uno

For interfacing with a joystick, we will use the analog pins of Arduino Uno 

There is a total of 6 analog input pins in the microcontroller, we will use any 2 for taking the input for the x and y-axis. The microcontroller has a built-in ADC which will convert the input analog values to digital values. Using these digital values we can control LEDs or buttons or just turn on or off a DC Motor. Further, the analog inputs can directly be used with devices that support analog values.

To connect the joystick to the Arduino Uno, you will require a joystick, an Arduino Uno, and a few jumper wires.

Below are the connections

Connect the Gnd of Joystick to Gnd of Arduino and the VCC to 5v of the microcontroller.

Next Connect VRx of Joystick to analog pin A4 and VRy of Joystick to analog pin A5 of the uno.

If you wish to use the switch/push-button present in the joystick then connect its SW pin to any digital pin of the Arduino.

That’s it! 

Next is the Code to take analog input from the joystick as it is moved in different directions and then we display the values of both x and y in the serial monitor.

Setup – 

Open your Arduino IDE or Visual Studio Code, wherever you are comfortable writing your code, then write the code as shown below.

In the Tools, Make sure the selected Board is Arduino Uno. Then, Plugin in your Arduino Uno, and then select the Port from the tools.

Then first Verify your code to make sure there are no syntax errors and then hit upload.

Now, Go to Tools and Open the Serial Monitor or use keys Ctrl +  Shift + M.

And select the baud rate 9600. 

The values will start to appear quickly on the screen. See the image of the Serial Monitor below, you will get something like this.

Also, you can select the auto scroll option in the bottom left of the serial monitor, this will scroll the screen on its own as the new values will come up.

You will observe that the lowest value will be 0 and the highest possible value will be 1023 for both X and Y.


 const int x_pin = A4;
 const int y_pin = A5;

 void setup() {



 void loop() {

   int x_val, y_val; 

   x_val = analogRead(x_pin);  

   y_val = analogRead(y_pin);

   Serial.print("X = ");



   Serial.print("Y = ");




Code Explanation –

Start your code by defining the pin number for x and y analog input values. Here, we have named the X-axis input pin as x_pin and equates it to pin A4 of Arduino. Similarly, pin A5 is named y_pin and will be used to take input values from the y-axis of the joystick.

Next, inside the void setup()  function we write Serial.begin(9600). This is used to start the serial communication so that Arduino can send out commands through a USB connection.

The value 9600 signifies the baud rate which tells how fast the data is to be sent.

So this code runs only once when the program starts.

Next, we call the void loop function, which as the name suggests will continuously run again and again till the program runs.

So, inside the void loop function, we first define x_val and y_val as integer values which will be used to store the readings from pins A4 and A5 every time the loop executes.

Then using the analogRead() function we read both the values and store them.

Next, to print the values to the serial monitor, we use Serial.print().

Since, Serial.print() is used all the values i.e the string “X=”, then the value of x_val then string “Y = ” and then the value of y_val, all these will print in the same line.

And we use Serial.println() with the value of y_val so that the next set of values are printed in a new line.

At last, a delay of 100 microseconds is given so that it gets easier to read values from the screen as the next value will be read and displayed after 100 microseconds. 

You can increase or decrease the time delay at your convenience. 

 Thus, this loop will continue to work till the program is running.

Also note, that Arduino is case sensitive, so int_X and int_x will be different values.

An image of the serial monitor is shown below

So this is how we can interface our joystick with an Arduino Uno in just a few steps.

Now, the next thing you can try is to control the LED’s

Take a note of the range of values you get when you move the joystick handle in a certain direction. Do this for all the different directions you wish to use.

Now check if, for example, your input comes in range 0 to 508 for X and 508 to 1023 for Y then, the first LED will glow. Similarly, for another range, another LED will glow, and so on.

You can also use the Switch in the joystick to glow another LED of a different color. The switch will give you a digital output i.e. either it is pressed(High)  or not pressed LOW.

Related Posts

Leave a Comment