KY-023 joystick dual axis XY module with Arduino??

What is the joystick module, how does it work, and how to use the joystick module with Arduino are some questions that may arise when programming an Arduino.

Hello, guys welcome back to my blog today we going to see how to connect ps2 module with Arduino.

What is KY-023 Joystick Module

A joystick is an input device that is used for controlling the movement of the cursor on the screen, controlling drones. Many of us have seen a joystick when playing video games, play stations, controlling robots, cars or toy moments, etc. 

Analog joystick module

There are different types of joysticks such as displacement joysticks, hand-operated joysticks, and thumb/fingertip-operated joysticks. Joysticks provide interface control between users and hardware or software devices. The joystick provides moments in the up direction, down direction, left direction & right direction.

How joystick works??

This joystick is made of a two-independent potentiometer, one controls the x-axis, and another controls the y-axis.

Arduino 2 axis joystick

When we move the slide in x-direction i.e. horizontally then the value of potentiometer Vrx changes from 0 to 1023. Similarly, when we move the slider in the y-direction, the value of potentiometer Vry changes from 0 to 1023. it is just like using a 2-potentiometer separately. Joystick gets rid of this, it is easy and more convenient to use.

Arduino thumb joystick

When the joystick is at a stable or normal position (center position) then the value of Vrx & Vry will be 512 as the center of 1023 is 512. when there is a moment in x- the direction the value varies from 0 to 1023. If there is a moment in x- direction i.e. towards the left- side then the value of Vrx will be 1023 & when there is a moment on the right side then the value of Vrx will be 0. Similarly for y-direction if there is a moment toward upwards (upside) then the value of Vry will be 1023 & if we move the joystick in a downward direction (downside) then the value of Vry will be 0.

The value of Vrx and Vry varies between 0 to 1023  according to the position of the joystick moment.

xy joystick module

This joystick has a switch. This switch is nothing but a push button that will be connected to the digital pin of Arduino. You can hear a tickle sound when you press the button.

Parts Required 

  • Joystick module
  • Arduino
  • Jumper wires
  • Breadboard

Schematic Diagram


joystick module Arduino

To connect joystick pin Vrx and Vry to analog pin A0 and A1 of Arduino & VCC pin to 5v, GND to ground Switch pin to 8.

Code

int vx=A0;                    // declaring and initiating x-axis pin to analog pin A0
int vy=A1;                     // declaring and initiating y-axis pin to analog pin A1
int switch_pin=8;              // declaring and initiating switch pin to analog pin 8
void setup() {
 Serial.begin(115200);           // setting baud rate to 115200
pinMode(vx,INPUT);               // setting x-axis pin as input
pinMode(vy,INPUT);               // setting y-axis pin as input
pinMode(switch_pin,INPUT);       // setting switch pin as input
}
void loop() {
 int x_value = analogRead(vx);    // reading value of x-axis
 int y_value = analogRead(vy);    // reading value of y-axis
 int switch_value = digitalRead(switch_pin);  // reading value of switch pin
Serial.print("x-axis:");          
Serial.print(x_value);            // printing value of x-axis
Serial.print(" | ");
Serial.print("y-axis:");          // printing value of y-axis
Serial.print(y_value);
Serial.print(" | ");
Serial.print("Switch:");
Serial.print(switch_value);       // printing value of switch
Serial.println(" | ");
delay(350);
}

First, initialize the pins of the joystick. We have declared a variable name to read values from the joystick. In the void setup, we configure pins as INPUT or OUTPUT. We want to read values from the joystick so we will declare it as INPUT. Also, we configure the baud rate to 115200.

In a void loop, we continuously read values from the joystick and store values in a variable. here we read the x-axis, y-axis & switch value in a variable in x_value, y_value, switch_value and print the following variable using the serial print function. We have introduced a delay of 350 milliseconds.

Output
joystick module Arduino code

You can see output on the Serial monitor. When we move the joystick in x and y directions we get different values of the x-axis and y-axis according to the moment.

 Conclusion - 

Today we learn about what is the joystick, how to use a joystick with Arduino, the application of the joystick & working principles of the joystick.


"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."

Close Menu