How to Connect Joystick With An Arduino

Posted on

Arduino Joystick Tutorial – Joystick is an electronic component that has a lever that can be moved in several directions. This lever is used to control position in a particular direction or coordinates. The joystick has two axes, X and Y axis. Some types of joysticks have a button. In this tutorial you will use an analog joystick as shown in the following picture.


How the Joystick works

This joystick will issue voltage on VRX and VRY pins. This voltage will swing from 0V to 5V when the lever is moved. To get a precise value, this voltage will be converted using the Analog Digital Converter (ADC) on Arduino. This voltage comes from two potentiometers (X and Y axis) which are the main components of this Joystick. Because this joystick has two axes, the ADC used is A0 and A1 on Arduino.

You can see the picture below:

joystick arduino tutorial

To be able to read the ADC value generated from the voltage issued by the Joystick, please use the following program:

int adc_x = 0; //initial value ADC A0
int adc_y = 0; //initial value ADC A1
int button = 0; //initial value button Pin

void setup()
pinMode(2, INPUT); //Pin Button

void loop()
adc_x = analogRead(A0); //read ADC A0
adc_y = analogRead(A1); //read ADC A1
button = digitalRead(2);//read button pin condition

Serial.print(adc_x); //show ADC A0 value
Serial.print(", ");
Serial.print(adc_y); //show ADC A1 value
Serial.print(", ");
Serial.print(!button);//show button Value
Serial.println(", ");

In this Arduino Joystick Tutorial experiment, the Arduino joystick is not moved. This means that we only see the results of the above program through the serial monitor. Please open the serial monitor. You can see value like picture below:

joystick arduino tutorial

There are 3 values ​​obtained, 525, 515, and 0. The initial value of 525 is the value of the A0 ADC. The second value of 515 from ADC A1 and third 0 is the condition of the button. Values ​​525 and 515 are the middle values ​​of the ADC. Because the joystick is not moved, the position of the potentiometer is in the middle.

Read other articles  How to use ADC In Arduino Tutorial

The ADC value at 0V of voltage is 0. the ADC value at 5V is 1023. Because the potentiometer position is in the middle, the result is also half of the total ADC value, 1023/2 is 511.5.

Please note that each component has tolerance, meaning that the value obtained can change 5% higher or lower. Therefore, the ADC value obtained from the joystick in this experiment is 525 and 515.

Reading the button shows the value 0, meaning that the button has not been pressed. If the button is pressed it will produce a value of 1. You can try move the knob or the joystick button in any direction, then the ADC value will swing from 0-1023.

In the Arduino Joystick Tutorial expreriment above, the ADC value of swinging is unstable, tends to change, even though this change only occurs in the last digit as in example 525 changes to 522, 524, 525, 524 and so on. This condition is not good if we use it to control equipment. So it takes one technique to make this Arduino joystick more stable, called smooting. how is the program? please visit my article “ADC Arduino Tutorial“.

Leave a Reply

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