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

Trackbacks / Pingbacks

  1. Fix Arduino Error ‘wire’ Was Not Declared In This Scope Windows XP, Vista, 7, 8 [Solved]
  2. Fix Arduino Error ‘spi’ Was Not Declared In This Scope Windows XP, Vista, 7, 8 [Solved]
  3. کنترل سروو موتور با آرداینو توسط موس
  4. ArduinoでPS2コントローラを制御する 回路編 | Let's making!Let's making!
  1. 587 Comments.

    • […] PlayStation 2 Controller Arduino Library v1.0 « The Mind … – 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 …… […]

    • […] PlayStation 2 Controller Arduino Library v1.0 « The Mind … – 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 …… […]

    • I have tried your library, but it seems that it can found the PS2X_lib, it said like this:

      PS2X_Example:25: error: ‘PS2X’ does not name a type
      PS2X_Example.ino: In function ‘void setup()’:
      PS2X_Example:44: error: ‘ps2x’ was not declared in this scope
      PS2X_Example.ino: In function ‘void loop()’:
      PS2X_Example:100: error: ‘ps2x’ was not declared in this scope
      PS2X_Example:102: error: ‘GREEN_FRET’ was not declared in this scope
      PS2X_Example:104: error: ‘RED_FRET’ was not declared in this scope
      PS2X_Example:106: error: ‘YELLOW_FRET’ was not declared in this scope
      PS2X_Example:108: error: ‘BLUE_FRET’ was not declared in this scope
      PS2X_Example:110: error: ‘ORANGE_FRET’ was not declared in this scope
      PS2X_Example:113: error: ‘STAR_POWER’ was not declared in this scope
      PS2X_Example:116: error: ‘UP_STRUM’ was not declared in this scope
      PS2X_Example:118: error: ‘DOWN_STRUM’ was not declared in this scope
      PS2X_Example:121: error: ‘PSB_START’ was not declared in this scope
      PS2X_Example:123: error: ‘PSB_SELECT’ was not declared in this scope
      PS2X_Example:126: error: ‘ORANGE_FRET’ was not declared in this scope
      PS2X_Example:128: error: ‘WHAMMY_BAR’ was not declared in this scope
      PS2X_Example:132: error: ‘ps2x’ was not declared in this scope
      PS2X_Example:134: error: ‘PSB_START’ was not declared in this scope
      PS2X_Example:136: error: ‘PSB_SELECT’ was not declared in this scope
      PS2X_Example:139: error: ‘PSB_PAD_UP’ was not declared in this scope
      PS2X_Example:141: error: ‘PSAB_PAD_UP’ was not declared in this scope
      PS2X_Example:143: error: ‘PSB_PAD_RIGHT’ was not declared in this scope
      PS2X_Example:145: error: ‘PSAB_PAD_RIGHT’ was not declared in this scope
      PS2X_Example:147: error: ‘PSB_PAD_LEFT’ was not declared in this scope
      PS2X_Example:149: error: ‘PSAB_PAD_LEFT’ was not declared in this scope
      PS2X_Example:151: error: ‘PSB_PAD_DOWN’ was not declared in this scope
      PS2X_Example:153: error: ‘PSAB_PAD_DOWN’ was not declared in this scope
      PS2X_Example:156: error: ‘PSAB_CROSS’ was not declared in this scope
      PS2X_Example:158: error: ‘PSB_L3’ was not declared in this scope
      PS2X_Example:160: error: ‘PSB_R3’ was not declared in this scope
      PS2X_Example:162: error: ‘PSB_L2’ was not declared in this scope
      PS2X_Example:164: error: ‘PSB_R2’ was not declared in this scope
      PS2X_Example:166: error: ‘PSB_TRIANGLE’ was not declared in this scope
      PS2X_Example:170: error: ‘PSB_CIRCLE’ was not declared in this scope
      PS2X_Example:172: error: ‘PSB_CROSS’ was not declared in this scope
      PS2X_Example:174: error: ‘PSB_SQUARE’ was not declared in this scope
      PS2X_Example:177: error: ‘PSB_L1’ was not declared in this scope
      PS2X_Example:177: error: ‘PSB_R1’ was not declared in this scope
      PS2X_Example:179: error: ‘PSS_LY’ was not declared in this scope
      PS2X_Example:181: error: ‘PSS_LX’ was not declared in this scope
      PS2X_Example:183: error: ‘PSS_RY’ was not declared in this scope
      PS2X_Example:185: error: ‘PSS_RX’ was not declared in this scope

    • I have tried your library, but it seems that it can found the PS2X_lib, it said like this:

      PS2X_Example:25: error: ‘PS2X’ does not name a type
      PS2X_Example.ino: In function ‘void setup()’:
      PS2X_Example:44: error: ‘ps2x’ was not declared in this scope
      PS2X_Example.ino: In function ‘void loop()’:
      PS2X_Example:100: error: ‘ps2x’ was not declared in this scope
      PS2X_Example:102: error: ‘GREEN_FRET’ was not declared in this scope
      PS2X_Example:104: error: ‘RED_FRET’ was not declared in this scope
      PS2X_Example:106: error: ‘YELLOW_FRET’ was not declared in this scope
      PS2X_Example:108: error: ‘BLUE_FRET’ was not declared in this scope
      PS2X_Example:110: error: ‘ORANGE_FRET’ was not declared in this scope
      PS2X_Example:113: error: ‘STAR_POWER’ was not declared in this scope
      PS2X_Example:116: error: ‘UP_STRUM’ was not declared in this scope
      PS2X_Example:118: error: ‘DOWN_STRUM’ was not declared in this scope
      PS2X_Example:121: error: ‘PSB_START’ was not declared in this scope
      PS2X_Example:123: error: ‘PSB_SELECT’ was not declared in this scope
      PS2X_Example:126: error: ‘ORANGE_FRET’ was not declared in this scope
      PS2X_Example:128: error: ‘WHAMMY_BAR’ was not declared in this scope
      PS2X_Example:132: error: ‘ps2x’ was not declared in this scope
      PS2X_Example:134: error: ‘PSB_START’ was not declared in this scope
      PS2X_Example:136: error: ‘PSB_SELECT’ was not declared in this scope
      PS2X_Example:139: error: ‘PSB_PAD_UP’ was not declared in this scope
      PS2X_Example:141: error: ‘PSAB_PAD_UP’ was not declared in this scope
      PS2X_Example:143: error: ‘PSB_PAD_RIGHT’ was not declared in this scope
      PS2X_Example:145: error: ‘PSAB_PAD_RIGHT’ was not declared in this scope
      PS2X_Example:147: error: ‘PSB_PAD_LEFT’ was not declared in this scope
      PS2X_Example:149: error: ‘PSAB_PAD_LEFT’ was not declared in this scope
      PS2X_Example:151: error: ‘PSB_PAD_DOWN’ was not declared in this scope
      PS2X_Example:153: error: ‘PSAB_PAD_DOWN’ was not declared in this scope
      PS2X_Example:156: error: ‘PSAB_CROSS’ was not declared in this scope
      PS2X_Example:158: error: ‘PSB_L3’ was not declared in this scope
      PS2X_Example:160: error: ‘PSB_R3’ was not declared in this scope
      PS2X_Example:162: error: ‘PSB_L2’ was not declared in this scope
      PS2X_Example:164: error: ‘PSB_R2’ was not declared in this scope
      PS2X_Example:166: error: ‘PSB_TRIANGLE’ was not declared in this scope
      PS2X_Example:170: error: ‘PSB_CIRCLE’ was not declared in this scope
      PS2X_Example:172: error: ‘PSB_CROSS’ was not declared in this scope
      PS2X_Example:174: error: ‘PSB_SQUARE’ was not declared in this scope
      PS2X_Example:177: error: ‘PSB_L1’ was not declared in this scope
      PS2X_Example:177: error: ‘PSB_R1’ was not declared in this scope
      PS2X_Example:179: error: ‘PSS_LY’ was not declared in this scope
      PS2X_Example:181: error: ‘PSS_LX’ was not declared in this scope
      PS2X_Example:183: error: ‘PSS_RY’ was not declared in this scope
      PS2X_Example:185: error: ‘PSS_RX’ was not declared in this scope

    • kiranksNo Gravatar says:

      Thanks Bill!! Lib just worked as mentioned. I brought a cheap 8$ PS controller from wallmart (Innovation 739549 PlayStation 2 Dual Shock 2 Controller (PS2)) and hooked up wires as you mentioned in the Lib. Using a Arduino Mini and able to get all the readings.
      Found Controller, configured successful
      Try out all the buttons, X will vibrate the controller, faster as you press harder;
      holding L1 or R1 will print out the analog stick values.
      Go to http://www.billporter.info for updates and to report bugs.
      DualShock Controller Found
      X just changed
      Circle just pressed
      Triangle pressed
      Square just released
      Select is being held
      Start is being held
      R2 pressed
      L2 pressed
      LEFT held this hard: 50
      LEFT held this hard: 110
      LEFT held this hard: 108
      DOWN held this hard: 74
      DOWN held this hard: 81
      Right held this hard: 123
      Right held this hard: 136
      Right held this hard: 116
      Right held this hard: 58
      Up held this hard: 105
      Up held this hard: 118
      Up held this hard: 83
      Stick Values:128,128,128,128
      Stick Values:128,128,128,128
      Stick Values:128,128,128,128
      Stick Values:128,128,0,128
      Stick Values:128,128,0,128
      Stick Values:128,128,0,128
      Stick Values:128,128,0,128
      Stick Values:128,128,0,128
      Stick Values:128,128,0,128

      Stick Values:128,128,128,128
      Stick Values:128,128,128,128
      Stick Values:128,128,128,128
      Stick Values:255,20,128,128
      Stick Values:255,0,128,128
      Stick Values:255,0,128,128
      Stick Values:255,0,128,128

    • […] removed) connecting to Arduino UNO board…etc.? https://github.com/madsci1016/Arduino-PS2X The Mind of Bill Porter فقط این که اینا هیچ کدوم آماده نیست خودتون باید سیم […]

    • Gary GrahamNo Gravatar says:

      Thanks Bill,

      I got my PTZ camera controller project working and tested it this weekend. I used your controller library and some code form another project to control a PTZ camera using Pelco-D commands, and bridged the gaps, learning a lot in the process. The PS2 controller is very intuitive to use for camera control and with a generation of gamers, we will never have to look very far for a camera operator.

      The code and a couple of photos are on the Arduino forum at http://forum.arduino.cc/index.php?topic=306453.msg2154210#msg2154210

      Regards,
      Gary G.

    • Gary GrahamNo Gravatar says:

      Photo of my project.

    • iqbalNo Gravatar says:

      please help, it doesn’t work, I used UNO, I have try to debug like the troubleshooting guide said
      the monitor said “Controller found but not accepting commands. see readme.txt to enable debug. Visit http://www.billporter.info for troubleshooting tips
      DualShock Controller Found

    • ashokNo Gravatar says:

      the ps2 controller is not getting recognised in the serial monitor of my arduino v 1.6.3 on mac OS X yosemite

    • Stefan Huus GregersenNo Gravatar says:

      Hello, I’ve been working on making this work, I’ve testet each connector and wired it correctly but it just won’t work. The Controller is able to turn on the “Analog” Light but it still says inside the console “No controller found, check wiring, see readme.txt to enable debug. visit http://www.billporter.info for troubleshooting tips
      Unknown Controller type”
      .. I am posting debug when i make the debug mode work.. atm. I don’t understand why it doesn’t work..

      • OUT:IN
        1:FF 42:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0
        OUT:IN
        1:FF 42:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:FF 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0 0:0
        Controller mode not matched or no controller found
        Expected 0x41 or 0x73, got FF
        No controller found, check wiring, see readme.txt to enable debug. visit http://www.billporter.info for troubleshooting tips
        Unknown Controller type
        Here’s the debug

    • ujjvalNo Gravatar says:

      Hey,
      First of all thankz for this awesome explanation.. I successfully made the above thing but when i add the rf module library to my code the thing stops working. When i upload the sketch with rf library the analog light on the controller goes off but when i press the analog button the light turns on but the buttons dont work and i dont get anything in the serial monitor please help me out of this .. 🙂 😀

      • Stefan Huus GregersenNo Gravatar says:

        Hello!
        I had same problems with my controller and figgure out some cable wiring are not the same if you have problem figguring it out try open your controller sometimes they wrote a small label on the controller PCB where shows you what cables are the right onces hope that would help you 🙂

    • ujjvalNo Gravatar says:

      Hey stefen, thanks for that very quick reply 🙂
      As you told i opened my controller but the wires are not labeled unfortunately can u please send me the wiring diagram u followed to fix that error please?

      And for your information im using
      wired dualshock-2 ps2 controller 🙂

      You can email me on u.kavathia54@gmail.com

    • KiranNo Gravatar says:

      I am trying to use a PS2 as a RC remote controller for a RC plane. the sticks are very sensitive meaning I donot get the complete x and y values when I move the sticks. half way through I start getting 0 or 255 on either side.

      Can we make the joysticks more precise.

Leave a Reply