Controlling A Servo Motor using a PS4 Controller and and ESP32


I recently found the the Freenove ESP32 breakout board, which provides a very simple way to interface an ESP32 Dev Module with sensors and motors.  The reason I especially like this breakout board is you can power it via a DC adapter.  This provides power to the 5V pins of the breakout board and allows for up to 3A of current draw, which means you can pretty easily power multiple servo motors straight from the board.  The breakout board can be found here:

Freenove Esp32 Breakout Board: https://geni.us/HWuH

Just a quick note: the breakout board only works with the Freenove ESP32 dev modules, which can be found here:

Esp32 Wroom: https://geni.us/QBiKeMe

Esp32-S3 Wroom: https://geni.us/nwPyZ

Connecting the PS4 Controller to the ESP32 is surprisingly easy, but note that the library currently doesn't support the S3.  Also note that some basic Arduino knowledge is required, however there are a ton of great tutorials online, including the one that Freenove provides with their Esp32 boards.

Here are the steps to connect the PS4 Controller to the ESP32 Board:

  1. Set Up the PS4 Controller:

    • Install the PS4Controller library in the Arduino IDE.
    • Upload the PS4Data example to the Esp32 Module using the Arduino IDE. The MAC address of the ESP32 will be displayed in the Serial Monitor
    • Plug the PS4 Controller into your computer using a usb cable.
    • Install the "SixAxis Pair Tool" app and use it to change the MAC address of the PS4 controller to match the ESP32. (You literally just need to copy and paste the MAC address into the text box and hit "update"). You can easily find the app just by googling the name.
  2. Pairing the Controller:

    • Turn on the PS4 controller, and it will automatically connect to the ESP32.  The Serial Monitor should show the data coming in from the PS4 controller.
  3. Connect the Servo Motor:

    • Power the setup with a 12V DC adapter. Here's one that would work well: https://geni.us/uTS2fO5
    • Attach the servo motor directly to the Freenove ESP32 breakout board at pin 18 (for my demo code below)
  4. Control the Servo:

    • Use the ESP32 servo library to connect and control the servo motor.
    • Write a simple code to map the left joystick of the PS4 controller to control the servo motor's angle. I've provided a really basic code below that runs the servo on pin 18.

That's it! Your servo motor is now controllable via the PS4 controller.  Now you can level up and do something cool with it. Make sure to tag me on instagram if you make something epic.

If you want to level it up a bit more, you can power the board with a high power USB-C bank, and a USB-C to 12V adapter, so you can make mobile projects as well.  Here are the ones I used:

USB-C Power Bank: https://geni.us/LGJay

USB-C to 12V adapter cable: https://geni.us/AV9JsO

 

Here is some basic code to get you started:

#include <PS4Controller.h>

#include <ESP32Servo.h>

Servo myservo;  // create servo object to control a servo
int servoPin = 18;      // GPIO pin used to connect the servo control (digital out)

int val=90;    // variable to read the value from the analog pin

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

  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  myservo.setPeriodHertz(50);// Standard 50hz servo
  myservo.attach(servoPin, 500, 2400);  

  Serial.println("Ready.");

}

void loop() {
  if (PS4.isConnected()) {
     
    val = PS4.LStickY();            // read the up/down value of the Left Stick
    val = map(val, -127, 127, 0, 180);     // scale it to use it with the servo
    Serial.println(val);  //Print the angle to the Serial Monitor
    myservo.write(val);  //write the value to the servo
    
   
    delay(10);
  }
}