ESP32 PS4 Controller: Set LED flashing

In this tutorial we are going to learn how to set the PS4 controller back led to flash with a given on and off rate, using the ESP32 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 are going to learn how to set the PS4 controller back led to flash with a given on and off rate, using the ESP32 and this library. We will be using the Arduino core.

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 functionalities we need to set the PS4 controller LED to flash.

#include <PS4Controller.h>

Moving on to the Arduino setup, we will open a serial connection to output the results of our program.

Serial.begin(115200);

After that we are going to initialize the Bluetooth layer to be ready to receive an incoming controller connection. For a detailed explanation about the function that will be called please read this introductory tutorial.

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

Serial.println("Initialization finished.");

To finalize the Arduino setup, we will set a callback function that will be executed when a controller connection is received. We will call that function onConnect and check its implementation later.

The complete Arduino setup function, already containing the call to the attachOnConnect method to setup the previous callback, 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.");

  PS4.attachOnConnect(onConnect);

}

To finalize we will check the implementation of the onConnect callback function, where we will send the instructions to the controller to start flashing the LED.

Note that this function follows a pre-defined signature: it receives no arguments and returns void.

void onConnect() {
   // function implementation
}

Before we set the flashing rate, we will define the color of the LED. You can check this previous tutorial where we have covered the led color functionality.

In short, to set the color of the led, we call the setLed method on our PS4 extern variable, passing as inputs the Red, Green and Blue (in this order). Each argument is an integer beween 0 and 255.

We will set the LED color to red, which corresponds to R = 255, G = 0 and B = 0.

PS4.setLed(255, 0, 0);

Now, to set the flash rate of the led, we simply need to call the setFlashRate method on the PS4 extern variable. As input, this method receives the on time and the off time, both as integers, specified in milliseconds.

We will set the led to be on for 1 second and off also for 1 second.

PS4.setFlashRate(1000, 1000);

To send the instructions to the controller, we need to call the sendToController method, which takes no arguments.

PS4.sendToController();

The full callback function implementation can be seen below.

void onConnect() {
 
  if (PS4.isConnected()) {

    Serial.println("Connected.");

    PS4.setLed(255, 0, 0);
    PS4.setFlashRate(1000, 1000);

    PS4.sendToController();
  }
}

The complete code can be seen below. As shown, since we don’t need to make use of the Arduino main loop, we are simply deleting the corresponding FreeRTOS task.

#include <PS4Controller.h>

void onConnect() {
 
  if (PS4.isConnected()) {

    Serial.println("Connected.");

    PS4.setLed(255, 0, 0);
    PS4.setFlashRate(1000, 1000);

    PS4.sendToController();
  }
}

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


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

  Serial.println("Initialization finished.");

  PS4.attachOnConnect(onConnect);

}

void loop()
{
  vTaskDelete(NULL);
}

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 be printed.

Then, connect the PS4 controller. You should get a message indicating that the controller was connected to the ESP32 and then the back LED of the controller should start flashing with an on and off pattern of 1 second, like shown in the video below.

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading