Introduction: PS2 Wire Controller and Arduino (control LEDs)

In this tutorial, I like to show my work on interfacing PS2 wire controller with Arduino through controlling LEDs on breadboards.

ORIGINAL Work and source file :

http://store.curiousinventor.com/guides/PS2/

http://www.billporter.info/?p=240

Parts you will need :

1 wire PS2 controller

1 Arduino Uno, or any other version

LEDs

Jumpers

Breadboard

A Computer

Step 1: Step 1 : Wiring PS2 Controller and Arduino

This is the part that took me quite a while, try to keep the plastic case connector and follow the wiring digram, in my case I cut the wire and lost track of the ordering or the wire label. But I figured it out in the end.

Here are my PS2 controller wire codes :

1 brown Data
2 orange command

3 violet vibration

4 black ground

5 red power

6 yellow attention

7 blue clock

8 ---- not used

9 green acknoledge

Hook up the connection between ps2 and arduion according to the diagram .

Step 2: Step 2 : the Code

Please download the library written by Bill Porter at https://github.com/madsci1016/Arduino-PS2X

#include //for v1.6

/****************************************************************** * set pins connected to PS2 controller: * replace pin numbers by the ones you use ******************************************************************/ #define PS2_DAT 8 //14 This pin will need a pull up resistor #define PS2_CMD 11 //15 #define PS2_SEL 10 //16 #define PS2_CLK 12 //17

/****************************************************************** * select modes of PS2 controller: * - pressures = analog reading of push-butttons * - rumble = motor rumbling * uncomment 1 of the lines for each mode selection ******************************************************************/ #define pressures true //#define pressures false #define rumble true //#define rumble false

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning //you must always either restart your Arduino after you connect the controller, //or call config_gamepad(pins) again after connecting the controller.

int error = 0; byte type = 0; byte vibrate = 0;

void setup(){ // To output to LEDS pinMode(3, OUTPUT); //Top pinMode(5, OUTPUT); // Right pinMode(6, OUTPUT); // Bot pinMode(9, OUTPUT); //Left pinMode(7, OUTPUT); //Square pinMode(13, OUTPUT); //Triangle pinMode(4 OUTPUT); // Circle pinMode(2, OUTPUT); // X Serial.begin(57600); delay(3); //added delay to give wireless ps2 module some time to startup, before configuring it //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble); if(error == 0){ Serial.print("Found Controller, configured successful "); Serial.print("pressures = "); if (pressures) Serial.println("true "); else Serial.println("false"); Serial.print("rumble = "); if (rumble) Serial.println("true)"); else Serial.println("false"); Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;"); Serial.println("holding L1 or R1 will print out the analog stick values."); Serial.println("Note: Go to www.billporter.info for updates and to report bugs."); } else if(error == 1) Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips"); else if(error == 2) Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

else if(error == 3) Serial.println("Controller refusing to enter Pressures mode, may not support it. "); // Serial.print(ps2x.Analog(1), HEX); type = ps2x.readType(); switch(type) { case 0: Serial.print("Unknown Controller type found "); break; case 1: Serial.print("DualShock Controller found "); break; case 2: Serial.print("\nController found , Press any button to check \n"); break; case 3: Serial.print("Wireless Sony DualShock Controller found "); break; } }

void loop() { /* You must Read Gamepad to get new values and set vibration values ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255) if you don't enable the rumble, use ps2x.read_gamepad(); with no values You should call this at least once a second */ if(error == 1) //skip loop if no controller found return; else if(type == 2){ //Guitar Hero Controller ps2x.read_gamepad(); //read controller

if(ps2x.ButtonPressed(RED_FRET)) { Serial.println("CIRCLE"); digitalWrite(7,HIGH); delay(50); digitalWrite(7,LOW); } if(ps2x.ButtonPressed(YELLOW_FRET)) {Serial.println("TRIANGLE"); digitalWrite(13,HIGH); delay(100); digitalWrite(13,LOW); } if(ps2x.ButtonPressed(BLUE_FRET)) {Serial.println("X"); digitalWrite(4,HIGH); delay(50); digitalWrite(4,LOW); } if(ps2x.ButtonPressed(ORANGE_FRET)) {Serial.println("SQUARE"); digitalWrite(2,HIGH); delay(50); digitalWrite(2,LOW); }

if(ps2x.ButtonPressed(ONE_LEFT)) Serial.println("ONE LEFT Pressed"); if(ps2x.ButtonPressed(ONE_RIGHT)) Serial.println("ONE RIGHT Pressed"); if(ps2x.ButtonPressed(TWO_RIGHT)) Serial.println("TWO Right Pressed"); if(ps2x.ButtonPressed(TWO_LEFT)) Serial.println("TWO LEFT Pressed"); if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed { Serial.println("Up "); digitalWrite(3,HIGH); delay (50); digitalWrite(3,LOW); } if(ps2x.Button(DOWN_STRUM)){ Serial.println("DOWN"); digitalWrite(6,HIGH); delay (50); digitalWrite(6,LOW); } if(ps2x.Button(RIGHT_STRUM)){ Serial.println("RIGHT"); digitalWrite(5,HIGH); delay (50); digitalWrite(5,LOW); } } if(ps2x.Button(LEFT_STRUM)){ Serial.println("LEFT"); digitalWrite(9,HIGH); delay (50); digitalWrite(9,LOW); } // if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed // Serial.println("Start is being held"); // if(ps2x.Button(PSB_SELECT)) // Serial.println("Select is being held"); // delay(50); }

Step 3: The Results

Click on Serial print to view the results

Or connect LEDs positive side to OUTPUT pins 3,5,6,9,7,13,4,2 and all LEDs negative side to ground to view results.

The code can be use for LEFT 1,2 and RIGHt 1,2 buttons but I run out of pin, you can use shift register to get more output. The joysticks of the ps2 controller , I'm not sure how to run them yet , but will be added when I figured them out.