ESP32: Connecting a PS3 controller

The objective of this tutorial is to explain how to connect a PS3 controller to an ESP32, using the Arduino core and this library. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

The objective of this tutorial is to explain how to connect a PS3 controller to an ESP32, using the Arduino core and this library. The connection between the devices will be established using the Bluetooth protocol.

If you are looking for a similar tutorial but using a PS4 controller, please go here.

For instructions on how to install the library, please check here. In short, the library is available for installation in the Arduino Library Manager by searching by “PS3 Controller Host” [1].

Before we get started, it’s important to understand that a controller paired with a PS3 console has the Bluetooth MAC address of the console stored. Consequently, this is the only device to which the controller will connect [1].

So, in order to be able to connect the controller to the ESP32, we need to find out what is the address stored in the controller [1]. To do it, we can use this tool (called SixaxisPairTool), which allows both to find what is the address stored in the controller and also change it.

Then we can choose to use the address already stored in the controller (the library allows to use a custom address) or to change the stored address to the one of the ESP32. In my case, I’ve chosen to use the address already stored in the controller.

For a detailed tutorial on how to use the SixaxisPairTool, please check the “Preparing the controller” section of this previous tutorial. That tutorial explains how to connect a PS4 controller to the ESP32, which needs the same step of figuring out the Bluetooth address stored in the controller. The SixaxisPairTool works exactly the same for both the PS3 and PS4 controllers.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

We will start by including the installed library. This will make available an extern variable of class Ps3Controller. This extern variable is called Ps3 .

#include <Ps3Controller.h>

After that we are going to move on to the Arduino setup, where we are going to start by opening a serial connection.

Serial.begin(115200);

Then we are going to call the begin method on our Ps3 extern variable. As input of this method we need to pass the Bluetooth address stored in the controller, as a string.

The MAC needs to follow the standard representation: each byte in hexadecimal, separated by colons, such as the example below:

"00:11:22:33:ff:ee"

This method call returns a Boolean value indicating is the procedure was successful or not. For simplicity, we won’t be doing any error handling but, in a real application scenario, you should do it.

Take in consideration that this method call doesn’t connect the ESP32 to the controller. Instead, it is responsible for initializing the ESP32 Bluetooth layer and setting up the services to be ready to listen for an incoming PS3 controller connection.

Ps3.begin("YourDeviceAddress");

After this point the ESP32 should be ready to accept the connection of a controller. The full Arduino setup can be seen below,

void setup()
{
    Serial.begin(115200);

    Ps3.begin("YourDeviceAddress");

    Serial.println("Initialization finished.");
}

We will write the rest of the code in the Arduino main loop. We will periodically check if a controller is connected by calling the isConnected method on our Ps3 extern variable.

This method takes no arguments and returns a Boolean value indicating if a controller is connected (true) or not (false).

if (Ps3.isConnected()){
    Serial.println("Controller connected!");
}

Note that we are following this simpler polling approach on this introductory tutorial but the library supports registering a callback to notify when a connection is received (the method to be used to register the callback is called attachOnConnect).

The full loop can be seen below. We have added a small 1 second delay so we are not constantly polling.

void loop()
{
  if (Ps3.isConnected()){
    Serial.println("Controller connected!");
  }

  delay(1000);
}

The final code can be seen below.

#include <Ps3Controller.h>

void setup()
{
    Serial.begin(115200);

    Ps3.begin("YourDeviceAddress");

    Serial.println("Initialization finished.");
}

void loop()
{
  if (Ps3.isConnected()){
    Serial.println("Controller connected!");
  }

  delay(1000);
}

Testing the code

To test code, simply compile it and upload it to your device. After the procedure finishes, open the Arduino IDE serial monitor. You should see a “Initialization finished” message, indicating that the initialization was done.

After that, turn on the PS3 controller by clicking the PS button. You should get a “Controller connected!” message printed periodically to the monitor, as shown in figure 1.

Output of the program.
Figure 1 – Output of the program.

References

[1] https://github.com/jvpernis/esp32-ps3

Leave a Reply