Posts

PS4 controller support for the USB Host library

The PS4 controller is now also supported via Bluetooth. It uses the same API as the other libraries I have written, so if you have used them before, then you should be quite familiar with it.

The example code is available at Github: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS4BT/PS4BT.ino.

For more information take a look at my blog post: http://blog.tkjelectronics.dk/2014/01/ps4-controller-now-supported-by-the-usb-host-library/. This explains how to pair with the controller and have some background information as well.

You should also check out the readme which will always have the newest information available.

That is all for now. Hopefully this will be useful to anybody out there that wants to use the PS4 controller with the library.

Update 18. January 2014
A USB version of the library is now also available.

50 comments to PS4 controller support for the USB Host library

  • Todd DeWitt

    Is the code capable of transmitting/receiving audio? If so, how is this done? I would like to utilize the headphones and microphone in a project.

    Also, can this be used with the BT HID Keyboard? Basically, two BT devices at the same time. If possible, how is that done?

    I’m currently using the previous code version and the BT dongles suggested from Amazon.com.

    Thanks!!

  • Todd DeWitt

    I received the below errors when attempting to send the PS4BT to an Arduino 2560. Thoughts?


    PS4BT.cpp: In function 'void setup()':
    PS4BT:32: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:35: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT.cpp: In function 'void loop()':
    PS4BT:42: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:44: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:46: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:48: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:53: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:55: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:59: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:64: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:66: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:68: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:70: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:73: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:75: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:77: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:79: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:82: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:84: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:86: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:88: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:91: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:93: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness
    PS4BT:95: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away constness

  • Todd DeWitt

    I also ran into this with the PS3Multi compiling the program.


    PS3Multi.cpp: In function 'void loop()':
    PS3Multi:118: error: 'class PS3BT' has no member named 'printStatusString'
    PS3Multi.cpp: In function 'void onInit()':
    PS3Multi:141: error: 'LEDEnum' was not declared in this scope

    • What version of the Arduino IDE do you use? I can not reproduce that. Please update to the newest available – at the moment it 1.5.5.

      Make sure you have updated the library and not just the examples. Those errors are caused by a change I did a few weeks back.

  • Todd DeWitt

    Looks like it was the IDE version issue. Thanks for the help! Works like a charm!

  • Matej

    I’m using your library for some time now and it is great!
    I have only one problem and that is the Controller Bluetooth range. Do you know any solution to this problem? Thank you.

  • mczemek

    I have an odd problem that appears to be related to the USB Host Software Library.

    I have a UNO, Seeed Motor Driver Shield and the USB Host Shield. The Seeed Motor Driver Shield uses pins P8,P9,P10,P11,P12 and P13 so I moved P9 and P10 to P7 and P6 on the USB Host Shield and updated the USBCore.H file as follows:

    #else
    typedef MAX3421e MAX3421E; // Official Arduinos (UNO, Duemilanove, Mega, 2560, Leonardo etc.) or Teensy 2.0 and 3.0
    #endif

    What I am seeing is random pulses on P11,P12,P13 which the Motor Driver Shield uses for direction. It only occurs when the USB Host software is active. P8 is clean. ie. it starts after this line in the sketch runs.

    if (Usb.Init() == -1)
    {
    if (debug || debug1) Serial.print(F(“\r\nOSC did not start”));
    while (1); //halt
    }
    if (debug || debug1) Serial.print(F(“\r\nPS3 Bluetooth Library Started”));

    The motor drive software and USB software work separately so it does not appear to be a hardware problem.

    Is the library doing anything with these pins? Any suggestions?

    McZ

  • mczemek

    Matej, LM Technologies has a long range BlueTooth USB Adapter, LM-540, that has a 1000m range when used in pairs. I have not tried them but I am trying to get my hands on a pair for testing the range and with the PS-3 controller.

    http://lm-technologies.com/wireless-adapters/lm540-bluetooth-usb-adapters/

    McZ

    • Darren

      Did you ever have luck using a LM-540 with the USB Host Shield?

      If it works, I’d consider giving it a try for some added range for a PS3 controller.

  • W11cE

    Hi. Im having problems with DS4 pairing. I flash teensy with “PS4BT” example, connect BT adapter and open serial monitor. This shows up:

    PS4 Bluetooth Library Started
    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Write class of device
    Local Bluetooth Address: 00:1A:7D:DA:71:13
    Please enable discovery of your device

    I press PS and share buttons on DS4 and it starts blinking. This shows up:

    Gamepad found
    HID device found
    Now just create the instance like so:
    BTHID hid(&Btd);
    And then press any button on the device
    Connecting to HID device
    Connection Failed: 04
    Please enable discovery of your device
    Couldn’t find HID device
    Wait For Incoming Connection Request

    DS4 keeps still blinking. I comment “PS4BT PS4(&Btd, PAIR);” and uncomment “PS4BT PS4(&Btd);” and flash teensy with this. BT dongle cant find the controller. I quess the pairing didnt work.

    Am i doing something wrong here or is this bug ?
    Im using teensy 3.1 and tested with PS3BT that the dongle works.

  • W11cE

    Ah, i misunderstood the pairing. DS4 never went to pairing process. Imo, the pairing isnt enough crearly described.
    The share button must be pressed and held first. Then PS button. Both must be held down for approx 5 sec. DS4 led will go to “double” blink state. I was mixing this with the regular blinking it does when its searching previously paired device.

    Works now, thanks.

  • Thank you for your feedback. I just updated the readme at Github: https://github.com/felis/USB_Host_Shield_2.0/commit/2b31f96bcd278711846d33006edab7ec83639ea5. Please tell me if it’s still not clear enough.

  • Nique

    Hi,
    What do you think, will your code and the usb host shield be a good point to start to connect an Thrustmaster Hotas Warthog Joystick? I want to use it with a teensy 3.1 to generate ppm output to finally control a rc airplane. The Warthog is somehow to expencive to try’n’error. Hopefully someone can provide some hope, thanks

  • Synapsido

    Is possible use the Bluetooth module BTM-5 for connect PS3 and PS4 controllers?
    You have some examples for wiring?

  • langwan

    put Bluetooth Dongle tty no this message

    Bluetooth Dongle Initialized
    No response to HCI Reset
    HCI Reset complete
    Write class of device
    Local Bluetooth Address: 00:1A:7D:DA:71:13
    Please enable discovery of your device

    “PS4 Bluetooth Library Started” only

  • RAVI

    Sir When I am Using this library to connect my PS4 controller with Arduino Mega ADK using a bluetooth dongle i am getting the reqired data but along with continuous error messages like 1.HCI Command Failed: 01 which is creating a lot of lag in my actual data.Please Help Immediately..I am using a Sony PS4 dualshock PS4 controller.

  • José Méndez

    I have problems to compile the code:

    Arduino:1.6.0 (Windows 8), Placa:”Arduino Uno”

    C:\Program Files (x86)\Arduino\libraries\USB_Host_Shield_2.0-master\BTD.cpp: In member function ‘void BTD::setBdaddr(uint8_t*)’:
    C:\Program Files (x86)\Arduino\libraries\USB_Host_Shield_2.0-master\BTD.cpp:1346:66: error: ‘bmREQ_HID_OUT’ was not declared in this scope
    pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
    ^
    C:\Program Files (x86)\Arduino\libraries\USB_Host_Shield_2.0-master\BTD.cpp: In member function ‘void BTD::setMoveBdaddr(uint8_t*)’:
    C:\Program Files (x86)\Arduino\libraries\USB_Host_Shield_2.0-master\BTD.cpp:1362:66: error: ‘bmREQ_HID_OUT’ was not declared in this scope
    pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
    ^
    Error de compilación

    This report would have more information with
    “Show verbose output during compilation”
    activala desde Archivo > Preferencias

  • bryan

    whenever I try to run example sketches for the usb host library, there is an error. I have tried it on two different arduino IDE versions 1.0.6 and 1.6.1 to see if that was the problem. this is the error message that returns when I try and verify the code

    /Users/bryanmiller/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/BTD.cpp: In member function ‘void BTD::setBdaddr(uint8_t*)’:
    /Users/bryanmiller/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/BTD.cpp:1346: error: ‘bmREQ_HID_OUT’ was not declared in this scope
    /Users/bryanmiller/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/BTD.cpp: In member function ‘void BTD::setMoveBdaddr(uint8_t*)’:
    /Users/bryanmiller/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/BTD.cpp:1362: error: ‘bmREQ_HID_OUT’ was not declared in this scope

  • mits

    hey i have the same problem in a deferent project

    C:\Users\Mitsatsos13\workspace\Documents\Arduino\libraries\USBHostShield2\BTD.cpp: In member function ‘void BTD::setBdaddr(uint8_t*)’:
    C:\Users\Mitsatsos13\workspace\Documents\Arduino\libraries\USBHostShield2\BTD.cpp:1346:66: error: ‘bmREQ_HID_OUT’ was not declared in this scope
    pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
    ^
    C:\Users\Mitsatsos13\workspace\Documents\Arduino\libraries\USBHostShield2\BTD.cpp: In member function ‘void BTD::setMoveBdaddr(uint8_t*)’:
    C:\Users\Mitsatsos13\workspace\Documents\Arduino\libraries\USBHostShield2\BTD.cpp:1362:66: error: ‘bmREQ_HID_OUT’ was not declared in this scope
    pUsb->ctrlReq(bAddress, epInfo[BTD_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
    ^
    Error compiling.

    how to fix this error is for different project.
    Ty For your time

  • Do you know if anyone has ported your USB Host library to Cortex M microcontroller (such as the STM32F4)?

  • Tom Frederick

    I am trying to pair a PS4 controller with an Arduino UNO with Host Shield and BT dongle. I know that the dongle is supported by the libraries. I am able to use the code as it sits to pair over the air with the Share button, but I was wondering if pairing via a USB cable was possible in the current framework? I am interested in configuring the code to behave like the PS3 library did. Have the option to pair over USB or connect to any device that has been paired.

    Thank you for all of your work on this great library.

    • No that is currently not support, but if you know of any sources that does this, just let me know and I will look into it.

  • Chang

    Good day Lauszus,

    Thank you for publishing the dualshock 4 support.

    I have successfully connected my DS4 to my arduino. But I am bothered that I need to open up the serial monitor so the dongle would go into into pairing mode. Is there someway to override this so the dongle would immediately go into pairing mode without opening the serial monitor?

    Sincerely,
    Chang

    • I guess you are using a Leonardo or some other chip with built in USB functionality.

      Simply comment out this line:

      while (!Serial);

      • Chang

        Hi Lauszus,

        I was only able to test this recently and it worked. Thank you again. ^_^

        And yes, I am using a Leonardo.

        Sincerely,
        Chang

  • Mike

    Hi, I am using the PS4 Bluetooth connection and it works great. I have one problem though. I am changing the LED colors on the controller and it works fine until i have set the colors approx 40 times, then the controller disconnects. It does this every time.
    I have tried different manufacturers dongles, but it’s just the same. Any thoughts?

  • Jerry

    Yeah I had to find out the hard way about needing to comment out that: while(!Serial); line on my Pro-Micro (Same as a Leonardo but in the footprint of a Pro-Mini)

  • Jerry

    @Mike

    It would probably be best if you posted the code, so people can see if anything in there is causing such behavior.

  • Demian

    hi all
    i need a little help, I spent hours trying to understand and find solution and read all yours comment on similar topic but I can not find a solution. So here is the thing.
    I would like to control the Arduino using the Chinese (USBWiFiGamepad for Pc + receiver) that I have. These are the thing which i done so far to check where there is a malfunction.
    AGAIN PROBLEM: When i connect a (reciever from USBWiFiGamepad) the serial monitory is just the start

    ++++++Only for better explanation, these are things I’ve done +++++++++++

    1. Arduino + USBHost + USBMouse = All Ok
    2. Arduino + USBHost + USBWiFiMouse = All Ok
    3. Arduino + USBHost + USBGamepad = All Ok ( library “UsbHidJoystick”)
    4. Arduino + USBHost + USBWiFiGamepad = Problem ( on Seral monitor is just “Start”)
    5. Arduino + USBHost + USBWiFiGamepad = Problem ( on Seral monitor is just “Start”) (With extertnal Suply = same problem)
    6. My Computer + USBWiFiGamepad = All Ok
    7. I replace PID and VID numbers with MATCAZ in library = Problem (on Seral monitor is just “Start”)
    8. I’ve tried other literature like “PS3” “PS4”
    “XBOXRECV” is just “Start”

    ++++++Only for better explanation, these are things I’ve done +++++++++++

    This is my report:

    ++USB DESC+++++++++++++++++++++++++++++++++
    Start
    01

    Device descriptor:
    Descriptor Length: 12
    Descriptor type: 01
    USB version: 0110
    Device class: 00
    Device Subclass: 00
    Device Protocol: 00
    Max.packet size: 08
    Vendor ID: 2563
    Product ID: 0523
    Revision ID: 0200
    Mfg.string index: 01
    Prod.string index: 02
    Serial number index: 00
    Number of conf.: 01

    Configuration descriptor:
    Total length: 0022
    Num.intf: 01
    Conf.value: 01
    Conf.string: 00
    Attr.: 80
    Max.pwr: AF

    Interface descriptor:
    Intf.number: 00
    Alt.: 00
    Endpoints: 01
    Intf. Class: 03
    Intf. Subclass: 00
    Intf. Protocol: 00
    Intf.string: 00
    Unknown descriptor:
    Length: 09
    Type: 21
    Contents: 100100012263000705

    Endpoint descriptor:
    Endpoint address: 83
    Attr.: 03
    Max.pkt size: 0008
    Polling interval: 08

    Addr:1(0.0.1)

    +++USBHID DESC+++++++++++++++++++++++++++++++++
    Start
    0000: 05 01 09 04 A1 01 A1 02 75 08 95 04 15 00 26 FF
    0010: 00 35 00 46 FF 00 09 32 09 35 09 30 09 31 81 02
    0020: 75 04 95 01 25 07 46 3B 01 65 14 09 39 81 42 65
    0030: 00 75 01 95 0C 25 01 45 01 05 09 19 01 29 0C 81
    0040: 02 06 00 FF 75 01 95 08 25 01 45 01 09 01 81 02
    0050: C0 A1 02 75 08 95 04 46 FF 00 26 FF 00 09 02 91
    0060: 02 C0 C0
    Usage Page Gen Desktop Ctrls(01)
    Usage Game Pad
    Collection Application
    Collection Logical
    Report Size(08)
    Report Count(04)
    Logical Min(00)
    Logical Max(FF00)
    Physical Min(00)
    Physical Max(FF00)
    Usage Z
    Usage Rz
    Usage X
    Usage Y
    Input(00000010)
    Report Size(04)
    Report Count(01)
    Logical Max(07)
    Physical Max(3B01)
    Unit(14)
    Usage Hat Switch
    Input(01000010)
    Unit(00)
    Report Size(01)
    Report Count(0C)
    Logical Max(01)
    Physical Max(01)
    Usage Page Button(09)
    Usage Min(01)
    Usage Max(0C)
    Input(00000010)
    Usage Page Undef(00)
    Report Size(01)
    Report Count(08)
    Logical Max(01)
    Physical Max(01)
    Usage
    Input(00000010)
    End Collection
    Collection Logical
    Report Size(08)
    Report Count(04)
    Physical Max(FF00)
    Logical Max(FF00)
    Usage
    Output(00000010)
    End Collection
    End Collection

    ++++++++++++++++++++++++++++++++++++++++++++++++++
    I would ask if you could direct me and told me what to do next because i can not find a solution
    Sory for my English
    Thanks in advance