top of page

Arduino PS2 Controller

  • 2 days ago
  • 1 min read



Arduino PS2 controller

An Arduino PS2 controller setup lets an Arduino read button presses and analog sticks from a PlayStation 2 (PS2) gamepad using a PS2 gamepad interface library (commonly PS2X / PS2X_lib).


The controller communicates over a simple synchronous serial link (PS2’s SPI-like protocol):

the Arduino provides the clock and commands, the controller returns data, and a “select/attention” line activates communication.


Pins:

  • DAT (Data out) → Arduino input (MISO-like) 13

  • CMD (Command in) → Arduino output (MOSI-like) 11

  • CLK (Clock) → Arduino output (SCK-like) 12

  • SEL / ATT (Select/Attention) → Arduino output (chip-select-like) 10

  • VCC → 3.3V or 5V (depends on the adapter/controller board; many prefer 3.3V)

  • GND → GND


#include <PS2X_lib.h> 

constexpr uint8_t PS2_DAT = 13;
constexpr uint8_t PS2_CMD = 11;
constexpr uint8_t PS2_SEL = 10;
constexpr uint8_t PS2_CLK = 12; 

//controller features
#define pressures   false
#define rumble      false

PS2X ps2x;

int error = 0; //holds the result of trying to initialize/configure the PS2 controller. 0 usually means success 
byte vibrate = 0;  //vibrate strength
byte type = 0; //store a small ID code for controller type


void setup() {
  Serial.begin(57600);
  error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  type = ps2x.readType(); 

}

void loop() {
  if(error == 1) //skip loop if no controller found
      return;
  ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed

  if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed
    Serial.print("Up is presed.");
  }
  if(ps2x.Button(PSB_PAD_RIGHT)){
    Serial.print("Right is presed.");
  }
  if(ps2x.Button(PSB_PAD_LEFT)){
    Serial.print("LEFT is presed.");
  }
  if(ps2x.Button(PSB_PAD_DOWN)){
    Serial.print("DOWN is presed.");
  }   
  if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
  {
    Serial.println("X just changed");
  }
  delay(50);  
}

Comments


bottom of page