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. Chris chandra
    August 8th, 2017 at 12:32 | #1

    hi kristian

    I am using the ps4 Bluetooth to connect my Arduino mega and it works.I was mainly trying to find out which pins are used for the L2 and R2 and the left thump stick.

    thanks Chris

    • Sammy
      January 14th, 2014 at 02:50 | #2

      Great! Was exactly looking for this. Thanks for sharing.

    • January 14th, 2014 at 22:48 | #3

      @Sammy
      You are welcome! It is always nice to see that people find it useful πŸ™‚

    • Raggy
      January 19th, 2014 at 20:18 | #4

      Amazing! I don’t have arduino, but do use PICAXE. Got any tips for connections? i.e. what do I need to serial out to connect? Will serial out and in work?
      Thanks a bunch

    • January 19th, 2014 at 20:30 | #5

      @Raggy
      You will need the USB Host Shield: http://shop.tkjelectronics.dk/product_info.php?products_id=43 for it to work, but for now PICAXE is not supported by the library.

      Anyway they communicate via SPI. The relevant code is in the following file: https://github.com/felis/USB_Host_Shield_2.0/blob/master/usbhost.h.

    • Matt
      January 20th, 2014 at 09:40 | #6

      I cant get a working dongle?!? Ive tried 3 of the cheap half mooned ebay ones and no luck. Any help?

    • January 20th, 2014 at 12:53 | #7

      @Matt
      What errors are you getting when you plug them in?
      There is a list of working dongles here: https://github.com/felis/USB_Host_Shield_2.0/wiki/Bluetooth-dongles.

    • Jake
      February 1st, 2014 at 04:31 | #8

      Hey ive been trying to use this library with a USB host sheild I got online but when I run the PS3BT script my serial monitor just returns random symbols. Has any one else experienced this issue and found a way to resolve it?

    • February 2nd, 2014 at 10:21 | #9

      @Jake
      You need to set the baudrate in the serial monitor to 115200. See the dropdown menu in the bottom right.

    • Azim
      February 23rd, 2014 at 14:41 | #10

      Hi,

      I have purchased the USB Shield and cant wait to get on with it, I really appreciate your efforts and this great work.

      I am currently planning to build a robot and was doing some research on interfacing XBOX 360 controller via BT and came across your great site, I will be going through the information provided should I require any assistance will be in touch.

      Thanks

      Azim

    • February 23rd, 2014 at 16:31 | #11

      @Azim
      Glad to hear you like my work πŸ™‚ Note that the Xbox 360 wireless controller needs a special wireless receiver and is NOT using Bluetooth.
      Please see: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/.

    • Joe M
      April 13th, 2014 at 21:39 | #12

      Hi guys!

      (First of all, sorry for my english level, I’m not an native english speaker…)

      So, I’m a newbie for Arduino stuff, but I’d like to start a project whit it. I want to control a very simple illumination system, you know turn on/off some lights maybe a dimmer.

      I’ve read this could be done by an Android device, but if it’s possible, i’d like to control it whit a PS controller, I dont care if PS3 or PS4.

      Does anybody think it could be possible to do whitout the USB host shield? because if I do that I’ll do it whit a BT shield and I’ll use Arduino UNO.

      Hope anyone could help me!

      Thanks for reading!

    • April 13th, 2014 at 23:17 | #13

      @Joe M
      No you can’t use a normal Bluetooth shield, as it doesn’t support Bluetooth HID. Also the PS3 controller behaves a bit different, as you need to send a special command to it before it starts sending out data.
      If you want to know more about it, then see the PS3 wiki I wrote: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.

    • Joe M
      May 9th, 2014 at 06:03 | #14

      @Lauszus

      Hi Lauszus! Thank you for reply!

      I’ve already bought the usb shield and BT dongle, so I could run the example!

      Then I got a bunch of doubts XD. Let me explain you and maybe (I hope) you can help me.

      The thing is, I want to use a couple of outputs to control a little DC motor, but if you could tell me how to turn on a led it’ll be more than useful!

      So, it’s posible to do? And if yes…. how can i do it?

      Thank you again, take care πŸ™‚

    • May 9th, 2014 at 11:51 | #15

      @Joe M
      It’s really simple πŸ™‚

      Here is some example code:

      if (PS4.getButtonPress(CROSS))
         digitalWrite(led, HIGH);
      else
         digitalWrite(led, LOW);

      Note that if you are using an Arduino Uno, then you can not use the build in LED, as pin 13 is used by the shield for SPI communication.

    • Joe M
      May 21st, 2014 at 23:40 | #16

      Man, you’re so kind! I found that code very helpful. But now I got a new problem ( sorry again I’m new on this πŸ™ ). When I pair de DS4 sometimes I lost the connection after 3 mins. I suppose that it ain’t normal. Haha… I tried using a new dongle (trendnet TBW 107 UB) but the arduino can’t recognise it.

      Do you think it’s a matter of the first dongle, or code…. Maybe bad luck? Hahaha

      Thank you very much for your replies and help.

      Ps. Sorry for write my feedback almost 10 days after your reply.

    • May 22nd, 2014 at 00:03 | #17

      @Joe M
      Make sure that the dongle is getting a stable 5V also make sure that there is actually battery left on the PS4 controller? You can use the following function to read the battery level: https://github.com/felis/USB_Host_Shield_2.0/blob/2c5f5e8c0964c1639265486b6fac17739a55f1d2/PS4Parser.h#L259-L265.

      Also what is the output when it disconnects?

    • Joe M
      June 2nd, 2014 at 02:33 | #18

      @Lauszus

      Man! Thank you very much! you was very helpful!! Seriously, you helped me a lot whit my final project!
      The trouble was the input to arduino, (wasn’t 5V stable as you said).
      Then I was able to control a RC car using DS4, USB Shield, the BT dongle and Arduino UNO!

      Again, thank you very much!

      Take care man, Cheers from Mexico!

    • Joe M
      June 2nd, 2014 at 02:36 | #19

      @Joe M

      HAhaha I wrote “you was” I ment “You were”

      Just a little mistake XD Sorry!.

    • June 2nd, 2014 at 11:08 | #20

      @Joe M
      Glad to hear that everything worked out for you πŸ™‚

    • Tony W
      July 26th, 2014 at 03:09 | #21

      @Lauszus,

      Awesome work first of all! I’ve used the PS4USB library without a hitch, however with the PS4BT example, it seems that I can only pair the PS4 with the dongle once…. in a while, and after that they can no longer pair (LED keeps flashing rapidly then eventually the controller shuts off). I know that in your instructions it says I only need to pair it once, and then I can just create an instance like this: PS4BT PS4(&Btd); and then simply press the PS button. But when I do that, the PS4 will just begin looking for the console (slow flashing LED) and not connect with the BT dongle…and suggestions? Thanks!

    • July 28th, 2014 at 09:24 | #22

      @Tony W
      Please enable serial debugging: https://github.com/felis/USB_Host_Shield_2.0#enable-debugging and send me the output πŸ˜‰

    • Lee
      October 1st, 2014 at 23:40 | #23

      @Tony W Exact same problem here. Not sure if its circumstantial but I can’t connect at all if the terminal window is open.

    • October 2nd, 2014 at 00:42 | #24

      @Lee
      Please see my reply above. If you don’t paste the output it is impossible for me to see what is going on.

    • Lee
      October 2nd, 2014 at 04:13 | #25

      Sorry – I completely missed that your response there was to Tony’s question… ok so I’ve enabled serial debugging and this is the only output. My computer is a mac mini and both the keyboard and mouse are connected via bluetooth. Its interesting that it shows something is connected but I have no idea what as there is no indication that the PS4 controller or anything else is actually connected.

      Serial Output
      —————
      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:02:72:D9:E8:04
      Wait For Incoming Connection Request
      Gamepad is connecting
      Incoming Connection Request
      Remote Name: Wireles
      Connected to Device: 84:17:66:22:E7:5C
      Wait For Incoming Connection Request

      Thanks greatly for your help!

      btw the output really does say Wireles (e.g. that isn’t a retype/cut/paste error)

    • Lee
      October 2nd, 2014 at 07:19 | #26

      I forgot to mention in my previous post that the front LED goes solid white – so maybe it does some to be connected at some level but is not getting complete connection?

      I’m still completely unable to get a connection in non-pairing mode. In paring mode I can get it connected maybe one time in 5.

      Following output was from one of the successful connections

      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:02:72:D9:E8:04
      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
      Received Key Request
      Bluetooth pin is set too: 0000
      Pairing successful with HID device
      Send HID Control Connection Request
      Send HID Control Config Request
      Set protocol mode: 00
      Send HID Interrupt Connection Request
      Send HID Interrupt Config Request
      HID Channels Established
      Wait For Incoming Connection Request
      LeftHatX: 129 LeftHatY: 138 RightHatX: 122 RightHatY: 124
      :
      :
      in this run the BT pairing happens almost instantaneously and the front controller goes to solid blue

      Now another run… everything identical
      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:02:72:D9:E8:04
      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
      Received Key Request
      Bluetooth pin is set too: 0000

      I’ve just enabled the EXTRADEBUG in BTHID and get this output on a failed pairing connection:
      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:02:72:D9:E8:04
      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
      Received Key Request
      Bluetooth pin is set too: 0000
      Pairing successful with HID device
      Send HID Control Connection Request
      Send HID Control Config Request
      Set protocol mode: 00
      Send HID Interrupt Connection Request
      L2CAP Connection Request – PSM: 00 11 SCID: 00 42 Identifier: 02

      this goes to solid white.

      I’m not seeing a pattern in this
      pairing mode: works occassionally
      sometimes fails (e.g. nothing more happens) after line Bluetooth pin is set too: 0000 – light continues to blink in the pairing mode sequence
      sometimes fails to solid white following is an example of that:
      —-
      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:02:72:D9:E8:04
      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
      Received Key Request
      Bluetooth pin is set too: 0000
      Pairing successful with HID device
      Send HID Control Connection Request
      Send HID Control Config Request
      Set protocol mode: 00
      Send HID Interrupt Connection Request
      L2CAP Connection Request – PSM: 00 11 SCID: 00 42 Identifier: 02

      in non pairing mode it sits at the waiting for connection even though it says its connected

      Ideas?

    • Lee
      October 2nd, 2014 at 17:11 | #27

      I’m guessing there is a problem in the if condition inside the run function in BTHID where it seems that in some cases the following block never gets executed.

       if(pBtd->connectToHIDDevice && !pBtd->l2capConnectionClaimed && !connected && !activeConnection) {
          pBtd->l2capConnectionClaimed = true;
          activeConnection = true;
      #ifdef DEBUG_USB_HOST
          Notify(PSTR("rnSend HID Control Connection Request"), 0x80);
      #endif
          hci_handle = pBtd->hci_handle; // Store the HCI Handle for the connection
          l2cap_event_flag = 0; // Reset flags
          identifier = 0;
          pBtd->l2cap_connection_request(hci_handle, identifier, control_dcid, HID_CTRL_PSM);
          l2cap_state = L2CAP_CONTROL_CONNECT_REQUEST;
    • Lee
      October 2nd, 2014 at 18:47 | #28

      hmmm ok so it seems that that word “Wireles” is in fact the problem. For whatever reason it should be getting the entire phrase “Wireless Controller” to identify it in the code as a PS4 and since its not getting the entire phrase it wasn’t setting the incomingPS4 value to true which in turn was keeping the connectToHIDDevice value from getting set to true so it was never entering into the block of code I mentioned in the previous note.

      I’m not sure why its only getting this partial phrase but on the two PS4 controllers I have I’m consistently getting Wireles and not “Wireless Controller” anyway, here is a hack i’ve added to BTD.cpp as a workaround.

      replace the if block starting at line 824 with this code in BTD.cpp and it fixes my problem

      if(classOfDevice[2] == 0 && classOfDevice[1] == 0x25 && classOfDevice[0] == 0x08) {
              if (strncmp((const char*)remote_name, "Wireless Controller", 19) == 0) {

      #ifdef DEBUG_USB_HOST
                      Notify(PSTR("rnPS4 controller is connecting"), 0x80);
      #endif
                      incomingPS4 = true;
              } else if (strncmp((const char*)remote_name, "Wireles", 7) == 0) {
                      // ok so here is the problem - entire phrase "Wireless Controller isn't coming through"
      #ifdef DEBUG_USB_HOST
                      Notify(PSTR("rnbad phrase: hack lw -> PS4 controller is connecting"), 0x80);
      #endif
                      incomingPS4 = true;

              }
      }
    • Lee
      October 2nd, 2014 at 19:06 | #29

      sorry to be filling up your message board with comments – I just wanted to put one of thanks in here – this is truly a remarkable effort and your contribution is massively appreciated! Awesome work on this!

    • VincentC
      October 4th, 2014 at 17:55 | #30

      Hello Lauszus,
      I have a problem when I want to connect my new PS4 controller by USB to my Arduino Due.
      I programmed my Due with the PS4USB.ino and I added the line #include but when I connected the controller and launched it with the button ‘PS’, the controller blinked 6 times and turned off.
      Have you a solution to connect the controller ?
      Vincent

    • October 8th, 2014 at 01:32 | #31

      @Lee
      This is a problem with your dongle. That errors is because your dongle doesn?t not support 2.0+EDR, so the last bytes are not received.

      So unfortunately you will have to buy a new dongle. There is a list of dongles available here: https://github.com/felis/USB_Host_Shield_2.0/wiki/Bluetooth-dongles, but I recommend just buying some from a bunch of different sellers and then you will most likely get some that work.

      @VincentC
      Did you follow these instructions: https://github.com/felis/USB_Host_Shield_2.0#ps4-library?

      Also please enable serial debugging: https://github.com/felis/USB_Host_Shield_2.0#enable-debugging.

    • VincentC
      October 8th, 2014 at 07:49 | #32

      Does it important to connect PS4 controller with Bluetooth before to connect by USB ? Else I did all instructions for USB connection.

      I have activated debugging in settings.h but I haven’t any debug on Serial port. I tested my USB cable and my Arduino Due with an Arduino example with a mouse on usb host and it worked.

      I think I haven’t any connexion problem between the Arduino Due and the PS4 controller.

      Installation problem ?

      Have you an explication ?

    • October 8th, 2014 at 09:01 | #33

      @VincentC
      No you don’t need to connect it via Bluetooth first – maybe that wasn’t clear.

      If nothing shows up, then it means that it does not register that you plug anything in. Please tell me what shield you are using and try to connect an external power supply to the Arduino.

    • VincentC
      October 8th, 2014 at 17:49 | #34

      I use the USB host onboard. It must be use a shield with Max3421?

    • October 8th, 2014 at 19:14 | #35

      @VincentC
      Yes the native USB Host is not supported. It might be supported at some point, as it is in the works πŸ˜‰

    • VincentC
      October 8th, 2014 at 19:52 | #36

      Thanks for this response πŸ˜€
      So let’s go to program this πŸ˜‰

    • Lee
      October 9th, 2014 at 17:13 | #37

      hmm, just fyi, I consulted that list before I bought. The adapter in question is the gmyle with a link to amazon. I’ll try another to see if I can get different and hopefully better result.

      2nd question are there any instructions for making another arduino talk to one with the shield/usb bt combo. I think the other arduino would just need a standard ble attached – as opposed to another host shield and usb bt but that info would be good info.

    • October 10th, 2014 at 01:56 | #38

      @Lee
      Just order a couple of different ones from China, then you should receive a couple that will work πŸ˜‰

      No not yet, as only SPP server is supported for now. I have been working on a SPP client as well, but never got the time to finish it. The code is available here: https://github.com/felis/USB_Host_Shield_2.0/tree/SPPClient.

      You will need a Bluetooth adapter that can configured into host mode in order to connect to the other Arduino.

    • Lee
      October 10th, 2014 at 19:55 | #39

      @Lauszus – ok, I ordered another one. btw, I’m using a standard arduino USB host shield not the Circuits@home – could that be the issue?

      Would you mind commenting on why neither your library (nor most arduino libraries) use callback style functionality? It just makes for such a messy main loop when you have to constantly poll a function as opposed to an event getting raised back out to a callback? Is that just due to the implicit standard or is there an actual technical reason not to use callbacks?

      Also, it looks like the ds4 does not send a final message when the joysticks return to home – if I simply look at the last received message it could indicate that the joystick location is 20% or more off of the home position. It does look possible to more/less infer when the stick is at home as the message stream stops so I can determine home by setting a timer and if there is no update in x milliseconds then it must be at home. Is there any other way to do this?

      thanks for the client code – will try to take a look over the weekend

    • October 15th, 2014 at 03:40 | #40

      @Lee
      Yes that can be the issue. Please use the official shield in order to support the actual developers of the hardware and software.

      The library works by each driver registers themselves – see https://github.com/felis/USB_Host_Shield_2.0/blob/58a143b9fb1b8fc7c7b4568b74d3b9f175a2302b/BTD.cpp#L44-L45 and https://github.com/felis/USB_Host_Shield_2.0/blob/58a143b9fb1b8fc7c7b4568b74d3b9f175a2302b/UsbCore.h#L215-L223 and the different classes are then call in Usb.Task(). For instance here Poll is called: https://github.com/felis/USB_Host_Shield_2.0/blob/58a143b9fb1b8fc7c7b4568b74d3b9f175a2302b/Usb.cpp.

      The code does in fact using callback some places. Here is an example: https://github.com/felis/USB_Host_Shield_2.0/blob/58a143b9fb1b8fc7c7b4568b74d3b9f175a2302b/examples/Bluetooth/BTHID/BTHID.ino.
      The only thing that is called in the loop is Usb.Task(), but you could differently call this in an interrupt if you wanted to.

      What do you mean by home? Are you talking about when it disconnects? But yes using a timer is the proper way to do it.

    • @Stella
      November 1st, 2014 at 11:44 | #41

      Hello,
      it could be possible to use a new BT shield (ble shield ) and Arduino UNO?

    • November 3rd, 2014 at 22:08 | #42

      @@Stella
      No that is not possible. Shield like that normally only support SPP and I need HID host in order to talk with the PS4 controller.

    • Donut
      November 6th, 2014 at 01:07 | #43

      @Lauszus
      Hello! I’m new here and I’m trying to use this setup to control an RC

      I’d like to ask if it’s possible to have the R2 and L2 buttons to control the motors? If so, how do I get around to doing so I’m so lost right now. Any tips?

    • November 6th, 2014 at 08:47 | #44

      @Donut
      Simply use the analogWrite function: http://arduino.cc/en/Reference/analogWrite.

      Then you can simply do something like this:

      analogWrite(pin, PS4.getAnalogButton(L2));
    • Donut
      December 2nd, 2014 at 08:29 | #45

      Hello @Lauszus, thank you for the help!
      Sorry for the late reply but I’ve been working on a drone for an engineering project and I’ve come across another hurdle!

      I need help with programming a 180ΒΊ servo to work with the PS4, as of right now I’ve got

      if (PS4.connected()) {
      myservo.write(map(PS4.getAnalogHat(RightHatX), 0, 255, 0, 180));
      }
      else
      {
      myservo.write(90);
      }

      But this doesn’t seem to be doing anything at all, please advice

    • December 2nd, 2014 at 08:45 | #46

      @Donut
      Did you remember to attach the servo as well: http://arduino.cc/en/Reference/ServoAttach?

    • Vinit
      January 17th, 2015 at 20:03 | #47

      Hi Lauszus !!

      I have a PS4 wireless controller from sony which comes with it’s own usb bluetooth dongle . The interfacing of the PS4 controller with Arduino Mega ADK is not at all smooth . Sometimes it succesfully gets connected , sending the correct data which i can see on the serial monitor of the Arduino . While on some occasions there is no connection .

      Moreover sometimes even when the connection is succesfull , i am getting an error –
      HCI command failed : 01 and
      Unknown Report type : 11 while the data that the PS4 wireless controller is sending is correct for the

    • Vinit
      January 17th, 2015 at 20:06 | #48

      The error is for all the buttons on the PS4 controller while the data received for the corresponding button is correct .

      Any idea as to what may be happening ?

    • January 18th, 2015 at 02:51 | #49

      @Vinit
      Can you send me a link to the controller you are using?

    • cem
      February 6th, 2015 at 15:30 | #50

      what about how can ? use ps4 or ps3 controller with wireless dongle on host shield?
      can you help me?

    • Vinit
      February 11th, 2015 at 07:48 | #52

      Sorry to be late !!

      Link for the controller – http://www.sony.co.in/product/ps4+dualshock+wave+blue .

    • February 12th, 2015 at 00:29 | #53

      @Vinit
      It’s sounds like a problem with your dongle. Please try another one.

    • Krunal
      February 26th, 2015 at 16:15 | #54

      Hello,
      I am using two PS4 controllers to control two robots with arduino Uno simultaneously. But, the PS4 often loses synchronization with the arduino. As soon as 1 controller loses sync, the other one loses it immediately. And this happens very often. Please help!!!

    • February 26th, 2015 at 17:33 | #55

      @Krunal
      Can you post your code somewhere? For instance here: https://gist.github.com/.

      Also make sure that you have a stable 5V rail.

    • Krunal
    • Krunal
      February 28th, 2015 at 18:18 | #58

      @Lauszus
      Actually I had put that line because if the connection is lost, I had to upload the program again to pair the controllers.
      And also how can i cahnge the pairing key for the controller?

    • March 1st, 2015 at 00:32 | #59

      @Krunal
      That is not the correct way to do it!

      You should call:

      PS4.pair();

      But why do you want to pair it again and again? It is only needed once. After that you can simply just press on the PS button on the controller.

    • Krunal
      March 1st, 2015 at 03:37 | #60

      @Lauszus
      Actually while using 2 controllers, there is aproblem occuring that the controllers often get disconnected. I thought it will be so;ved if I change the pairing key for anyone controller which is set to 0000 by default. Bu how to do that?

    • March 1st, 2015 at 11:38 | #61

      @Krunal
      Please remove that line first. I might be the reason why they are disconnecting. The pairing key doesn’t matter for the PS4 controllers.

    • Krunal
      March 2nd, 2015 at 08:14 | #62

      @Lauszus
      Ohkk. Now even after removing that line, theres a problem coming. Consider that one of my controllers is connected, when I try to connect my second controller, the first one is disconnected if the second one goes into partial pairing mode (ie a constant white led is on). But this happens often. So please tell me a solution for this.

    • March 2nd, 2015 at 22:41 | #63

      @Krunal
      Okay now I understand what you are trying to do. You need to make a instance for each controller. Please see this example for the PS3 controller for inspiration: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS3Multi/PS3Multi.ino.

    • Marco
      March 17th, 2015 at 21:12 | #64

      Hello, my son and I are willing to connect PS4 controller using Arduino due native USB port, but seems that Bluetooth dongle is not recognised by the library. We tried also with PS3 controller (and related software) but same bad result.
      Do the library support also native USB port or only USB shield?
      Thank you

    • March 18th, 2015 at 00:21 | #65

      @Marco
      Unfortunately the Due native port is not supported as of yet, but this is something we are working on supporting. Hopefully it will get released in a couple of months.

      If you can’t wait, then you can buy a shield. The API should be almost the same in the new library, so you won’t have to rewrite your code once it is released.

    • Frank
      March 25th, 2015 at 23:58 | #66

      I’m attempting to use a PS4 controller over BT. It doesn’t seem very stable at all, and there are delays that happen when using the PS4BT sketch. For instance I will press the up button which should turn the LED red. IT doesn’t. I press it again, and then it lights up red. I next press right, which should go to blue, but it stays red. Then I press it again and it finally goes to blue. I do see the button presses happening, but the led isn’t responding right. After a short bit of time the controller loses connection for whatever reason and won’t reconnect.

    • March 26th, 2015 at 00:13 | #67

      @Frank
      Make sure that there is actually battery left on the PS4 controller. Also please enable serial debugging: https://github.com/felis/USB_Host_Shield_2.0#enable-debugging and post the output.

    • Frank
      March 30th, 2015 at 22:05 | #68

      @Lauszus – Thanks for the quick reply. I picked up a different dongle and it seems to be working much better, but still not 100%. I have a fully charged PS4 controller, so that isn’t the problem. I paired the controller while running PS4BT.ino, and when finished the controller LED turned BLUE, and all the controls functioned correctly.

      Next, I comment out the PAIR line and uncommented the corressponding line. When I ran it, the controller appears to connect, but the light doesn’t turn BLUE, and none of the controls do anything. What are your suggestions?

      Here is the log from that run:

      PS4 Bluetooth Library Started
      BTD ConfigureDevice
      BTD Init
      Addr: 01
      Endpoint descriptor:
      Length: 07
      Type: 05
      Address: 81
      Attributes: 03
      MaxPktSize: 0010
      Poll Intrv: 01
      Endpoint descriptor:
      Length: 07
      Type: 05
      Address: 82
      Attributes: 02
      MaxPktSize: 0040
      Poll Intrv: 01
      Endpoint descriptor:
      Length: 07
      Type: 05
      Address: 02
      Attributes: 02
      MaxPktSize: 0040
      Poll Intrv: 01
      Bluetooth Dongle Initialized
      Connection established
      Connection established
      Connection established
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 5C:F3:70:65:E8:27
      Wait For Incoming Connection Request
      Gamepad is connecting
      Class of device: 00 25 08
      Incoming Connection Request
      Remote Name: Wireles
      Connection established
      Connected to Device: 1C:96:5A:81:F9:25
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Wait For Incoming Connection Request

    • Frank
      March 30th, 2015 at 22:08 | #69

      I tried it again, and the same situation on the PS4 controller, but slightly different debug output….

      BTD ConfigureDevice
      BTD Init
      Addr: 01
      Endpoint descriptor:
      Length: 07
      Type: 05
      Address: 81
      Attributes: 03
      MaxPktSize: 0010
      Poll Intrv: 01
      Endpoint descriptor:
      Length: 07
      Type: 05
      Address: 82
      Attributes: 02
      MaxPktSize: 0040
      Poll Intrv: 01
      Endpoint descriptor:
      Length: 07
      Type: 05
      Address: 02
      Attributes: 02
      MaxPktSize: 0040
      Poll Intrv: 01
      Bluetooth Dongle Initialized
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 5C:F3:70:65:E8:27
      Wait For Incoming Connection Request
      Gamepad is connecting
      Class of device: 00 25 08
      Incoming Connection Request
      Remote Name: Wireless Controller
      PS4 controller is connecting
      Connection established
      Connected to Device: 1C:96:5A:81:F9:25
      Send HID Control Connection Request
      Connection established
      Connection established
      Connection established
      Connection established
      Connection established
      Received Key Request
      Bluetooth pin is set too: 0000

    • March 31st, 2015 at 15:14 | #70

      @Frank
      It still looks like a problem with your dongle. Could you try to replace this line: https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.cpp#L393 with:

      if(!rcode) {

      And: https://github.com/felis/USB_Host_Shield_2.0/blob/master/BTD.cpp#L619 with:

      else if(rcode != hrNAK) {
    • Frank
      March 31st, 2015 at 16:11 | #71

      That seemed to work better, but not perfect. Now it either connects correctly (turns BLUE), or else it tries for a moment then stops with (Connection Failed: 10).

      Since it is most likely my dongle, I think I will just order one through your site. You should see the order come through soon.

    • March 31st, 2015 at 16:46 | #72

      @Frank
      Yes the new dongle should solve your problem πŸ™‚

    • Frank
      March 31st, 2015 at 19:03 | #73

      I do have one suggestion if you get time. Could you make a singleton version of the PS4BT project? I have lots of different classes that need to get info from the controller, and a singleton would make it a whole lot easier to use.

      Here some pseudo code of the ino that would use it…

      #include “PS4BTSingleton.h”

      void setup() {
      // init usb
      PS4BTSingleton::instance().init();
      }

      void loop() {
      //usb task
      PS4BTSingleton::instance().task();

      // do something if connected
      if (PS4BTSingleton::instance().connected())
      PS4BTSingleton::instance().setLed(255, 0, 0);
      }

    • April 1st, 2015 at 15:57 | #74

      @Frank
      No I won’t make the official library a singleton, as you need to create more instances if you want to use multiple PS4 controller.

      Why would you want to use a singleton in the first place?

    • Frank Trocchia
      April 1st, 2015 at 18:58 | #75

      I’m using the PS4 controller for my R2-D2 build. I only need one controller for the droid. Having a singleton would allow each module to have access to the controller instead of passing the instance around. I could then use it easily with my sound board, servo boards, lighting boards, etc.

      It’s just a suggestion. Thought it would be easier for you since you know the ins and outs of the code that you could make a singleton version that sits side by side with the original.

      Anyway, I did order the dongle, so hopefully that will fix that problem.

    • April 1st, 2015 at 20:57 | #76

      @Frank Trocchia
      Okay cool. There is actually numerous people using my libraries for R2-D2’s πŸ™‚
      Hmm, I won’t do that, as it will break a lot of peoples code, but you can just make a new class which inherits it and make it a singleton?

      Why can’t you just make it global and access it from the difference drivers? If they are C++ classes they could just take a pointer to the PS4BT class as an argument, just like the BTD class take a pointer to the USB class as an argument.

    • Frank Trocchia
      April 2nd, 2015 at 12:03 | #77

      Yeah I will probably go that route. Thanks for all your help. Once the dongle arrives I will give it some tests and if anything is odd I will get back to you.

      Thanks

    • April 2nd, 2015 at 12:23 | #78

      @Frank Trocchia
      Okay super πŸ™‚

    • Frank
      April 10th, 2015 at 00:32 | #79

      The new dongle works much better. I am curious though why it only fully connects every other time? Using the example code… when I start the controller it connects and the light turns blue. I press the PS button and it disconnects, with the light turning off. When I try to connect again, the light flashes briefly then turns on solid white. I hold the PS button for 10 seconds and the controller powers off. When I press the PS button again, it connects and the light turns blue. It just seems like every other time it connects properly.

      Is there something wrong? Is there a timeout period, or some sort of cleanup code that isn’t called that should be?

      Thanks

    • Frank
      April 10th, 2015 at 01:09 | #80

      Here is a log. You can see it connects the first time… and then I hit PS and it disconnects. The next time it tries it starts the connection process, but just stops with the ‘Received Key Request’ message.

      Wait For Incoming Connection Request
      Gamepad is connecting
      Incoming Connection Request
      Remote Name: Wireless Controller
      PS4 controller is connecting
      Connected to Device: 1C:96:5A:81:F9:25
      Send HID Control Connection Request
      Received Key Request
      Bluetooth pin is set too: 0000
      Send HID Control Config Request
      Set protocol mode: 00
      Send HID Interrupt Connection Request
      Send HID Interrupt Config Request
      HID Channels Established
      Wait For Incoming Connection Request
      PS
      Disconnected Interrupt Channel
      Disconnected Control Channel
      HCI Disconnected from Device
      Wait For Incoming Connection Request
      Gamepad is connecting
      Incoming Connection Request
      Remote Name: Wireless Controller
      PS4 controller is connecting
      Connected to Device: 1C:96:5A:81:F9:25
      Send HID Control Connection Request
      Received Key Request

    • Frank Trocchia
      April 10th, 2015 at 12:45 | #81

      I don’t see where I’ve posted it, so I want to add that I’m using an Arduino Mega ADK, the bluetooth dongle I just received from you, and a stock PS4 controller.

      Thanks

    • April 20th, 2015 at 01:23 | #82

      @Frank
      That sounds very weird. Is it exactly every second time? Or does it just happen once in a while?

    • luis silva
      April 24th, 2015 at 05:22 | #83

      i have a arduino uno with the usb shield and the bluetooth dongle and i got it to connect to the remote but what i want to do it to use my ps4 controller to control an 8 relay module i have connected to pins 2-9 but I’m not getting no where with this sketch cause I’m new to this anyone can help?

    • April 27th, 2015 at 02:42 | #84

      So I am loving the simplicity this library gives the end user, however I am running into one small issue with PS4.getButtonClick(). It seems that when placed into an if statement, it will only be triggered as True once. If the button you are calling for is being held down, it won’t trip the if statement and will instead go over it. So my question is, what does PS4.getButtonClick() return? and how can I have it continuously perform an action via an if statement while the button is held?

    • April 27th, 2015 at 06:12 | #85

      @Mike O?Donnell
      You are exactly right, as this is the feature of the getButtonClick() function.
      In your case you should be using the PS4.getButtonPress() method as this will return the current state of the button and therefor work as you require.

    • April 27th, 2015 at 23:51 | #86

      @luis silva
      Please share you code somewhere, so I can give you some hints. Also check out this code I wrote for a guy some time ago for the SPP library: https://gist.github.com/Lauszus/3383715.

      @Mike O?Donnell
      Yes getButtonPress() is what you are looking for. Also check out the documentation for the PS4BT class: http://felis.github.io/USB_Host_Shield_2.0/class_p_s4_b_t.html.

    • Jimmy
      May 1st, 2015 at 23:04 | #87

      Hello!

      I have stupid problem, but i am not able to get through.

      All i need is with pressing button turn LED ON and with pressing another button turn that LED OFF.

      Thanks so much for help!

    • May 2nd, 2015 at 18:29 | #88

      @Jimmy
      It can be done like this:

      if (PS4.getButtonClick(UP))
          PS4.setLed(Blue);
      if (PS4.getButtonClick(DOWN))
          PS4.setLed(Off);
    • Marco
      July 2nd, 2015 at 11:44 | #89

      @Lauszus
      Hello, me again πŸ™‚ hoping there are some good news for the support of native usb port on Arduino Due.
      Currently we are using the native port to receive input from a Mouse and control a VEX robot (via UART connection between Arduino and VEX).

      Thank you

      Marco & Luca

    • July 4th, 2015 at 22:12 | #90

      @Marco
      Not it is not yet supported, but should be added to the 3.0 version of the library at some point: https://github.com/felis/UHS30.

    • parth
      October 26th, 2015 at 18:01 | #91

      hello!!
      i use host shield for connecting ps4 to arduino for controlling robot…
      but as soon as i give any input to analog pins , my ps4 gets disconnected….
      what could be the reason???

    • November 2nd, 2015 at 15:09 | #92

      @parth
      Can you share you code somewhere, then I will have a look.

    • oleg
      November 11th, 2015 at 21:07 | #93

      Hi,
      I try to connect the PS4 controller with Arduino UNO, and I cant. I got this message:
      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:15:83:0C:BF:EB
      Please enable discovery of your device
      Unknown Device Connected – VID: 054C PID: 05C4
      BTD Init Failed, error code: D1

      can you help me?

    • November 12th, 2015 at 18:36 | #94

      @oleg
      Please uncomment the following line: https://github.com/felis/USB_Host_Shield_2.0/blob/master/examples/Bluetooth/PS4BT/PS4BT.ino#L17, as it might have a hub inside.

    • oleg
      November 12th, 2015 at 21:16 | #95

      Thanks. I uncomment that line but it did not work. I change the Dongle and push tighter the share and Ps for 5 seconds, and it works.

    • November 13th, 2015 at 00:30 | #96

      @oleg
      Good to hear you got it working πŸ˜‰

    • terrance
      December 4th, 2015 at 07:52 | #97

      do you still have the code to PS3 instead?

    • December 4th, 2015 at 20:30 | #98

      @terrance
      Yes it located at the same location as the PS4 library: https://github.com/felis/USB_Host_Shield_2.0.

    • simon watts
      January 6th, 2016 at 01:09 | #99

      @Lauszus
      Hi there I notice that the USB Host Shield has been retired, you mentioned that you are working on getting the Due Native USB port working, have you managed to do this as yet?
      Thanks
      Simon

    • January 6th, 2016 at 16:00 | #100

      @simon watts
      The shield is not retired only the version made by the Arduino team. You can get it from us if you live in EU: http://shop.tkjelectronics.dk or here: https://www.circuitsathome.com/products-page/arduino-shields if you live in USA.

      No I have not worked on getting native usb host working. One of the reasons is that I do not own a Due.

    • Uddhav
      January 11th, 2016 at 19:39 | #101

      @Lauszus
      Thanks for the library…I have encountered a problem while working on PS4 via USB for my robotics… The PS4 controller sometimes disconnects from arduino” while its still plugged in “…
      This happens less frequently but happens….I mean the Led is on in Ps4 but It gives no response….To check connection I added an ELSE case to the PS4USB example code like this..
      void loop() {
      Usb.Task();

      if (PS4.connected()) {
      // my code to run robot
      }
      else{
      stop(); //stop the robot
      digitalWrite(led,HIGH); // indicator led to check when it disconnects
      }
      }
      After being disconnected we have to unplug ps4 then put it back again then it regains connection
      Like I said ps4 is on but unresponsive…
      We tried changing many circuits connected to arduino but problem still persists
      I’m still confused from where this error is coming either from programming side or Circuits..Please help on disconnection issue

    • Uddhav
      January 11th, 2016 at 20:05 | #102

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

    • January 18th, 2016 at 14:51 | #103

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

    • Shawn
      March 6th, 2016 at 07:34 | #104

      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 πŸ™‚

    • March 8th, 2016 at 23:41 | #105

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

    • Yuji
      March 28th, 2016 at 06:53 | #106

      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.

    • April 14th, 2016 at 23:08 | #107

      @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.

    • Uddhav
      April 30th, 2016 at 05:24 | #108

      @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

    • mitch
      April 30th, 2016 at 06:35 | #109

      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

    • Fernando
      May 11th, 2016 at 13:33 | #111

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

    • Patrick
      June 25th, 2016 at 01:28 | #112

      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

    • August 2nd, 2016 at 12:44 | #113

      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.

    • Rafal
      September 28th, 2016 at 11:30 | #114

      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,

    • October 10th, 2016 at 20:29 | #115
    • axebuddy
      October 19th, 2016 at 08:03 | #116

      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

    • October 20th, 2016 at 19:24 | #117

      @axebuddy
      Do you have any delays in your code?

    • axebuddy
      October 21st, 2016 at 04:12 | #118

      @Kristian Sloth Lauszus
      Thank you for your quick reply.

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

      Thanks

    • Rodrigo Alves Nunes
      November 13th, 2016 at 21:34 | #119

      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);
      Β Β Β Β Β Β  }

    • Helge Nodland
      December 2nd, 2016 at 15:13 | #120

      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

    • December 5th, 2016 at 03:30 | #121

      @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();
    • antoine
      January 15th, 2017 at 01:48 | #122

      Hi kristian, i wonder if we can use the ps4 controler communicate with the arduino and at the same time using it to play with on the ps4 console?

    • Eddie Wong
      January 24th, 2017 at 05:40 | #123

      Please help, it seems not work on new DualShock 4 Wireless Controller for PlayStation 4 – Jet Black (CUH-ZCT2) .

    • February 3rd, 2017 at 11:20 | #124

      @antoine
      Unfortunately this is currently not supported.

      @Eddie Wong
      Please open a issue on Github: https://github.com/felis/USB_Host_Shield_2.0/issues.

    • Erick
      February 6th, 2017 at 21:55 | #125

      I am having the same issue as Frank. Where you able to fix this? Where did you find the list of working BT dongles? Also, I only get “PS4 Bluetooth Library Started” fro the serial, I don’t get the rest, where did I go wrong here? @Patrick

    • February 7th, 2017 at 16:55 | #126
    • February 12th, 2017 at 20:35 | #127

      If I use the mac address to hard code the pairing like this:

      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);

      I get the errors below. What am I doing wrong here?

      S4BT:27: error: stray ‘303’ in program
      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);
      ^
      PS4BT:27: error: stray ‘227’ in program
      PS4BT:27: error: stray ‘303’ in program
      PS4BT:27: error: stray ‘227’ in program
      PS4BT:27: error: stray ‘303’ in program
      PS4BT:27: error: stray ‘227’ in program
      PS4BT:27: error: stray ‘303’ in program
      PS4BT:27: error: stray ‘227’ in program
      PS4BT:27: error: expected ‘)’ before numeric constant
      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);
      ^
      PS4BT:27: error: expected ‘)’ before numeric constant
      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);
      ^
      PS4BT:27: error: expected ‘)’ before numeric constant
      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);
      ^
      PS4BT:27: error: expected ‘)’ before numeric constant
      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);
      ^
      PS4BT:27: error: no matching function for call to ‘PS4BT::PS4BT(BTD*, int, int, int, int, int, int)’
      PS4BT PS4(&Btd,0Γ—00,0Γ—15,0Γ—83,0xE9,0x3C,0Γ—22);
      ^
      /var/folders/jf/ymyrxwb12jggs7qnbjp0ck8m0000gn/T/arduino_modified_sketch_766671/PS4BT.ino:27:49: note: candidates are:
      In file included from /var/folders/jf/ymyrxwb12jggs7qnbjp0ck8m0000gn/T/arduino_modified_sketch_766671/PS4BT.ino:7:0:
      /Users/darrenblondin/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/PS4BT.h:36:9: note: PS4BT::PS4BT(BTD*, bool, const char*)
      PS4BT(BTD *p, bool pair = false, const char *pin = “0000”) :
      ^
      /Users/darrenblondin/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/PS4BT.h:36:9: note: candidate expects 3 arguments, 7 provided
      In file included from /var/folders/jf/ymyrxwb12jggs7qnbjp0ck8m0000gn/T/arduino_modified_sketch_766671/PS4BT.ino:7:0:
      /Users/darrenblondin/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/PS4BT.h:28:7: note: PS4BT::PS4BT(const PS4BT&)
      class PS4BT : public BTHID, public PS4Parser {
      ^
      /Users/darrenblondin/Documents/Arduino/libraries/USB_Host_Shield_2.0-master/PS4BT.h:28:7: note: candidate expects 1 argument, 7 provided
      Using library USB_Host_Shield_2.0-master at version 1.3.0 in folder: /Users/darrenblondin/Documents/Arduino/libraries/USB_Host_Shield_2.0-master
      Using library SPI at version 1.0 in folder: /Users/darrenblondin/Library/Arduino15/packages/arduino/hardware/sam/1.6.4/libraries/SPI
      exit status 1
      stray ‘303’ in program

    • February 20th, 2017 at 17:36 | #128

      @Darren Blondin
      It looks like you have added non-ascii characters – see: http://stackoverflow.com/questions/22384338/stray-303-and-stray-215-in-program-why. Please replace letter x in the hex value with the normal letter x.

      What editor are you using?

    • Erick
      February 28th, 2017 at 23:03 | #129

      Hello Lauszus,

      I have been trying to connect my PS4 Controller tot he USB Shield for some time without success. At first I thought it was my BT dongle so I ordered one from your website. I just received dongle but still I am not having success in paring it and being able to read info from the controller. Can you please let me know what I am doing wrong or what I am missing?

      I start with the pairing mode then I comment it out and uncomment the other part.

      I am using an Arduino UNO with he USB Host Shield Arduino.

      And the is what i get from the serial port.

      PS4 Bluetooth Library Started
      Bluetooth Dongle Initialized
      HCI Reset complete
      Write class of device
      Local Bluetooth Address: 00:11:22:98:76:54

      this is the version of the code I am using.

      /*
      Example sketch for the PS4 Bluetooth library – developed by Kristian Lauszus
      For more information visit my blog: http://blog.tkjelectronics.dk/ or
      send me an e-mail: kristianl@tkjelectronics.com
      */

      #include
      #include

      // Satisfy the IDE, which needs to see the include statment in the ino too.
      #ifdef dobogusinclude
      #include
      #include
      #endif

      USB Usb;
      //USBHub Hub1(&Usb); // Some dongles have a hub inside
      BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so

      /* You can create the instance of the PS4BT class in two ways */
      // This will start an inquiry and then pair with the PS4 controller – you only have to do this once
      // You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in pairing mode
      PS4BT PS4(&Btd, PAIR);

      // After that you can simply create the instance like so and then press the PS button on the device
      //PS4BT PS4(&Btd);

      bool printAngle, printTouch;
      uint8_t oldL2Value, oldR2Value;

      void setup() {
      Serial.begin(115200);
      #if !defined(__MIPSEL__)
      while (!Serial); // Wait for serial port to connect – used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
      #endif
      if (Usb.Init() == -1) {
      Serial.print(F(“rnOSC did not start”));
      while (1); // Halt
      }
      Serial.print(F(“rnPS4 Bluetooth Library Started”));
      }
      void loop() {
      Usb.Task();

      if (PS4.connected()) {
      if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) 137 || PS4.getAnalogHat(LeftHatY) 137 || PS4.getAnalogHat(RightHatX) 137 || PS4.getAnalogHat(RightHatY) < 117) {
      Serial.print(F("rnLeftHatX: "));
      Serial.print(PS4.getAnalogHat(LeftHatX));
      Serial.print(F("tLeftHatY: "));
      Serial.print(PS4.getAnalogHat(LeftHatY));
      Serial.print(F("tRightHatX: "));
      Serial.print(PS4.getAnalogHat(RightHatX));
      Serial.print(F("tRightHatY: "));
      Serial.print(PS4.getAnalogHat(RightHatY));
      }

      if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
      Serial.print(F("rnL2: "));
      Serial.print(PS4.getAnalogButton(L2));
      Serial.print(F("tR2: "));
      Serial.print(PS4.getAnalogButton(R2));
      }
      if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
      PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
      oldL2Value = PS4.getAnalogButton(L2);
      oldR2Value = PS4.getAnalogButton(R2);

      if (PS4.getButtonClick(PS)) {
      Serial.print(F("rnPS"));
      PS4.disconnect();
      }
      else {
      if (PS4.getButtonClick(TRIANGLE)) {
      Serial.print(F("rnTraingle"));
      PS4.setRumbleOn(RumbleLow);
      }
      if (PS4.getButtonClick(CIRCLE)) {
      Serial.print(F("rnCircle"));
      PS4.setRumbleOn(RumbleHigh);
      }
      if (PS4.getButtonClick(CROSS)) {
      Serial.print(F("rnCross"));
      PS4.setLedFlash(10, 10); // Set it to blink rapidly
      }
      if (PS4.getButtonClick(SQUARE)) {
      Serial.print(F("rnSquare"));
      PS4.setLedFlash(0, 0); // Turn off blinking
      }

      if (PS4.getButtonClick(UP)) {
      Serial.print(F("rnUp"));
      PS4.setLed(Red);
      } if (PS4.getButtonClick(RIGHT)) {
      Serial.print(F("rnRight"));
      PS4.setLed(Blue);
      } if (PS4.getButtonClick(DOWN)) {
      Serial.print(F("rnDown"));
      PS4.setLed(Yellow);
      } if (PS4.getButtonClick(LEFT)) {
      Serial.print(F("rnLeft"));
      PS4.setLed(Green);
      }

      if (PS4.getButtonClick(L1))
      Serial.print(F("rnL1"));
      if (PS4.getButtonClick(L3))
      Serial.print(F("rnL3"));
      if (PS4.getButtonClick(R1))
      Serial.print(F("rnR1"));
      if (PS4.getButtonClick(R3))
      Serial.print(F("rnR3"));

      if (PS4.getButtonClick(SHARE))
      Serial.print(F("rnShare"));
      if (PS4.getButtonClick(OPTIONS)) {
      Serial.print(F("rnOptions"));
      printAngle = !printAngle;
      }
      if (PS4.getButtonClick(TOUCHPAD)) {
      Serial.print(F("rnTouchpad"));
      printTouch = !printTouch;
      }

      if (printAngle) { // Print angle calculated using the accelerometer only
      Serial.print(F("rnPitch: "));
      Serial.print(PS4.getAngle(Pitch));
      Serial.print(F("tRoll: "));
      Serial.print(PS4.getAngle(Roll));
      }

      if (printTouch) { // Print the x, y coordinates of the touchpad
      if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
      Serial.print(F("rn"));
      for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
      if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
      Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
      Serial.print(PS4.getX(i));
      Serial.print(F("tY")); Serial.print(i + 1); Serial.print(F(": "));
      Serial.print(PS4.getY(i));
      Serial.print(F("t"));
      }
      }
      }
      }
      }
      }

    • zacharylkc
      March 7th, 2017 at 09:23 | #130

      Thanks for the library
      i have try the Arduino mega 2560 with ps4 controller
      how can i remove the Rumble when i press R2&L2 button

    • Jower
      March 29th, 2017 at 18:36 | #131

      It is possible to use your library using an HM-10 Bluetooth module instead of the the USB Dongle+USB Shield? Those bluetooth modules can act as a Master or Slave

    • March 29th, 2017 at 20:34 | #132

      @Jower
      No, that is not possible. The HM-10 module enumerates as an SPP device both in Slave and Master mode and can therefore not be used to connect to HID-type of devices.

    • mitch
      April 3rd, 2017 at 23:36 | #133

      Kristian,

      I saw in the example you include some Teensy communication library. I love teensy.
      How do I connect the USB shield to it. Physically, there is no SPI port on it,

      Tnks

  2. Nicholas Rooyakkers
    October 12th, 2017 at 00:03 | #134

    Great work!
    I set up a PS4 controller to operate the boom and grapple on a forestry forwarder and it works great!
    Arduino Mega, USB 2.0 host shield and USB bt dongle.
    The 24vdc solenoids are controlled via PWM through ULN2003A. This setup works very smooth but I have a problem with the code freezing after what seems like random amounts of time.
    The game pad led gets stuck blue for ~30 seconds after arduino is powered off. I think arduino calls for data from game pad and gets hung waiting. Where can I find this info so I can add a watchdog to ask for data again using Millis?
    Thanks
    Nick.

  1. No trackbacks yet.