#include <PS4Controller.h>
Moving on to the Arduino setup, we will start by opening a serial connection, so we can output the results of our program.Serial.begin(115200);
Followed by that, we will perform the initialization of the PS4 instance by calling the begin method and passing as input the MAC address that is stored on the controller. It is passed as a string with the default format for representing a Bluetooth MAC address, like the arbitrary example below:"00:11:22:33:ff:ee"
PS4.begin("yourDeviceMAC");
Serial.println("Initialization ready!");
From this point onward the ESP32 should be able to receive a controller connection. So, in the Arduino main loop, we will check when a controller is connected by calling the isConnected method on the PS4 extern variable.if(PS4.isConnected()) {
Serial.println("Controller connected");
}
You can check the full code below. As can be seen, we have added a small 1 second delay between iterations of the main loop, so we are not constantly polling.#include <PS4Controller.h>
void setup()
{
Serial.begin(115200);
PS4.begin("yourDeviceMAC");
Serial.println("Initialization ready!");
}
void loop()
{
if(PS4.isConnected()) {
Serial.println("Controller connected");
}
delay(1000);
}
Figure 4 – Output of the program after a PS4 controller is connected.
References