How to use the PS2 Joystick with Arduino

How to use the PS2 Joystick with Arduino

 

PS2 Joystick with Arduino

 

INTRODUCTION

The PS2 style joystick is a thumb-operated device when put to creative use offers a convenient way of obtaining operator input to move a set of motors or servos. It fundamentally consists of two potentiometers and a push-button switch.

APPLICATIONS

Most common applications is to control the motion of a robot in different directions. It is also commonly used for gaming.

WIRING DIAGRAM

GND – GND

VCC - 5V

X – Analog Pin A0

Y – Analog Pin A1

K – Digital Pin 2

PS2 Joystick connection Arduino

CODE

 

int Xin= A0; // X Input Pin
int Yin = A1; // Y Input Pin
int KEYin = 2; // Push Button

void setup ()
{
  pinMode(KEYin, INPUT);
  digitalWrite(KEYin, HIGH);
  Serial.begin (115200); 
}
void loop ()
{
  int xVal, yVal, buttonVal;
  
  xVal = analogRead (Xin);
  yVal = analogRead (Yin);
  buttonVal = digitalRead(KEYin);
  
  Serial.print("X = ");
  Serial.println (xVal, DEC);
  
  Serial.print ("Y = ");
  Serial.println (yVal, DEC);
  
  Serial.print(buttonVal);
 
  delay (500);
}
 

END RESULT

Open the serial monitor and watch the X and Y values change from 0 to 1023 as you move the joystick around. Also, try pressing down on the joystick to activate the built-in button

ADDITIONAL RESOURCES

http://henrysbench.capnfatz.com/henrys-bench/arduino-sensors-and-input/arduino-ps2-joystick-tutorial-keyes-ky-023-deek-robot/