With just a few extra pieces of hardware, upgrade your Arduino to receive input from a Sony DualShock 4 controller via a Bluetooth connection.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
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!