Joystick Arduino Uno Basic Code Tutorial

Joystick with Arduino Uno

Basic Code of Joystick Control With Arduino Uno – A joystick is an electronic component that has a lever that can be moved in several directions. This lever is used to control the position in a certain direction or coordinates.

Joysticks have two axes, X and Y axes. Some types of joysticks also have a button. In this tutorial, we will use an analog joystick as shown in the following image.

This joystick will produce a voltage on the 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 the Arduino.

This voltage comes from the two potentiometers (X and Y axis) which are the main components of this Joystick. Because this joystick has two axes, in this tutorial, the ADC used on Arduino is A0 and A1.

Joystick Connection

For the connection, see below:

Joystick Control With Arduino Uno Basic Code

Basic Code

To be able to read the ADC value generated from the voltage produced by the Joystick please use the following code:

int adc_x = 0;  
int adc_y = 0;  
int btnCond = 0; 


void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT);  
}

void loop()
{
  adc_x = analogRead(A0); 
  adc_y = analogRead(A1); 
  btnCond = digitalRead(2);

  Serial.print(adc_x); 
  Serial.print(", ");
  Serial.print(adc_y); 
  Serial.print(", ");
  Serial.print(!btnCond);
  Serial.println(", ");
}

In this 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 and you will get the value as shown in the following image:

Joystick Arduino Uno Basic Code Tutorial

The value will change when the lever is moved. There are 3 values ​​obtained above, 525, 515, and 0.

  • 525 is the value of ADC A0.
  • 515 is the value of ADC A1
  • 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 potentiometer is in the middle.

The ADC value at 0V voltage is 0, while the ADC value at 5V voltage is 1023. Because the potentiometer 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 a tolerance, meaning that the value obtained can change 5% higher or lower. Therefore, the ADC values ​​obtained from the joystick in this experiment are 525 and 515.

The button reading shows a value of 0, meaning the button has not been pressed. If the button is pressed, it will produce a value of 1. Try moving the knob or joystick button in any direction, the ADC value will swing from 0-1023.

In the above experiment, the ADC value swings unstable, tends to fluctuate, although this change only occurs in the last digit, for example, 525 changes to 522, 524, 525, 524 and so on.

This condition is not good if we use it to control the equipment. So it takes a technique to make this Arduino joystick more stable which is called smoothing.

How’s the program? please visit my article ADC Smooting Tutorial on Arduino.

Similar Posts