$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32DFRobotEnglish

ESP32 PS4 Controller: Setting the LED RGB value

DFRobot May 22 2020 1034

In this tutorial we will learn how to use the ESP32 to set the color of the RGB LED that is located in the back of a PS4 controller. We will be 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

In this tutorial we will learn how to use the ESP32 to set the color of the RGB LED that is located in the back of a PS4 controller. We will be using the Arduino core and this library.

For an introductory tutorial on how to connect a PS4 controller to an ESP32 with the mentioned library, please check here.

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 importing the PS4Controller.h library, so we have access to the PS4 extern variable. We will make use of this variable to get the ESP32 ready to receive a controller connection and then to be able to set the color of the controller back LED.

#include <PS4Controller.h>

After that, in the Arduino setup function, we will initialize a serial connection. That way, we will be able to print the results from our program.

Serial.begin(115200);

Then we will take care of initializing the Bluetooth layer of the ESP32 and make it ready to receive a PS4 controller connection. As mentioned before, we will do this by using the PS4 extern variable.

More specifically, we will call the begin method on the PS4 extern variable, passing as input the Bluetooth address stored on the controller. For a tutorial explaining what is this Bluetooth address and how to obtain it, please check here.

Note that the begin method returns a Boolean value indicating if the initialization was successful or not, which we will use for a very simple error checking where we will print a message to the user in case something went wrong.

if(!PS4.begin("01:01:01:01:01:01")){
      Serial.println("Initialization failed");
      return;
}
   
Serial.println("Initialization finished.");

The complete setup function can be seen below.

void setup()
{
  Serial.begin(115200);
 
 
  if(!PS4.begin("01:01:01:01:01:01")){
      Serial.println("Initialization failed");
      return;
  }
   
  Serial.println("Initialization finished.");
  
}

We will write the rest of our code in the Arduino main loop function. There, we will periodically change the value of the RGB LED in case the controller is currently connected.

We can check if the controller is connected with a call to the isConnected method on the PS4 extern variable.

Note that in this previous tutorial we already saw how to handle the connection of a controller using events, which is much more efficient than polling. Nonetheless, for this simple program, we will use the polling approach to make the code shorter.

if (PS4.isConnected()){
    // set the RGB LED    
}

To set the RGB value of the LED we will call the setLed method, passing as input the Red, the Green and the Blue values, by this order. These three values are passed as integers but they need to be between 0 and 255.

We will set the RGB values randomly, so the LED changes between different colors. To get a random number we can use the Arduino random function.

As input of the random function, we pass an integer with the upper bound of the random value, meaning that we will get as output a number between 0 and this upper bound. Note that the upper bound is excluded from the generated numbers so we need to pass the value 256 to obtain a random number between 0 and 255.

PS4.setLed(random(256),random(256), random(256));

When we call this method, we are just setting some internal values in the PS4 extern variable, which are not immediately sent to the controller.

So, to send the previous command to the controller, we need to call the sendToController method. This method takes no arguments and returns void.

PS4.sendToController();

The full loop can be seen below. We have added a 1 second delay between each iteration.

void loop()
{
  if(PS4.isConnected()){
     
    PS4.setLed(random(256),random(256), random(256));
    PS4.sendToController();
     
  }
 
  delay(1000);
   
}

The final code can be seen below.

#include <PS4Controller.h>
 
void setup()
{
  Serial.begin(115200);
 
 
  if(!PS4.begin("01:01:01:01:01:01")){
      Serial.println("Initialization failed");
      return;
  }
   
  Serial.println("Initialization finished.");
  
}
 
void loop()
{
  if(PS4.isConnected()){
     
    PS4.setLed(random(256),random(256), random(256));
    PS4.sendToController();
     
  }
 
  delay(1000);
   
}

Testing the code

To test the code, simply compile it and upload it to your device, using the Arduino IDE. When the procedure finishes, open the IDE serial monitor and wait for the “Initialization finished” message to get printed. Once it does, connect the PS4 controller.

You should get a result similar to the one presented on the video below, which shows the controller RGB back led changing its color randomly every second.


Original Link: https://techtutorialsx.com/2020/03/15/esp32-ps4-controller-setting-the-led-rgb-value/

REVIEW