Web
Analytics
 
 

ESP32 + PS3 Controller + Robotic Arm = Awesome!

Updated: Nov 22, 2020

Now the esp32 is full of awesome features, it has wifi and Bluetooth onboard, is capable of BLE 4.0, and even has it's own wireless communication protocol capable of hundreds of feet, without even using wifi or Bluetooth! One of it's coolest party tricks is its ability to connect to a standard PlayStation 3 controller, without any additional hardware! Adding a sophisticated, wireless, affordable, feature-rich controller to your next robotics project has never been easier! In this tutorial, we'll walk you through each step you'll need to get your esp32 communicating with a PS3 controller. As always, we've got a full video tutorial to go along with this post here. Check it out below! Next, we'll use that controller to control a robotic arm! Your first step is choosing a controller.





Step One: The PS3 Controller

We have some really solid, and affordable generic PS3 controllers that we've tested with our robots, and when paired with an esp32 Wroom 32U with an external antenna, we've seen a range of over 500 ft! That being said, any generic, or official PS3 controller will work. While you may not want to use a generic controller to play a real PS3, they make for an awesome robot controller!

Step Two: Finding the MAC Address

Now every Bluetooth device out there, from headphones, mice and keyboards, even speakers, have what's known as a MAC Address, and we'll need to find the MAC address of our particular controller to pair it with our ESP32. The good news is, it's a really easy process. There are a few ways to pull this off, but I've found the easiest is to download a program called the "Sixaxis Pair Tool".


You can download the Sixaxis Pair Tool Here!


Once you have the program installed, you'll want to open it up. Once you do, you'll be greeted with a screen that looks something like this.



Without the controller plugged in, you'll see the message, "No device Found...". Grab the USB cable for your PS3 controller and connect it to your computer.





Once you have your controller connected, if the program was able to recognize your controller, you should see the MAC address of your controller displayed, labeled as "Current Master:". One of the cool things about this program is the ability to change the controller MAC Address, this means that having multiple controllers, controlling multiple robots around each other is not only possible but really easy to set up! Once you have the MAC Address of your controller, copy it or write it down. Next, we need the code for our ESP32. We'll be using an awesome library developed using the Arduino Core, which will make communication really easy to get going.


Step Three: The ESP32 PS3 Library

This library is awesome! It is really easy to understand, and really feature-rich. Not only can you use every button on the PS3 controller, but you can also read the analog values of the buttons, record press, and release functions, read the battery level, and even accessing the internal accelerometer! In this tutorial, we'll be focusing on the joystick data. Before we can do that, we've got to install the library! The link to the library file can be found right here.


https://www.arduinolibraries.info/libraries/ps3-controller-host


You'll want to download the zip file, add the zip to your libraries and head to the examples. The first piece of code we'll look at is the "Demo" code. Once you have the library installed, head to your "examples" tab, scroll down until you see PS3 Controller Host, then select Ps3 Demo.


Once you've got your Demo sket open, you'll want to scroll all the way near the bottom, and locate the "void Setup ( )"

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

    Ps3.attach(notify);
    Ps3.attachOnConnect(onConnect);
    Ps3.begin("01:02:03:04:05:06");

    Serial.println("Ready.");
}

The most important line here is Ps3.begin("01:02:03:04:05:06"), if you have a different MAC address from my generic controller, you'll have to type your MAC address in this line of code, otherwise, you won't be able to pair your controller.


Step 4: Pairing your controller

Once you have your MAC Address added to the Demo sketch, compile and upload the sketch and open up the Serial Monitor. (Be sure to set the Baud Rate to 115200).


If you were able to upload your code successfully, then it's time to pair your controller! All you'll need to do here is press the circular middle button on your controller! On a genuine controller, this button has the PlayStation logo on it, but lots of generic controllers have a different graphic. Once you press the button, you'll see the pairing lights on the top of the controller starts flashing, and after a few seconds, the player one light, will turn on, and you're paired! Now head back to the Serial Monitor, so we can make sure everything is working properly!



The first thing that will pop up in your Serial Monitor will be the message "Connected", then a message on the status of the battery, but after that, the example code will start cycling through lighting up different combinations of the player LEDs. Next, try pressing some of the buttons! Once you begin, you'll see the corresponding message displayed in the Serial Monitor.

That's it! If you've made it to this point, then you're ready to use this data to control all kinds of awesome robotics projects! For this tutorial here, I decided to use this example code to breathe some new life into a classic, the MeArm robotic arm!


Step 5: Controlling the MeArm


Now, don't get me wrong, there are certainly bigger, stronger robotic arms out there. In fact, I've got an awesome ESP32 Robotic Arm Controller board, meant for a much larger robotic arm project I'm working on, in production right now I can't wait to try out! That being said, I'll always have a soft spot for the MeArm. It's a lot of our first robotic arm build, it's open-source, affordable, and for years now it's been helping teach thousands of people all over the world about servo control. In this example, I'm using a classic MeArm 1.0. I have a few of these guys available in the shop, but if you have a laser cutter or a 3D printer, this example code can be used for any servo based robotic arm. Feel free to hack this example code to do all kinds of crazy things!


These days, going from schematic to PCB has never been easier. So in order to keep our wiring nice and neat, I'm going to design a quick circuit board to organize our wires for us!

I decided to add in a Barrel Jack for external power, some 18650s to power our servos without the use of a power supply, and a buck converter to keep safe voltages. For the data wires of our servos, I went with pins 21,22,27 and 28.


Here's the final board. This isn't the same PCB I use in the video, I decided to go ahead and post the most recent version of the ESP32 Robotic Arm Driver Board here. One thing that's nice about this board, is that I included the holes for a MeArm, a large servo based robotic arm, as well as the popular mg90s pan-tilt camera mounts.


https://easyeda.com/matt_9455/esp32_robot_arm_ps3