A PS2 Style Joystick is very similar to PS2 controllers and is a thumb operated device. There are 2 potentiometers ( one for each axis and of 10k each ). It even includes a select button which can be pressed. In this tutorial, we will be using a 2-axis joystick module.
Parts Required
- 1 x Arduino UNO ( I have used Mega )
- 1 x Breadboard ( Optional )
- 1 x PS2 Style Joystick
- 1 x 1K Ohm Resistor
- Jumper Wires
Pin Wiring
Sensor Pin Arduino Pin
5V 5V GND GND VRx ( X Axis ) A0 VRy ( Y Axis ) A1 SW ( Push Button ) 3
Fritzing Sketch
You can draw your Fritzing Sketch too by downloading Fritzing from : http://fritzing.org/
Code
int xpin = 0; int ypin = A1; int button_key = 3; int button_value; void setup() { pinMode(button_key,INPUT); Serial.begin(9600); } void loop() { int xval,yval,buttonval; xval = analogRead(xpin); yval = analogRead(ypin); button_value = digitalRead(button_key); Serial.print("xval "); Serial.println(xval,DEC); Serial.print("yval "); Serial.println(yval,DEC); Serial.print("Button is "); if(button_value == HIGH) { Serial.println("Not Pressed"); } else { Serial.println("Pressed"); } delay(500); }
Output
The output shows the X and Y value ranging between 0-1023 and tells whether the button is pressed or not.
Direction Control of X and Y
The following figure gives an indication of how the outputs will respond when the joystick is pushed in various directions.
What Is a Potentiometer ?
We already saw that there are 2 Potentiometers in the Joystick but how do we use a Potentiometer ? A potentiometer is simply a variable resistor that allows us to modify its internal resistance. It works by adjusting the conductor length between the central and side terminals. High resistance potentiometer is used to prevent a lot of current passing through it ( 10K ohm ). It allows you to control things or adjust settings in a program. Let us see how to use it.
Parts Required
Fritzing Sketch
Arduino Sketch
int pin = 0; void setup() { pinMode(pin,INPUT); Serial.begin(9600); } void loop() { int value = analogRead(pin); Serial.println(value); delay(500); }
Output
Rotate the Potentiometer head to observe different readings. Inside each Aduino, there is an Analog-to-Digital-Converter ( ADC ) which converts Analog Signal values to Digital. More on ADCs will be discussed in the coming tutorials.
Final Comments
As a fun experiment, try integrating the PS2 Style Joystick with Processing ( Example mentioned in the previous tutorial : Processing Tutorial ) . Thank you for visiting the website. In case of any doubt, feel free to ask.
Pretty! This has been an extremely wonderful article.Many thanks for providing these details.