PlayStation 2 Controller Arduino Library v1.0

Posted in Arduino Libraries by Bill
5 Jun 2010
PlayStation 2 Controller Arduino Library v1.0

Want to interface a PlayStation 2 Controller with an Arduino Microcontroller? You have come to the right place. Below you will find a link to download an easy to use library that takes care of all the interfacing for you, so you can start using the controller right away for your project. Don’t forget to link your cool projects in the comments, I’d love to see what you do with the library.

First, a brief history:

A while ago, I spent countless days trying to interface an Arduino and a Play Station 2 controller. I wanted to build a controller for my SAGAR robot, and figured PS2 + Arduino would be perfect. However, no matter what I did, no existing library would work for me. I shelfed the project for a while, but recently found this forum post with some code by a member named Shutter. I tried out the code, and to my surprise, IT WORKED!!! Well, mostly anyway. It didn’t really have analog stick support, nor was it formatted into an easy to use library. Well, through need I spent some time adding analog stick support, and formatted it into a library. It works fine on my Arduino Pro mini.

The first fully working version (v1.0) is now available. The big change is you can now define what pins of the Arduino are used, no longer are you tied to pins 10-13. Also, vibration (Rumble) and analog button pressure readings (how hard is a button being pressed) are now working. The library has changed names. PSX -> PS2X to avoid confusion with other sets of code. Sorry for those that need to change a bunch of their programs.

I had to rewrite a lot of code that powers the library, there is not much left of the original code written by Shutter of Arduino forums. There were many bug fixes, now the controller should automatically be in analog mode, and the mode button should be locked. There’s also a catch to make sure not to much time has past since the last controller reading. If there was, it will configure the controller again, in case it timed out.

Source Code available on GitHub project page.

 

Download PS2X

 

Old Versions:

PS2X_lib_v1.0

PS2X lib v1.41

Arduino PS2 Controller Library V1.5

Wiring the Controller

The seemingly most notorious part prone to errors is wiring the controller to the Arduino. Lynxmotion sells a nice breakout connector available HERE.

WARNING! WARNING!

The Lynxmotion adapter may not follow the color wiring diagram below. Instead, it might follow this diagram. (Lynxmotion only). Be careful and use a volt meter to confirm pin positions.

Or, to wire the controller not using the Lynxmotion adapter, follow the pinout in the following picture from the amazing CuriousInventor PS2 Interface Guide:

Image Source: CuriousInventor

A note from CuriousInventor: “Red – Power: Many sites label this as 5V, and while this may be true for Play Station 1 controllers, we found several wireless brands that would only work at 3.3V. Every controller tested worked at 3.3V, and the actual voltage measured on a live Playstation talking to a controller was 3.4V. McCubbin says that any official Sony controller should work from 3-5V.”

Here’s a video demonstrating the library.

Guitar Hero Controller Example

The following is a video and source for example of how to use the library with a Guitar Hero Controller.

Source: (tested with v1.5, should work with v1.6 as well)

Guitar Hero Example Arduino Code

Guitar Hero Example Processing Code (thanks Patrick)

Another Demo

Author unknown

Project Showcase

Hey guys, feel free to drop a link off in the comments to your project that uses my library. I’ll post it here.

  1. Guitar Hero Axe Controlled Flamethrowers by Chris Marion
  2. Scanalogic Review by CuriousInventor
  3. SAGAR by Me
  4. Remote Controlled Robot (Video) by ‘teachengineering
  5. ‘America Dream’  Electric Hammock by Stephen Shaffer
  6. Life size R2D2 robot by Dan
  7. Simon Says via DDR mat by Dalpix

Trouble?

Follow my troubleshooting guide first and then visit the Support Forum if you still can’t get it working. Don’t ask for help in the comments below.
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Share
  1. 587 Comments.

    • nicksen782No Gravatar says:

      I just registered. (Was Nick, now nicksen782.) I am using a 5V Arduino. I am using a total of five resistors with one each on each wire to the PS2 remote except for the ground wire. I spent some time using trial and error to determine resistor values.

      The resistors that I used are:
      5V = 180 ohm, voltage drop of 1.85V (3.15v to the controller)
      ATT = 5.6K ohm, voltage drop of 1.08V (3.92 to the controller)
      CLK = 5.6K ohm, voltage drop of 1.15V (3.85 to the controller)
      CND = 860K ohm, voltage drop of 1.39V (3.61 to the controller)
      DAT = 180 ohm, voltage drop of 8.7mV

      I also tried 15K resistors on the ATT and CLK lines but I would get erratic responses from the Arduino program. I don’t really think that a resistor is needed on the DAT line as it is only one-direction and comes from the controller. I could never get a reading much over a few mV on that resistor. Everything works.

    • BillNo Gravatar says:

      Added version 0.2. Download link in original blog post above.
      Fixes:
      fixed config_gamepad miss-spelling
      removed ‘PS’ from beginning of ever function

      New features:
      added new functions:
      NewButtonState();
      NewButtonState(button);
      ButtonPressed(button);
      ButtonReleased(button);

      New Example Sketch has example of all the new functions, and explains what they do. Pressures and Rumble coming soon.

    • enrik says:

      Hi You did a good job and thank you

      If you are able to read when any button is released?

    • BillNo Gravatar says:

      Yes, download the example file and it goes over a function that returns TRUE when a button is released.

    • GeneNo Gravatar says:

      This is some great stuff! I am trying to use this library using a wired controller, but cant seem to get my arduino to read anything. Can you help me with a hard wired example of what goes where?

    • BillNo Gravatar says:

      I’m on travel for work, so it will be a while before I can properly document the wiring.

      Are you using an official Sony controller, or a third party knock off? Do you have the right board selected in your arduino IDE?

    • GeneNo Gravatar says:

      Whenever you get a chance is fine, I understand completely!
      I am using the official Sony wired controller, with the correct board selected in the IDE. One controller makes the serial monitor go crazy without me pressing anything, the other controller does nothing regardless of what I press.

      Thanks for the reply!

    • BillNo Gravatar says:

      I’ve added a new version that has some error reporting, and might increase compatibility with some controllers. Try it and let me know if it works / what it says.

      For wiring, if you use my example sketch, it should be
      yellow -> 10
      orange -> 11
      brown -> 12
      blue -> 13

      use the picture in the post if the colors don’t match up.

    • GeneNo Gravatar says:

      Thanks a lot, I will try it soon and let you know the results!

    • ericbarchNo Gravatar says:

      I have a standard Sony Dualshock 2 controller and a 5v Arduino…everything appears to be working and I’m getting data from the controller…but the analog sticks keep giving back weird feedback. It seems like the number will cycle quite a few times just while moving the joystick in one direction.

      If I push the joystick all the way to the bottom and slowly move it up, the values look something like:

      255
      230
      115
      80
      50
      0
      90
      50
      30
      0
      40
      30
      20
      0

      Have you come across this at all? I wonder if it’s an issue with the 5v signals…but you said you were using a Sony controller, right?

    • BillNo Gravatar says:

      I am using the 5V arduino.

      Is this from the example sketch?

      Hmm, Do the Analog pressures from the D Pad fluctuate the same way?
      Try both versions (1.0 and 1.2) and see if they both do it. Remember you have to replace the files in the libraries folder and restart the Arduino IDE.

      You can also try the resistor values here to drop the voltage down.

      Quote

      “I am using a 5V Arduino. I am using a total of five resistors with one each on each wire to the PS2 remote except for the ground wire. I spent some time using trial and error to determine resistor values.

      The resistors that I used are:
      5V = 180 ohm, voltage drop of 1.85V (3.15v to the controller)
      ATT = 5.6K ohm, voltage drop of 1.08V (3.92 to the controller)
      CLK = 5.6K ohm, voltage drop of 1.15V (3.85 to the controller)
      CND = 860K ohm, voltage drop of 1.39V (3.61 to the controller)
      DAT = 180 ohm, voltage drop of 8.7mV

      I also tried 15K resistors on the ATT and CLK lines but I would get erratic responses from the Arduino program. I don’t really think that a resistor is needed on the DAT line as it is only one-direction and comes from the controller. I could never get a reading much over a few mV on that resistor. Everything works.”

    • BobNo Gravatar says:

      Hey I have a Arduino Duemilanova 128, Sony Analogue SCPH-10010 A connected to a cable extender I cut off because I didn’t want to cut the end off the controller. I have to assume the pinout on the cable extender matches the pinout on the controller. I have the following wire colors(red,black,blue,yellow,green,orange,grey, brown). The only wires not used are the grey and green. I have loaded the latest sketch to the arduino but it still gives me the message “No controller connected” in serial monitor window. Any ideas?

    • BillNo Gravatar says:

      Well Bob, you are using the exact same controller as mine, so you shouldn’t be getting issues.

      You can verify the wire colors by using an ohm meter with the cut off end of the extension cord. Verify continuity between the correct wire color and the correct pin on the connect as listed in the diagram in my post.

      Is the Duemilanova 5V or 3.3V? Maybe try using resistors to drop down the voltage if it’s 5V. The last comment before yours has the resistor values one guy found would work.

      make sure you have the right board selected in your Arduino IDE. If the wrong board is selected the clock will be different and may not work.

      How are you powering the controller? Does the red light come on, if you press the analog button does the red light come on (or go off)?

    • BobNo Gravatar says:

      I have just check the wires on the extender with the multimeter and they match both color and location as per the image above. The board is 5 Volt operating voltage. I have resistors setup and checked the voltage which was between 3.3 and 4.3 volts. The Arduino Alpha IDE is version 0018 which only gave me one option for the Duemilenova, correct com port is selected as well as baud rate set at 57600. The controller is powered by the Arduino and when I push the analog button it does turn red. I’m new to this so I am kinda at a loss for ideas now.

    • BobNo Gravatar says:

      I have just checked the wires on the extender cable and they match the above pinout. I have verified they are connected properly and have added the resistors. The operating voltage is 5v for the board. I have checked the voltage at each wire and it ranged between 3.3 and 4.3v with the resistors. The Arduino Alpha software version 0018 only has one option for the duemilanove 128. When I push the analog button the red light dows come on. Any ideas? I’m lost. Thanks for the help

Leave a Reply