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. HDSPs and Playstation 2 controllers on Arduinos - Hack a Day
  2. How to create a PS2 PCB?
  3. Flaming guitar hero - Hack a Day
  4. Color Fire Hero Preview « The Mind of Bill Porter
  5. Arduino Blog » Blog Archive » FireHero To Warm You Up In Winter
  6. Arduino Playstation 2 Controller Library Troubleshooting Guide « The Mind of Bill Porter
  7. DIY Arduino Intervalometer : DIYfilm
  8. Early Development | hypermediacreator
  9. EasyTransfer Arduino Library « The Mind of Bill Porter
  10. Tesla Hero, An Awesome Million Volt Guitar « The Mind of Bill Porter
  11. Tesla Hero « adafruit industries blog
  12. George the Animatronic Puppet « adafruit industries blog
  13. Documentation, and future projects | Mecharobotics's Blog
  14. PS2 controller working! | Mecharobotics's Blog
  15. PS2 Controller Buttons sent over RFM12 wireless module | Mecharobotics's Blog
  16. dome drive and control | kimncris R2D2 Build
  17. Some fun with servos and a playstation gamepad | Xorsat
  18. Weekend Project: Arduino + Xbee + PS2 controller = 20 channel universal project controller! « adafruit industries blog
  19. ps2 controller to arduino |
  20. neophob.com » Dead simple Arduino Input devices
  21. PlayStation 2 Controller Arduino Library v1.0 – by billporter | xmod
  22. Wireless PS2 Controller | xmod.me
  23. Tapis de danse / Dancemat | Global Physical-Digital Gaming
  24. Murat Çileli | Arduino İle Kolay Elektronik
  25. Murat Çileli | Arduino İle Kolay Elektronik
  26. Budowa analoga od ps2 i jego zasada działania | Blog oVer'a
  27. lungStruck » Playstation 2 Motor Controller
  28. Assignment 7: Electronics Prototyping – Mamma Mia! Look at My Arduino! | Tar's Adventures in HCI+D
  29. Assignment 7: Electronics Prototyping – Mamma Mia! Look at My Arduino! | Tar's Adventures in HCI+D
  30. Usare joystick PS2 sul PC e sulla PS3 con Arduino. | TecnoMente
  31. Use PS2 controller on PC and PS3 with Arduino | TecnoMente
  32. Files for RED, the balancing robot - OzBotzOzBotz
  33. Arduinoween – MW1430 – Es Car Ghost | WSU ENGR101 Fall 2014
  34. Fix Arduino Error ‘wire’ Was Not Declared In This Scope Windows XP, Vista, 7, 8 [Solved]
  35. Fix Arduino Error ‘spi’ Was Not Declared In This Scope Windows XP, Vista, 7, 8 [Solved]
  36. کنترل سروو موتور با آرداینو توسط موس
  37. ArduinoでPS2コントローラを制御する 回路編 | Let's making!Let's making!
  38. I, Robot | gr33nonline
  39. Interfacing PS2 Wireless Controller With Arduino
  40. A Simple Remote-Controlled Arduino Tank | Paul Bleisch
  1. 570 Comments.

    • PeterNo Gravatar says:

      Hi, I need hepl. I have problem. J can nor compiling code from PS2X lib. Becouse:

      C:\Program Files (x86)\Arduino\libraries\PS2X_lib\PS2X_lib.cpp:5:20: fatal error: avr/io.h: No such file or directory
      #include
      compilation terminated.

      I do not know what happen. ( sorry for my eanglish. A never write from website like these)

    • JohannesNo Gravatar says:

      Hi,
      I found your and want to try it. But the Arduino can’t find my controller. I checked the wireing. It should be right. I also tried to change the connection of pin 12 and 13 because I thought Pin 13 is SCK and 12 is MISO. But it doesn’t help.

      I hope you can help me.

      Johannes

      Here are the debug messages:

      OUT:IN Configure
      1:FF 43:FF 0:FF 1:FF 0:FF
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:64 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:64 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:64 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:61 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:61 5A:40 5A:6 5A:60
      OUT:IN
      1:FF 42:63 0:10 0:FE 0:FF 0:64 0:42 0:6 0:60 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 Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:61 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:61 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:64 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:61 5A:40 5A:6 5A:60
      OUT:IN Configure
      1:0 43:0 0:0 1:0 0:0
      OUT:IN Configure
      1:FF 44:63 0:FE 1:FF 3:FF 0:FF 0:FF 0:FF 0:FF
      OUT:IN Configure
      1:FF 43:63 0:10 0:FE 5A:FF 5A:61 5A:40 5A:6 5A:60
      OUT:IN
      1:FF 42:63 0:10 0:FE 0:FF 0:61 0:40 0:6 0:60 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, 0x73 or 0x79, but got 63
      No controller found, check wiring, see readme.txt to enable debug. visit http://www.billporter.info for troubleshooting tips
      Unknown Controller type found

    • FrankNo Gravatar says:

      Hi,
      really cool, but R1, R2, Triangle, Circle and X doesn’t work at me.
      Do you have an Idea?

    • JackNo Gravatar says:

      Hi !

      Firtsly thanks for this website !

      I have connected all pins, but I have the error 1 :

      “No controller found, check wiring, see readme.txt to enable debug. visit http://www.billporter.info for troubleshooting tips
      Unknown Controller type found ”

      have you an idea please ?

      my controller is an official sony controller

      thanks for your time

      • MekaciNo Gravatar says:

        I only got one time the controller is found thing then I always got no controller is found so it was preventing me to use this library.

        I found the solution. It is very simple. Normally we had these line right ?

        if(error == 1) //skip loop if no controller found
        return;

        here make the error == 0
        then

        if ( type == 0 ) { /

        ps2x.read_gamepad(false, vibrate);
        bla bla ( the blow codes )

        here make the type == 1

        Right now, even at serial monitor it says the controller is not found, you can use your written project codes by using the joystick control now.

        The main reason is ” most of us buy these adapted playstation 2 joystics from ebay which means most of them are coming from china. That’s why we always encounter with technical problems. I was bought x3 joystick and only one of it was wroking as intented; other x2 joystics I always got ” no controller found ” error :(. I hope that this solution helps you, thank you.

    • celleNo Gravatar says:

      hello, i have the same problem & errormessage like Johannes. I hope, somebody can help me/us. i have an original sony ps2 controller.

    • PierreNo Gravatar says:

      Hello,

      Can we use the library varspeedservo.

      Have you sample code to provide me please because I try but I can not find.

      This is to control a servo motor with a joystick of the PS2 controller.

      it is here that I do not understand:

      ps2x.read_gamepad(); //read controller
      RXservo.write(map(ps2x.Analog(PSS_RX), 180, 30, 0, 179, true));

      Thanking you in advance

    • VitorNo Gravatar says:

      Hi,thanks for the nice tutorial!
      I am trying to follow it, but I’d like to know if the other Arduino -> Controller outputs hav to be at 3.3V too, or if 5v is fine.

    • arickbroNo Gravatar says:

      can this library work with esp8266 arduino?
      could you explain which part of the code that need to change to make this library work with esp8266 ?

      Regards
      Ari

    • Timmy P. JonesNo Gravatar says:

      I am using a Play Station 2 Wireless Controller with Arduino UNO: I hooked up the wires as advise: I unloaded the PS2 Arduino code. The Receiver light comes on and blinks. I then turned on the PS2 Controller and pressed the start button. The light quits blinking. I open up the Arduino Serial Monitor and I am getting feed back: L3, R3, Start,
      Up, Right, LEFT, DOWN, L1, R1, L2, R2,Triangle
      Circle, Square, X etc… But I am not getting real time response out of the controller as required… Is this a code error ?

    • SammieNo Gravatar says:

      Just want to know if playstation 2 controller is still available?

    • moxasikaNo Gravatar says:

      Please reduce the price of PS2 Controller.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.