• EUR€
  • £GBP
  • $USD

ESP32 PS3 Controller: Setting player LED

DFRobot May 22 2020 1357

In this tutorial we will learn how to set the player LEDs of the PS3 controller, using the ESP32 and the Arduino core. The tests from this tutorial were done using an ESP32 development board from DFRobot.


In this tutorial we will learn how to set the player LEDs of the PS3 controller, using the ESP32 and the Arduino core.

We will be using this library to a allow a PS3 controller to connect to the ESP32 and then to set the LEDs. For an introductory tutorial on the library, please check here.

Note that the PS3 controller has 4 player LEDs, which means that, in theory, we could represent 15 players (2^4 – 1) if we used a binary approach for combining the LEDs. Nonetheless, this is not how the player number is displayed.

In the controller, the LEDs are numbered from 1 to 4 and the player number is equal to the sum of those numbers. For example, players from 1 to 4 correspond to the LED 1 to 4 being on, respectively. Player 5 corresponds to LEDs 1 and 4 turned on (4+1 = 5), and so on until 10, which corresponds to all LEDs turned on (1+2+3+4 = 10).

The tests from this tutorial were done using an ESP32 development board from DFRobot.

The Code

As usual, we will start by importing the Ps3Controller.h library, so we have access to the Ps3 extern variable. This variable will allow us to interact with the controller.

#include <Ps3Controller.h>

We will also declare a global variable called playerLed that will contain the current player number. We will initialize it to zero, which basically means all the LEDs will be turned off.

int playerLed = 0;

Moving on to the Arduino setup function, we will start by opening a serial connection, so we can output the results of our program.


Then we will call the begin method on the Ps3 extern variable to perform the initialization of the Bluetooth layer of the ESP32 and to make it ready to receive a controller connection.

As input, this method receives the Bluetooth address stored on the PS3 controller. Please check this previous tutorial for a more detailed explanation on why we need to know this value and how to obtain it.


As mentioned in previous tutorials, calling the begin method only takes care of the mentioned initialization. At this stage, the controller is not yet connected to the ESP32 (it might not even be powered on).

The full setup function can be seen below.

void setup()
    Serial.println("Initialization finished.");

Moving on to the Arduino main loop, the first thing we will do is checking if the controller is connected with a call to the isConnected method on the Ps3 extern variable.

This method takes no arguments and returns a Boolean value that indicates if a PS3 controller is currently connected to the ESP32 or not.

   // setting the player LED

Inside the previous condition block (in case the controller is connected) we will take care of setting the player LEDs. In each iteration of the main loop we will set the LEDs to a new value.

First, we will increment the current player number variable we declared globally at the beginning of our program. The maximum number of players that can be represented by the LEDs is 10, so we will have a rollover at 11 to repeat the cycle again.

To implement an increment operation with a rollover we simply need to increment our variable by 1 and then we get the reminder of the division by the rollover value.

So, in our case, we want to sum 1 to the variable playerLed and then we apply the modulus operator to the result to get the reminder of the division by 11.

This means that, when the playerLed variable hits the value 11, the reminder of dividing by 11 is 0, which resets our counter.

playerLed = (playerLed +1)%11;

We will also print to the serial port the current player number so we can compare it against what we are seeing in the controller.

Serial.print("Player LED value:");

Then, to set the player LED in the controller, we simply need to call the setPlayer method on the Ps3 extern variable, passing a input the value to set.

The complete loop can be seen below. As can be seen, we have added a small 2 seconds delay between each iteration of the loop.

void loop()
      playerLed = (playerLed +1)%11;
      Serial.print("Player LED value:");
The final code can be seen below.

Testing the code

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

After that, connect the controller by pressing the PS button. After that, you should start seeing the number of the current player being printed to the serial monitor, as shown in figure 1, and the corresponding sequence of LEDs in the controller should be turned on.

Figure 1 – Output of the program on the Arduino IDE serial monitor.

You can check what to expect on the PS3 controller in the video below.

Original Link: https://techtutorialsx.com/2020/03/14/esp32-ps3-controller-setting-player-led/