Loading... Please wait...

Joystick

Joystick

This example shows how to use a PS2 style joystick.

Hardware Required

  • Arduino Board
  • PS2 style joystick

Circuit

There are five connections for a PS2 style joystick.

  • Connect +5V to +5V
  • Connect GND to GND
  • Connect SW to digital pin 2
  • Connect VRx to analog pin A0
  • Connect VRy to analog pin A1
 

Code

/* 

* There are five connections for a PS2 style joystick
* Connect +5V to +5V
* Connect GND to GND
* Connect SW to digital pin 2
* Connect VRx to analog pin A0
* Connect VRy to analog pin A1
*
* Sketch will display X & Y values values from 0 to 1023
* And an indicator showing the switch state (1 = OFF, 0 = ON, switch is driven low when on)
*
*/

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
}

void loop() {
  Serial.print("Switch: ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}

 

What's News

newsletter

Copyright 2019 oddWires. All Rights Reserved.
 Sitemap | oddWires