Posts

Sony PS3 Controller support added to USB Host library

A fragment of PS3 Bluetooth code

A fragment of PS3 Bluetooth code


I’m quite pleased to announce that ever-popular Sony PS3 game controllers are now supported by current (rev.2.0) USB Host Library. Kristian Lauszus from TKJ Electronics developed a library to interface with Dualshock 3, as well as Move Navigation and Motion controllers via a Bluetooth dongle (only CSR-based ones are supported at the moment). This library has been merged into the main code and I will start maintaining it as soon as my Dualshock 3 arrives.

Sony PS3 controllers are advanced versions of ordinary HID joysticks. They can be interfaced via USB or Bluetooth, the buttons can be read as analog or digital and controller contains rumble motor which can be activated remotely to indicate a hit, for example. The controller also contains a gyroscope and accelerometers so it is possible to control a robot by simply waving and/or rotating the controller.

Kristian got his inspiration from Richard Ibbotson’s articles, as well as works by Tomoyuki Tanaka and others (full list can be found in PS3README file). His Wiki page gives implementation details, communication protocol, differences between controllers, as well as plenty of links to other sources of PS3 controller interfacing knowledge.

Kristian developed this library as a part of his Balancing robot project. The project is very well documented – if you are interested in robots, PID or remote control, please take a look. I also enjoyed watching this video presentation, give it a shot to see balancing robot in action.

Oleg.

65 comments to Sony PS3 Controller support added to USB Host library

  • Zodius

    This library is great! It’s saved me loads of time trying to get my hexapod robot responding to external input. Unfortunately I have an issue that I can’t figure out. I’ve got a CSR Bluetooth dongle visually identical to the one on the Balancing robot and am connecting it to an Arduino Mega ADK. I can then sync a PS3 Sixaxis controller to it and get the lights changing and read most of the inputs. However, I can’t read any of the analog buttons, the controllers status, or the motion sensor values. The same occurs with a different Belkin branded bluetooth dongle, but both work fine on PC when setup with MotioninJoy, able to read everything.

    Anybody got any ideas what I’m doing wrong?

    • Hi,
      I got one bluetooth dongle that has the exact same problem.
      If you uncomment PRINTREPORT in PS3BT.cpp, you can see that the data is not changing. I still don’t know what the problem is. I had the same issue when I used it with the FEZ Devices: http://wiki.tinyclr.com/index.php?title=PS3_Controller with the same dongle.
      I recommend getting a different dongle. Just buy some different ones at ebay and they will properly work. I think is related to if it’s supports EDR or not, but I’m not sure, as it must support V. 2.0+EDR.

      Best Regards
      Lauszus

  • Harry

    The new ps3 library is graet. Thanks!

    The library should work with Arduino UNO?
    Did you use any timer or interrupt in the library? Can I still use timer2 to generate an constant 1 msec interrupt?

    Thanks!

    • Yes it works with all the Arduinos supported by the USB Host Shield.
      No I havn’t used timer2 in the library. I have only used the standard millis() and delay() functions which use timer0.

      Regards
      Lauszus

  • Thud

    Fascinating articles guys, but what I would really like to see is an Xbox 360 Wireless Controller Library along similar lines to the PS3 and Wii libraries available. Is anybody doing any work on this?

    Cheers

  • Roman Foltyn

    Hallo,
    USB is working correctly with the Shield SPEEDLINK PS3 Controller Type SL-4445-BK.
    Once connected via USB cable Controller in August and then reports the number of the program is part of BT Host to Controller. then when you insert the BT dongle everything works great.

    But here I have a big problem … Original Sony PS3 Controller Model CECHZC2E b1 purchased today is not recognized by USB Shielt and I can not enter the host address to it .. I thought of is defective and replaced it with another … The same problem.

    I tried to apply pressure all the keys on the Serial Monitor controller is there.

    Is Genuine Controller Sony need to do something extra?

    Please advice, I will not give you the same advice.

    I send greetings

    RomanF

  • Matej

    How do i pair PS3 controller with BT dongle?

    Thanx ๐Ÿ˜‰

  • Matej

    This is what i get if I press button TRIANGLE once and then CROSS:

    PS3 Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:15:83:3D:0A:57
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: PLAYSTATION(R)3 Controller
    Connected to Device: 04:76:6E:DA:DD:8C
    Scan Disabled
    Your dongle may not support reading the analog buttons, sensors and status
    Your HCI Version is: 1
    But should be at least 3
    This means that it doesn’t support Bluetooth Version 2.0+EDR
    HID Control Incoming Connection Request
    HID Control Configuration Request
    Wait For Incoming Connection Request
    HID Control Successfully Configured
    HID Interrupt Incoming Connection Request
    HID Interrupt Configuration Request
    HID Interrupt Successfully Configured
    Dualshock 3 Controller Enabled
    PS3 Controller – Traingle
    PS3 Controller – Traingle
    PS3 Controller – Traingle
    PS3 Controller – Traingle
    PS3 Controller – Traingle
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross
    PS3 Controller – Cross

    I have tried this with 3 original Dualshock3 controllers on Arduino Mega and Leonardo.

  • Matej

    What does this means “Your HCI Version is: 1”.
    Does this applies to Bluetooth dongle or Dualshock3 controller?

    Thankyou.

  • mej

    Hi Lauszus,

    I’ve got a SparkFun USB Host Shield and a MadCatz PS3 controller. I downloaded the USB Host Library and ran the example sketch PS3USB (after changing the values for VID and PID in PS3USB.h so that they matched my controller’s) and the serial monitor is continuously scrolling and the parameter values don’t change regardless of whether or not I’m pressing any buttons on my controller.

    Are there more values that I need to change in the header file? Or is this how the sketch should run? I’m a bit new to this so any help would be appreciated, thanks.

    • mej

      I’ve since gotten a usb cable for my official sony PS3 controller. I changed the VID and PID values once again and the sketch shows

      PS3 USB Library Started
      Dualshock 3 Controller Connected
      Bluetooth Address was set to: 00:00:00:00:00:00

      and nothing else; it doesn’t respond to any button presses/holds.

      • Try to connect an external power supply to the Arduino.
        The Sparkfun shield rely on a VIN that it’s greater than 5V since it got an onboard 5V regulator connected to VIN on the Arduino.

  • gio

    hi, i have a problem to pair the dongle with the control( i have 2 dualshocks and 1 sixaxis only detects the sixaxis i dont know why) when i started the serial monitor detect the dongle and shows the address, then i plugged the control and showed dualshock 3 detected and show the same address of the dongle, plug the dongle again and push the ps button but it didnยดt pair only flash, what can be the problem? thanks a lot

  • gio

    THANKS FOR ANSWERED, I DONT KNOW VERY MUCH OF THIS,WHAT DO YOU WANT TO LOOK? I DONT UNDERSTAND, COULD YOU EXPLAIN ME WITH MORE DETAILS, PLEASE, I NEED TO CHANGE SOMETHING IN THE SKETCH? THANKS

    • Please send me the output from the serial monitor after you have done the following: plug in the dongle and then unplug it again. Then plug in your PS3 controller.

  • gio

    Ps3 bluetooth library started
    Bluetooth dongle initialized
    Hci reset complete
    Write class of device
    Local bluetooth address:00:1f:81:00:02:50
    Wait for incoming connection request
    Dualshock 3 controller conected
    Bluetooth adress was set to :00:1f:81:00:02:50
    Bluetooth dongle initialized
    No response to hci reset
    Hci reset complete
    Write class of device
    Local bluetooth adress: 00:1f:81:00:02:50
    Wait for incoming conection request
    Incoming connection request

    Thats all and no pair

    • The paring is working fine. It is happening when you plug in your PS3 controller and the following line gets printed:

      Bluetooth adress was set to :00:1f:81:00:02:50

      It’s a problem with your Bluetooth dongle. You will have to try a different one!

  • gio

    Ok i will, the dongle needs something particular? Thanks

  • nero

    Hello , I would like to thank you for the library .
    Iโ€™m using it to control a motor of a rc Car using the ps3 controller by Bluetooth and arduino .
    Im now in the programming process and I want to use switches or else the programe will be too long
    so what should I edit to me code so that the the switch happens when I press a button
    switch (when button pressed) {
    case โ€˜PS3.getButtonPress(CROSS)โ€™:
    digitalWrite(BLP, HIGH);
    break;
    case โ€˜PS3.getButtonPress(SQUARE)โ€™:
    digitalWrite(BLP, LOW);
    break;
    }

  • nemer

    Im trying to use the ps3bt with the arduino adk but its not working
    the serial port gives me the following

    OSC did not start

  • drewfire

    How can I detect the PS3 has lost communications in my code? If this happens with the motors on, I need to turn them off. Thanks.

    • Simply check if PS3.PS3Connected is false.

      • drewfire

        It looks like that information is not resetting, when an unexpected drop occurs.

      • drewfire

        It looks like an unexpected disconnect is detected by HCI Event after about 20 seconds. I still can’t react to this in my sketch. I would like to turn the motors off instantly; 20 seconds is way too long. Can you please help me?

        • Are you pressing the reset button on the PS3 controller? In a real case scenario the PS3 controller disconnects when it get low on battery etc.
          What you could do it reset a timer every time data is received here: https://github.com/felis/USB_Host_Shield_2.0/blob/8e6ab3f3ae6fc86a18c78c010b6d9b351b488a4d/PS3BT.cpp#L364. If there is a stop in the data you would know if the controller is disconnected.

          • drewfire

            I have this hooked up to my CMM (Coordinate Measuring Machine) at work. Some how it lost communications and the arm keep going. I had to run over to the wired controller to stop it before it crashed. Thanks for the response, I’ll try your advice and let you know.

          • drewfire

            I hate to bug; I see your a busy man. The timer method is inconsistent. Though I can detect a stop in data and react quickly in my sketch, which is great, it also triggers too often when the connection is good. Is there another way to check if the connection is good.

          • That is why I wanted to use a timer instead of always setting a flag if there is no data – the Arduino might be polling data faster than the PS3 controller actually sends it.

            Instead you could reset a timer every time new data is received:

            timer = millis();

            And then just set a flag if the timer gets above a certain value.

          • drewfire

            Thanks for your help. I have it turn the motors off after each 200 millis delay. If it detects the PS3 button being pressed after that, it turns the motors right back on. If the 200 millis delay is detected 5 times in a row, it does a proper disconnect of the PS3.

  • drewfire

    It looks like that information is not resetting when an unexpected drop occurs.

  • Kyle

    Hello,
    Im using a arduino leonardo and everytime i try to upload the PS3BT sketch I get the error that it is too large. Is there anyway to cut it down that way i can use it. My dongles work and everything i just can’t run the PS3BT.
    Thanks

  • Chris

    Hello, I’m using a USB Host Shield full size w/ Arduino Uno and confirmed compatible Bluetooth dongle.
    When I start the serial monitor on several different sketches the library starts, but no further text is generated. For the PS3BT example it says
    PS3 Bluetooth Library Started
    No further information is given.
    Any idea why this is happening?

  • Tom Frederick

    I am experiencing difficulty similar to drewfire’s comments from November 6th, 2013. In my case, I believe that the drops are being caused by heavy wifi traffic in the area. I am going to implement the timing strategy that was discussed previously, but I would greatly prefer to minimize the number of hangups. I have read that BT 3.0 will run outside of the ISM band. Will your libraries function with BT 3.0? Do you have any suggestions as to how I can help minimize the occurrence of these incidents.

  • Darren

    Is there a way to determine the MAC address of a PS3 connector from within a sketch?
    I see that it is printed as debug when establishing a connection, but I’m not seeing a method to access that data.

    I was thinking about an application using two controllers, and was hoping to ensure which controller gets assigned to a “PS3Left” and “PS3Right”. Using the MAC address seemed like the most natural way to accomplish this.

  • Darren

    Thanks
    I did see that – but with it only being the “last” device,
    I was missing how I would be able to make an association with multiple connected devices.

    Then it hit me:
    That I could leverage this, and write the functionality that I need in onPSInit():
    PS3.attachOnInit(onPSInit);

    Tested with this, and looks like it works. Just need to do something useful now!
    void onPSInit()
    {
    Serial.print(“\r\nBT Address of Last connected Device: “);
    for(int8_t i = 5; i > 0; i–) {
    Serial.print(Btd.disc_bdaddr[i], HEX);
    Serial.print(“:”);
    }
    Serial.println(Btd.disc_bdaddr[0], HEX);
    }

  • ian

    Hi there!
    thanks so much for the library. It appears that there is a problem with my dongle. I bought three dongles that are identical to the approved dongles, but with each one i get the error:

    PS3 Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Local Bluetooth Address: 00:15:83:3D:0A:57
    Wait For Incoming Connection Request
    Incoming Connection Request
    Remote Name: PLAYSTATION(R)3 Controller
    Connected to Device: 04:76:6E:DA:05:D4
    Your dongle may not support reading the analog buttons, sensors and status
    Your HCI Version is: 2
    But should be at least 3
    This means that it doesn’t support Bluetooth Version 2.0+EDR
    HID Control Incoming Connection Request
    HID Control Configuration Request
    HID Control Successfully Configured
    HID Interrupt Incoming Connection Request
    HID Interrupt Configuration Request
    HID Interrupt Successfully Configured
    Wait For Incoming Connection Request
    Dualshock 3 Controller Enabled

    the main concern for me is the line that says “your HCI version is 2, but should be at least 3”.

    All my buttons work except l2 and r2, the analog triggers. these will be crucial to the robot i am building for school, please help!

  • khair

    Hi there!
    i have some problems when i use the PS3BT.im new to arduino and the error is
    PS3BT:14: error: ‘USB’ does not name a type

  • akshay

    Hey,
    I am using arduino ADK to control servo motors. What are the changes are to be made as compared to interfacing ps3 with uno….please reply me as earlier as possible

  • Sadat007

    Hi, Why USB shield and USB BT Dongle????????? No way to connect PS3 Controller direct to BT module???? for any initial setup of PS3 Cont. use PC??? may a total stupid idea by me!!!
    Thanks! Good work but to complicated..

  • Hello,
    Thanks for the code.Iยดd like to use 2 ps3 navigation controller instead of one.
    Iยดve got one working but the other one can not paired.
    Do you have any idea in how can i use both?
    It would be just like the ps3 regular controller with 2 joystyck,bt this is a navigation with one in each controller.

    Thanks in advance.
    regards
    carlos

  • BON SALIDO

    hi

    Need help,library works with my ps3 controller only issue was it was only working when my arduino board was plug on usb port of PC or laptop.
    Im trying to make it stand alone and it was not working when arduino was powered by external power supply.

    regards,

    BON