Creating a DualShock 4 Controlled Arduino

130K7629

Intro: Creating a DualShock 4 Controlled Arduino

With just a few extra pieces of hardware, upgrade your Arduino to receive input from a Sony DualShock 4 controller via a Bluetooth connection.

STEP 1: Gathering the Hardware

Here is all the hardware you'll need to control your Arduino with a DualShock 4 controller:

1. Arduino Uno - Microcontroller board that allows a user to run their own custom firmware.

2. USB 2.0 Host Shield - This is a must. Found here, the USB Host Shield fits right on top of the Ardiono Uno and provides support for many USB devices.

3. USB Bluetooth dongle - There is a list of Bluetooth dongles that are compatible with the USB Host Shield, many of them very small and cheap. I got mine on Amazon here.

4. DualShock 4 controller - This controller works via a Bluetooth connection with a PlayStation 4 video game console by Sony. With the USB Host Shield libraries, we have full access to the controller's many buttons, joysticks, triggers, gyroscopes, accelerometers, and LED. The controller charges with a micro USB cable, so it is also possible to use the controller in a wired fashion with the board, but we're interested in the wireless capability.

5. Computer with USB port - The Arduino requires another computer to write programs for it and a serial connection to write to the Arduino board. Connect the Arduino to a computer via the USB B port on the chip. This USB connection provides both power and serial communication to the board. For any software-related steps, I will be assuming that you're using a Windows PC, for simplicity. All of the following is possible with very little extra work in both Mac and Linux. If you run into any compatibility issues use Google to find an OS-specific solution, I promise they all exist.

STEP 2: Install Arduino Software

If you haven't already, download and install the Arduino IDE 1.0.5 here. This software allows you to write programs for your Arduino and uploads them to the board for you. This software will also be necessary to link the USB Host 2.0 libraries, which provide the code to communicate with the DualShock 4 controller. Remember to download the file for your specific operating system and install the Arduino IDE somewhere you'll be able to find.

The Arduino IDE is very simple, a few buttons up top to open, save and create project files. The 'Verify' button compiles the C code from the project open in the IDE, and 'Upload' compiles and loads the code onto the Arduino, if connected to the computer via USB. You can look at File->Examples to find some sample code to help bring you up to speed.

STEP 3: Installing the USB 2.0 Host Shield Libraries

Download the libraries from GitHub by clicking the 'Download as .zip' button, the last button on the right side . Once you have the .zip file, move it to the 'Libraries' folder within the 'Arduino' install folder, extract the files, and remove the '.' from the folder name. Leave the folder here and close your Arduino IDE window, if you have it open. To link the libraries in the Arduino IDE, go to 'Sketch->Import Libraries' and navigate the file window to find that folder. Once that is completed, 'File->Examples->USB_Host_20_Libs' will contain all the code we need to communicate with the USB Host Shield, the Bluetooth dongle, and the DualShock 4 controller. Check out some of the examples, especially the 'PS4BT' example in the 'Bluetooth' folder.

STEP 4: Assemble the Hardware

Insert the Bluetooth dongle into the USB Host shield's USB port. Set the USB shield on the Arduino Uno, lining up the correct pins and gently push them together. This little computer will communicate with the controller once we install our code onto it. To connect the Arduino to your computer, use your USB cable. Find

STEP 5: Run Your Program!

The example code provided shows how to read input from the controller, I quickly mocked up some code that plays around with the LED on the controller. By pressing the up button on the D-pad, you can switch between changing color based on the gyroscope readings and a right thumbstick plus left trigger combo.

Open led_demo.ino in the Aurduino IDE and click 'Verify' to see that it compiles. Make sure your Arduino Uno, USB shield and bluetooth dongle are assembled and connect it to the computer via a USB. Click 'Upload' and wait for the code to finish writing to the Arduino. Once you see that complete, go to the next step to open a serial terminal.

STEP 6: Getting Debug Information Via Serial Terminal

The Arduino creates a serial communication connection to communicate with the host computer. This connection is primarily used to upload compiled code directly onto the Arduino, but it can also be used to display output information as it happens on the board.

PuTTY is a tool that facilitates many types of terminal communication, such as serial communication, a fairly simple protocol. Download PuTTY from the website here. Install and run PuTTY, select 'Serial' underneath 'Connection Type' and select the correct COM port that the Arduino is on. On Windows, to find which COM port your Arduino is on, find the Device Manager (search for it under the Control Panel) and look at the devices listed under COM, one of them should be your Arduino! Now put in the same value for speed that is in your Arduino code ( Serial.begin(115200); is the line that starts a serial connection, default baud rate in PuTTY is 9600).

The Arduino software will not be able to upload any code to your board while there is an active serial connection open, so make sure you exit the serial terminal when you want to update code. Feel free to save your settings in PuTTY to make it easier to quickly open a serial connection.

Once the code is loaded onto your Arduino, open a serial connection to your board. You should see the line 'PS4 Bluetooth Library Started' printed at the top. If not, try hitting the reset button on the board or reconfigure your serial connection to the correct COM port and baud rate.

STEP 7: Test Your Program


Only after the serial connection is open will the Arduino connect with the Bluetooth dongle. If you want the Arduino to operate without the connection, just comment out the code initializing the Serial object and any print statements. Once the serial connection is open, the Bluetooth dongle goes into search mode, where it looks for a controller to pair with. Hold the Share and PS buttons on your controller to get the LED to flash quickly in a pattern, this means it is searching for a new connection. If the LED is pulsing, it is looking for a console, simply hold the PS button down for 10 seconds to shut the controller down at any time. If your controller connects successfully, you'll notice the LED holding solid. Shake the controller (carefully!) to watch the gyroscopes alter the color of the LEDs, or hit 'Up' on the D-pad to use the right thumbstick and left trigger to cycle through different colors. Pressing 'X' toggles printing the RGB values to the serial terminal.

That's just a taste of what you can do with the controller, now it's up to you to write your own amazing code!

STEP 8: Conclusion

So that's how you connect a PS4 DualShock 4 controller to an Arduino! I tried to cover the basics in case there were readers that were still learning how to use the Arduino, so hopefully it wasn't too bogged down with irrelevant information. Please let me know if there are any errors or changes I should make. Thanks!

22 Comments

I am new to programming, actually new is an understatment. Anyway I was wondering if this could be used to contol a robot, with contols like moving forward and backward and making noise? Any help woukd be appriciated, thanks in advance!

absolute legend looking for advice, il answer that 8 years later sorry i guess, but yes, an arudino is a microcontroller which can be programmed to interact with the connected pieces, such as a motor driver which can be connected to an arudino and start controlling motors

Yes, you would operate off the values given by the controller, turning motors on/off based by buttons/axis values.
Hi, can this work using just the built-in bluetooth chip on an esp32 cam instead?
The entire code is working absolutely fine. The only issue I am facing is that it takes 5 tries to connect the ps4 controller to the Bluetooth dongle. The controller doesn't automatically pair with the Bluetooth dongle, so first using PC I paired the dongle & the controller. Once the pairing is done, I connected the ps4 to the dongle. But sometimes, it gets disconnected all of a sudden. Then to pair it again I need to go to the PC. What could be the reason for this? Is there any possible solution?
Sketch uses 21624 bytes (67%) of program storage space. Maximum is 32256 bytes.
Global variables use 910 bytes (44%) of dynamic memory, leaving 1138 bytes for local variables. Maximum is 2048 bytes.
avrdude: ser_open(): can't set com-state for "\\.\COM4"

is the error I keep on getting.
Any solutions please?

Hello
can I use the same library for PS4 Thrustmaster T.Flight Hotas 4 and If I can't can you guide me to find the library for it

Hi,

very nice tutorial. But it just doesn't work at my arduino.

I have:

- Arduino Uno R3 https://www.amazon.de/gp/product/B00CR1FAS2/ref=oh...

- USB Host Shield https://www.amazon.de/gp/product/B00GHD2ZIE/ref=oh...

- Bluetooth Dongle https://www.amazon.de/gp/product/B007MKMJGO/ref=o...

With the demo in the Bluetooth library and with your led_demo.ino the controller is searching and searching but there is now connection.

The last state from arduino is "Send HID Control Connection Request" and nothing else happens.

Does anyone have an idea what I'm doing wrong? Do I have to 'disconect' my controller from my PS4? (PS4 is not available while testing - of course) Does any of the components won't work with this tutorial?

Instead of using a USB shield and a Bluetooth dongle, wouldn't it be possible to just use an HC-05 Bluetooth module?

hi, I downloaded the led_demo.ino file,(it got a completly different name) and when I try to verify it, came back with mistakes in line 19.

Is it possible that the file get corrupted in any way?

Thanks for any answer,

grtz,

Lex

Can an Bluetooth module such as the HC-06 be used instead of using a USB Shield+Bluetooth dongle?

dont quote me because i dont know if the new tech has changed anything that would let the HC-06 work with it, but i know that it wouldnt work with the older model controllers. in fact only a handfull of dongles would work with the older models. the PS4 has its own dongle made by sony and all ready, ready, already. so thats what im going with and its goig to set you back $25 at best buy

UPDATE

the duleshock 4 wireless adapter that I mentioned earlier will not work with the arduino host shield after all....

Really? Is it only work with the Bluetooth module HC-06 instead of the Bluetooth host shield?

How can I control the arduino with ps4 console and bluetooth module HC-06? Is it just as same as the article that you have written or not?

just want to control 4 servo's do you know who i can do that? wit ps4 controller

already got another question....

in your enxample code you have

#include <spi4teensy3.h>

im still a bit new so is that in referance to the dongle and would it be nessisary if i was using the sony brand wireless usb dongle?

DUDE!!!! thank you and brace yourself for my onslaught of questions.

ok so im building a battle bot and while looking for dongles that would work with the PS3 controller i saw the wireless dongle for the PS4 and googled to see if there was a library yet. your instructable was third on google.

so i read somewhere that the MEGA wont work with the USB host shield 2.0 do you know if that is correct, and with that. do you know if the DUE is compatible?

so far thats all the questions i have but i apologize in advance because im sure ill have more.

Hi there,

Very informative! I am having one issue. I get the line PS4 Bluetooth Library Started and press the share and ps button. The controller flashes but nothing else happens. could you please help me out?

Kind regards.

More Comments