How to Interface PS2 Controller to Arduino Uno

Introduction

Have you ever think of what else could you do with your old PS2 Game Controller? What if you can use it to control something other than your PS2? Yes, it is possible with the help of microcontroller. In this blog post, I will walk you through on how to interface the PS2 game controller to an Arduino Uno microcontroller. But before that, let me tell you how I come up to this idea.

I am thinking on controlling my Bipedal Robot project wireless. My options are the following:

  1. Android mobile phone (with Serial bluetooth terminal) and HC-06 bluetooth module but since I do not have personal android mobile phone(I am just borrowing from my son :)), this idea is not possible.
  2. Dual joystick from defective game controller interface to ESP32. I am able to make two ESP32 to talked with each other using BLE but this will cost more.
  3. Wireless PS2 controller, perfect idea! I personally don’t have it, so I tried to purchased from local electronics store. After tinkering with it, it was just a waste of money. After that, I decided to purchased it online.

So without further discussion, we need the following materials for this tinkering.

Bill Of Materials

  1. PS2 game controller (preferably wireless but the wired one functions the same).
  2. Arduino Uno microcontroller.
  3. PS2 Arduino library.
  4. A couple of jumper wires.

Hardware Instruction

So lets build it!

  1. Connect the PS2 DAT pin (pin 1) to digital pin D13 of Arduino Uno.
  2. Connect the PS2 CMD pin (pin 2) to digital pin D12 of Arduino Uno.
  3. Connect the PS2 GND pin (pin 4) to GND pin of Arduino Uno.
  4. Connect the PS2 VCC pin (pin 5) to +5V pin of Arduino Uno.
  5. Connect the PS2 C/S pin (pin 6) to digital pin D11 of Arduino Uno.
  6. Connect the PS2 CLK pin (pin 7) to digital pin D10 of Arduino Uno.

Now our setup is ready. We will be using the PS2 Library for Arduino of Bill Porter. I just modified the example sketch for PS2 so suit our needs. Lets upload the sketch and see the result.

Video Demonstration

Source Code

  1#include "PS2X_lib.h"  //for v1.6
  2
  3PS2X ps2x; // create PS2 Controller Class
  4
  5//right now, the library does NOT support hot pluggable controllers, meaning 
  6//you must always either restart your Arduino after you conect the controller, 
  7//or call config_gamepad(pins) again after connecting the controller.
  8int error = 0; 
  9byte type = 0;
 10byte vibrate = 0;
 11
 12void setup(){
 13  Serial.begin(115200);
 14  
 15  error = ps2x.config_gamepad(10,12,11,13, false, false);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
 16  
 17  if(error == 0){
 18    Serial.println("Found Controller, configured successful");
 19    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
 20    Serial.println("holding L1 or R1 will print out the analog stick values.");
 21    Serial.println("Go to www.billporter.info for updates and to report bugs.");
 22  }
 23   
 24  else if(error == 1)
 25    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
 26   
 27  else if(error == 2)
 28    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
 29   
 30  else if(error == 3)
 31    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
 32   
 33  type = ps2x.readType(); 
 34    
 35  switch(type) {
 36    case 0:
 37      Serial.println("Unknown Controller type");
 38      break;
 39    case 1:
 40      Serial.println("DualShock Controller Found");
 41      break;
 42    case 2:
 43      Serial.println("GuitarHero Controller Found");
 44      break;
 45    default:
 46      break;
 47  }
 48}
 49
 50void loop(){
 51   /* You must Read Gamepad to get new values
 52   Read GamePad and set vibration values
 53   ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
 54   if you don't enable the rumble, use ps2x.read_gamepad(); with no values
 55   
 56   you should call this at least once a second
 57   */
 58   
 59    ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
 60    
 61    if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
 62      Serial.println("Start is being held");
 63    if(ps2x.Button(PSB_SELECT))
 64      Serial.println("Select is being held");
 65         
 66    if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
 67      Serial.println("UP is being held");
 68    }
 69    if(ps2x.Button(PSB_PAD_RIGHT)){
 70      Serial.println("Right is being held");
 71    }
 72    if(ps2x.Button(PSB_PAD_LEFT)){
 73      Serial.println("LEFT is being held");
 74    }
 75    if(ps2x.Button(PSB_PAD_DOWN)){
 76      Serial.println("DOWN is being held");
 77    }   
 78    
 79    if (ps2x.NewButtonState())               //will be TRUE if any button changes state (on to off, or off to on)
 80    {
 81        if(ps2x.Button(PSB_L3))
 82          Serial.println("L3 pressed");
 83        if(ps2x.Button(PSB_R3))
 84          Serial.println("R3 pressed");
 85        if(ps2x.Button(PSB_L2))
 86          Serial.println("L2 pressed");
 87        if(ps2x.Button(PSB_R2))
 88          Serial.println("R2 pressed");
 89    }   
 90         
 91    if(ps2x.ButtonPressed(PSB_RED))             //will be TRUE if button was JUST pressed
 92          Serial.println("Circle just pressed");
 93
 94    if(ps2x.ButtonReleased(PSB_RED))             //will be TRUE if button was JUST released
 95          Serial.println("Circle just released"); 
 96
 97
 98    if(ps2x.ButtonPressed(PSB_PINK))             //will be TRUE if button was JUST pressed
 99          Serial.println("Square just pressed");
100                  
101    if(ps2x.ButtonReleased(PSB_PINK))             //will be TRUE if button was JUST released
102          Serial.println("Square just released");     
103
104    if(ps2x.ButtonPressed(PSB_GREEN))             //will be TRUE if button was JUST pressed
105          Serial.println("Triangle just pressed");
106                  
107    if(ps2x.ButtonReleased(PSB_GREEN))             //will be TRUE if button was JUST released
108          Serial.println("Triangle just released");   
109
110    if(ps2x.ButtonPressed(PSB_BLUE))             //will be TRUE if button was JUST pressed
111          Serial.println("X just pressed"); 
112                  
113    if(ps2x.ButtonReleased(PSB_BLUE))             //will be TRUE if button was JUST released
114         Serial.println("X just released");   
115
116         
117    //if(ps2x.NewButtonState(PSB_BLUE))            //will be TRUE if button was JUST pressed OR released
118    //     Serial.println("X just changed");    
119    
120    if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
121    {
122        Serial.print("Stick Values:");
123        Serial.print(ps2x.Analog(PSS_LY), DEC); // LY
124        Serial.print(",");
125        Serial.print(ps2x.Analog(PSS_LX), DEC); // LX
126        Serial.print(",");
127        Serial.print(ps2x.Analog(PSS_RY), DEC); // RY
128        Serial.print(",");
129        Serial.println(ps2x.Analog(PSS_RX), DEC); // RX
130    } 
131    
132 delay(50);
133}

Call To Action

That’s all everyone. Please kindly leave your comments and suggestions in the comment box.

Thank you. Happy tinkering!



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.