Home > Arduino, Bluetooth, TKJ Electronics, USB > PS4 controller now supported by the USB Host library

PS4 controller now supported by the USB Host library

I am very pleased to announce that I have now added support for the PS4 controller via Bluetooth. This will allow you to read all the buttons and joysticks with the same API as all the other libraries I have written for the USB Host library.

PS4 controller

To get started you should look at the provided example. It shows how to read the different buttons and joysticks. I still haven’t figured out how to control the light, rumble and read the accelerometer, gyroscope and touchpad, but hopefully I will figure that out soon.

The pairing process for the PS4 controller is a little different than the PS3 controller, as it works like a regular Bluetooth joystick, as you simply make the PS4 controller discoverable by holding down the PS and Share button at the same time. The light will then start to blink rapidly.

After that you should be able to pair with the controller, by creating the PS4BT instance like so:

PS4BT PS4(&Btd, PAIR);

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

The PS4 Bluetooth library actually uses the BTHID library to handle all the Bluetooth communication. This is much more modular and easier to maintain. I will work on making all the Bluetooth classes like that, so it will reduce the footprint of the libraries, but also make it much easier to maintain, as all the L2CAP communication would be handled in only one class.

I still haven’t written a library to use it via USB, but I will do that in the coming weeks, when I have time.

This is all for now. Please leave a comment below if you got any questions and I will answer as quickly as possible.

Update 18. January 2014
A USB version of the library is now also available. It allows you to read the gyroscope, accelerometer and touchpad as well.

Update 22. January 2014
I now also figured out how to read the IMU and touchpad data via Bluetooth. The problem is that the controller does not send out this information via Bluetooth by default.
To enable the full output the code sends out a get feature report. For more information see this excellent Wiki: http://eleccelerator.com/wiki/index.php?title=DualShock_4 by Frank Zhao.

Update 16. February 2014
It is now also possible to control the rumble and light as well. The code was inspired by this Linux driver: https://github.com/chrippa/ds4drv.

Categories: Arduino, Bluetooth, TKJ Electronics, USB Tags:
  1. Uddhav
    January 11th, 2016 at 20:05 | #1

    @Lauszus
    Can you explain these line how they work..
    if (Usb.Init() == -1) {
    Serial.print(F(“\r\nOSC did not start”));
    while (1); // Halt
    }
    Serial.print(F(“\r\nPS4 USB Library Started”));

  2. January 18th, 2016 at 14:51 | #2

    @Uddhav
    Those lines will initialise the USB host shield. I.e. it checks that SPI communication is working with the MAX3421E.

  3. Shawn
    March 6th, 2016 at 07:34 | #3

    I’ve been using this library and the host shield for a long time, and I love it. I normally have been running a PS4 controller wired to the shield because I didn’t have a BT dongle. I finally got one, and am running the example sketch for PS4 BT.

    I’m having issues with the PS4 controller and BT dongle keeping communication open. I get it paired, and it works great only for about 5 seconds. But they stop talking after that 5 seconds or so. When I press the PS button on the controller, they start talking again, for about 5 more seconds… rinse and repeat. I can tell when they are talking because I have the serial monitor open and am holding one of the joysticks to the side to have traffic to pass.

    Here’s what I’ve done:
    I upload the code, keeping the “PS4BT PS4(&Btd, PAIR);” line uncommented and active, and the “PS4BT PS4(&Btd);” line commented out. I pair the controller with the dongle. I switch those 2 lines of code, so now the “PS4BT PS4(&Btd, PAIR);” line is commented out, and the “PS4BT PS4(&Btd);” is active. I reupload the code. I open the serial monitor. I press the PS button and hold a joystick to the right. For a brief period, I get the values of the hats. Then, without me changing the stick position, the PS4 controller light goes out, and the serial monitor stops giving me new values for the stick position.

    I’m really hoping that I just have a user error. Please help :)

  4. March 8th, 2016 at 23:41 | #4

    @Shawn
    Please enable serial debugging and post the output: https://github.com/felis/USB_Host_Shield_2.0#enable-debugging.

  5. Yuji
    March 28th, 2016 at 06:53 | #5

    Hi,I’m Yuji.

    I attach the usb host shield to Arduino uno rev.3 and Intel Edison for Arduino, I try or PS4BT moves.
    Arduino uno was able to move the PS4BT. But Intel was not able to move the PS4BT.

    Intel Edison for Arduino, do you need to do something special process?

    Since English is not good, I’m sorry if was hard to understand.

  6. April 14th, 2016 at 23:08 | #6

    @Yuji
    The Arduino Edison is currently not supported. Please see: https://github.com/felis/USB_Host_Shield_2.0/issues?q=edison+label%3A%22Intel+Edison%22.

  7. Uddhav
    April 30th, 2016 at 05:24 | #7

    @Lauszus
    I have “2 PS4 CONTROLLERS” and 2 bluetooth dongles connected to usb host shield.

    Is it possible to connect ps4 controller to only “one” specific bth dongle…not other one.
    or like having “seperate address” for each ps4 to connect to host shield dongle.

    I’m trying to make two wireless controlled bots with ps4 controller using library example PS4BT.
    So I want each bot to connect to specific controller not other one.

    Please help…Thanks

  8. mitch
    April 30th, 2016 at 06:35 | #8

    I used the USB Host 2.0 library for a month, and loved it. Thank you.
    All of a sudden, I can no longer pair up dongles with the PS3 controllers. I upload the code PS3BT and I start the serial monitor. The shield is detected ok. When I plug the dongle, nothing happens. I tried 5 dongles, from 5 brands, all version 3 or 4.

    When I plug the PS3 controller still , nothing appears in the serial screen. I am desperate. I am using this for controlling an RC tank. It worked so nice for an hour and got un-paired.

    Please help

    CK it out on
    https://youtu.be/OaIb2V0fTnY

  9. Fernando
    May 11th, 2016 at 13:33 | #10

    I’m having the same issue as Shawn..
    @Shawn, did you fix it?

  10. Patrick
    June 25th, 2016 at 01:28 | #11

    Hey there,

    first of all, i really love ur Library. Its real fun to play with my Ps4 Controller.
    I first tested it with USB kabel and everything worked great. After this I tried it with the BT Dongle and unfortunately i got some problems with it.
    I read many of the comments, and i already ordered other Bluetooth Dongles (some of the list of working Dongles), since mine wasn’t on that list.

    But maybe im just a little too stupid pairing it ? 😉
    I followed the instructions: holding down the “share” and “ps4” buttons and the led starts blinking rapidly. After about 30sec it stops blinking and i guess that there was no pairing.

    Here the Serial Output before pairing ( after uploading the scetch:

    S4 Bluetooth Library Started
    Bluetooth Dongle Initialized
    HCI Reset complete
    Write class of device
    Local Bluetooth Address: 00:11:22:98:76:54
    Please enable discovery of your device

    and while /after pairing attempt:

    Gamepad found
    HID device found
    Now just create the instance like so:
    BTHID bthid(&Btd);
    And then press any button on the device
    Connecting to HID device

    Once i also got this Serial Output:

    PS4 Bluetooth Library Started
    Bluetooth Dongle Initialized
    HCI Reset complete
    Write class of device
    Local Bluetooth Address: 00:11:22:98:76:54
    Please enable discovery of your device
    Gamepad found
    HID device found
    Now just create the instance like so:
    BTHID bthid(&Btd);
    And then press any button on the device
    Connecting to HID device
    HCI Command Failed: 0C
    HCI Command Failed: 0C
    HCI Command Failed: 0C
    HCI Command Failed: 0C
    HCI Command Failed: 0C
    HCI Command Failed: 0C
    …….

    I also tried it with the USBHub class by uncommenting it. There it said:

    PS4 Bluetooth Library Started
    Bluetooth Dongle Initialized
    HCI Reset complete
    Write class of device
    Local Bluetooth Address: 00:11:22:98:76:54
    Please enable discovery of your device
    Gamepad found
    HID device found
    Now just create the instance like so:
    BTHID bthid(&Btd);
    And then press any button on the device
    Connecting to HID device
    Connected to HID device

    after this the PS4 controller was lightblue. i followed the instrucions and commented out the Pairing line, and uncommented the “PS4BT PS4(&Btd);” line. Then i uploaded the code.
    After this i got some different behaviours. Once it said ps4 controller is connecting but then cam an error/fail message. In another try it said “No response to HCI Reset
    ” and the Led on the controller turned off.

    Maybe u got some tipps for me? otherwise i hope another Dongle will work .

    Greetings Patrick

  11. August 2nd, 2016 at 12:44 | #12

    hello , i want to send ps4 buttons data from bluetouth dongle to the ps4 by arduino .
    what should i do ? can you help me about this?
    tnx.

  12. Rafal
    September 28th, 2016 at 11:30 | #13

    Hello,
    thanks for building such a robust library.
    I’ve been using it with an Xbox 360 controller and an Arduino and it works really nice. Currently I wanted to go smaller, so I decided to use a Teensy 3.1 with a USB host shield from sparkfun and I ran into problem when trying to connect a PS4 controller using a USB cable. To check if all the connections had been made correctly I ran a USBHIDBootMouse example. When I plug the mouse it’s being recognized and data goes through without a problem. Then I ran PS4USB (I want a wired connection to the controller) and I get stuck at “PS4 USB Library Started”. I plug in the USB cable of the PS4 controller and nothing happens. I tried pushing “share” and “PS” buttons but to no avail (I’ve assume they’re just for BT but was desperate at this point). I’ve ran the debug mode, but no data, other than “PS4 USB Library Started” appears on the serial monitor. When I run the mouse.ino I see additional data, but not when I run and connect a ps4 controller using USB. Do you think you could help me?
    Best regards,

  13. October 10th, 2016 at 20:29 | #14
  14. axebuddy
    October 19th, 2016 at 08:03 | #15

    Hello,
    Many thanks for your work!
    I have some problem with getButtonPress function.

    if (PS4.getButtonPress(LEFT)) {

    getButtonPress is too fast response. I’m released the button but PS4.getButtonPress(LEFT) still return “true” for long time. How can I set configure ?

    Thanks in advance for any advice,
    Axebuddy

  15. October 20th, 2016 at 19:24 | #16

    @axebuddy
    Do you have any delays in your code?

  16. axebuddy
    October 21st, 2016 at 04:12 | #17

    @Kristian Sloth Lauszus
    Thank you for your quick reply.

    } if (PS4.getButtonPress(LEFT)) {
    Serial.print(F(“\r\nLeft”));
    PS4.setLed(Green);
    }
    I’m got about 89 “Left” message for short pressed.
    ps. I’m use arduino DUE.

    Thanks

  17. Rodrigo Alves Nunes
    November 13th, 2016 at 21:34 | #18

    I’m trying to power a DC motor through a H-bridge.
    The answer I want from the program is that when I press the button the engine starts and when I release it stops, but with the following code this does not happen, I press the button and the engine turns on. But when I release the button the engine keeps running. I’m trying some code combinations but I’m not having an answer, can you help me?
    Follow the code I’m using.

    If (PS4.getButtonPress (CROSS))
           {
             AnalogWrite (VeloM4A, 100);
             AnalogWrite (VeloM4B, 0);
           }

  18. Helge Nodland
    December 2nd, 2016 at 15:13 | #19

    Hi Kristian.
    Thank you for this awsome peace of code.

    I would like to enter pairing mode by the use of a “pair button” but as the instance is set outside setup()/loop() I am not sure how to implement this in code.
    can anyone give me a hint on where to start? or if you have time, you could add functionality to the library :)
    Thanks from Norway

  19. December 5th, 2016 at 03:30 | #20

    @axebuddy
    You should use the “getButtonClick” function instead.

    @Rodrigo Alves Nunes
    You probably want to do something like this:

    If (PS4.getButtonPress (CROSS)) {
        analogWrite(VeloM4A, 255);
        analogWrite(VeloM4B, 0);
    } else {
        analogWrite(VeloM4A, 0);
        analogWrite(VeloM4B, 255);
    }

    @Helge Nodland
    It is already supported. Simply call:

    PS4.pair();
Comment pages
1 2 3 4247
  1. No trackbacks yet.