This library will allow you to turn your Arduino into a controller fully compatible with PS4!
IMPORTANT: You must install the PS4Arduino-AVR core for the library to work. Instructions here.
Once installed, you need to select "____ as PS4 controller" as the board.
Really simple! This library includes 6 functions to cover all inputs:
-
PS4controller.begin(): call this to initialize the library. -
PS4controller.setButton(button, state): sets the state of a button. It takes two parameters: the button, which can be one of the following:TRIANGLECIRCLECROSSSQUARESHAREOPTIONSPSL1L2L3R1R2R3TOUCHPAD
and the state, which can be either
true/1orfalse/0. -
PS4controller.setDpad(direction): sets the direction of the dpad. Takes 1 parameter, which can be:DPAD_NDPAD_NEDPAD_EDPAD_SEDPAD_SDPAD_SWDPAD_WDPAD_NWDPAD_RELEASED
(the letters stand for the cardinal directions, e.g. NE is the top and right buttons pressed simultaneously).
-
PS4controller.setTrigger(trigger, value): this is the function to use if you want to press L2/R2 but not fully (useful for throttle/braking). Takes 2 parameters: the trigger (LEFT/RIGHT) and a value from 0 to 255 (0 is fully unpressed and 255 is fully pressed). -
PS4controller.setJoystick(joystick, axis, value): sets the value of a joystick axis. Takes three parameters: the joystick (LEFT/RIGHT), the axis (X/Y) and a value from 0 to 255. 128 is the middle. -
PS4controller.maintainConnection(): You must call this frequently (e.g. once per loop) to prevent the board from being disconnected from your PS4.
And that's about it! You can take a look at this example for further reference.
- Arduino Leonardo
- Arduino Leonardo ETH
- Arduino Micro
- Arduino Esplora
Check out the tutorial for a step by step guide for people newer to the Arduino ecosystem. It will guide you through getting the required software to flash your Arduino and control your PS4.
This error occurs if you are trying to use the PS4Arduino library but have not use the PS4Arduino-ARV to change the board type. Instructions here.
This can occur after you have used the board "_____ as PS4 controller" (PS4Arduino-AVR) as it shows up to the computer as a game controller rather than an Arduino.
To fix this, start uploading your sketch and press the reset button on the Arduino board. More details above under 4. Iterate above.