Overview of Analog Joystick
Analog Joystick
Applications such as video games that require movement in a 2D plane often use analog joysticks as input devices.
An analog joystick outputs two voltages: one representing the position along the X-axis and another for the position along the Y-axis. These voltages vary depending on the joystick’s position.
For more details on analog joysticks and how to use them, refer to the “Analog Joystick” topic in the Sensors and Modules section.
To interface the analog joystick with the Arduino Uno, we use the ADC (Analog-to-Digital Converter) on the Arduino UNO’s microcontroller.
Connection Diagram of Analog Joystick with Arduino
Interfacing Analog Joystick Module with Arduino UNO
Get the X and Y Positions of Analog Joystick with Arduino Uno
Displaying the analog joystick voltages for X and Y directions on the Arduino serial monitor.
In this setup, we’ll use the Arduino’s analog pins to read and process the joystick’s analog voltage outputs.
Analog Joystick Code for Arduino Uno
const int joystick_x_pin = A2;
const int joystick_y_pin = A1;
void setup() {
Serial.begin(9600); /* Define baud rate for serial communication */
}
void loop() {
int x_adc_val, y_adc_val;
float x_volt, y_volt;
x_adc_val = analogRead(joystick_x_pin);
y_adc_val = analogRead(joystick_y_pin);
x_volt = ( ( x_adc_val * 5.0 ) / 1023 ); /*Convert digital value to voltage */
y_volt = ( ( y_adc_val * 5.0 ) / 1023 ); /*Convert digital value to voltage */
Serial.print("X_Voltage = ");
Serial.print(x_volt);
Serial.print("\t");
Serial.print("Y_Voltage = ");
Serial.println(y_volt);
delay(100);
}