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.

    • Eric says:

      I’m also getting the ‘No Controller Found’ error in Version 1.2 with the same board and wiring I was using that worked correctly with Version 1.0. I’m using the EL Sequencer board from SparkFun Electronics (http://www.sparkfun.com/commerce/product_info.php?products_id=9203), which is a 3.3V, LilyPad-compatible board. I have a 6″ flying-lead harness that goes from the EL Sequencer to the front-controller ports I’ve ripped out of an original PlayStation; everything after that is a stock SCPH-10010 cable and controller.

      I’ve gotten it to work by increasing the CTRL_CLK definition in PS2X_lib.h to 19. I’m guessing that it was originally decreased to 3 in version 1.2 to fix “Was running at 50kHz, not the required 500kHz.” as reported in the changelog. So it’s clocking slower, but it works.

    • BillNo Gravatar says:

      Hmm, I had a feeling that the arudino delay-us function might have an issue with such a low value on 8 mhz AVRs. My arduino is 16mhz and works fine at 3us delay.

      Thanks for posting, I’m going to add a auto-increment clock delay code segment to the init function, just like it auto increases sentence delay if it’s not talking. That should cover the clock speed issue.

    • BillNo Gravatar says:

      Well, I changed the clock back to 50kHz. Everyone having issues (Bob) download and install the new library v1.3 and see if it works then.

    • BobNo Gravatar says:

      I have uploaded the latest version to the Arduino, checked and reject wiring. I got the error “Controller connected but not receiving commands” once and every other time it was “No controller connected”. Frustrating but definetly a learning process.

    • BillNo Gravatar says:

      Bob,

      The fact that you got the “controller connected” error leads me to believe it’s a wiring or power issue; because for a split second, it worked. The only way it will return that error is if it successfully talked to and heard back from the controller.

      The only other thing I can think of is the clock is still too fast for you. If you open up the PS2x.h file, in the first few lines you will find “#define CTRL_CLK 20 ” try increasing that number to see if it helps.

    • BobNo Gravatar says:

      Thanks Bill, I toyed around with CTRL_CLK and it worked when I changed it to 500. Albeit does not detect the controller on every reset of the arduino it’s still great progress. One question about the button serial.print functionality. Why does the Square button display “Square just released” when pressed, X button displays “X just changed”, and both Select and Start buttons displays “being held”. Other than that thanks so much for taking the time to guide my newbie electronics mind into solving the problems I was having.

    • BillNo Gravatar says:

      Look through the example sketch. It’s demonstrating that my library has many different functions. Depending on your project, you may want the buttons to behave differently. Maybe you only want code to run if a button was just pressed, or just released, or being held!

    • BillNo Gravatar says:

      Hey Bob,

      I just thought of something. My example uses pin 13 as the clock pin, but some arduinos have a LED tied to pin 13, and that may be messing with the controller. Try moving the controller’s clock line to another pin, and changing the pin number in the config_gamepad() function.

    • stormNo Gravatar says:

      Regarding 50khz/500khz CTRL_CLK:
      your example ran like a charm, but in my project I had problems with interrupts happening during read_gamepad(). Analog stick values just jumped to 128 randomly. Setting clock to 500khz (CTRL_CLK = 2) fixed this completely + works great now. Thanks a lot for this!

    • BobNo Gravatar says:

      Hi Bill,
      I did change CLK line from pin 13 to 9 and it worked great. I had noticed the LED tied to PIN 13 was constantly going HIGH/LOW so this resolved that issue. It is still not constantly detecting the controller so I can only assume I may need to try a different resistor. On another note, I am trying to program a VB.NET app to read the data from the arduino through the virtual com. I am using Firmata.dll as the intermediary to create the connection but I’m not sure how to read the data. I was wondering if you have a reference or familiarity with this? Thanks

    • Kyler says:

      Here’s something interesting I noticed – the controller won’t be detected if analog mode is already on. Caused a lot of head-scratching!

    • BillNo Gravatar says:

      Hmm, I thought I considered all states in the initial check, but I’ll have to go back and see if I implemented it right. If there is a bug, I’ll fix it in the next release. Thanks for the report!

    • grehe says:

      Nice. I tested the code (v1.3) and it returns an error level2 “controller found but can’t communicate”. I then removed the error check and it works nicely. So this lib could work on psx as well.

    • BillNo Gravatar says:

      Grehe, i’m having a hard time understanding you. Are you telling me that you were trying my lib with a PS1 controller, but the error check is wrong? OR did this happen with a PS2 controller?

    • EricNo Gravatar says:

      Bill,
      I changed a few minor things (button definitions, ‘analog’ mode check) in your code and I’d like to recontribute my changes. Is there an e-mail address where I can send my zip file?

Leave a Reply