Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.5-flash· GoogleHugging Face LMArena Human Preference 140K

Connect PS3 gamepad to ESP32-S3. Which way is the best?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219-thinking-32k’s answer.
  1. User

    Connect PS3 gamepad to ESP32-S3. Which way is the best?

  2. Gemini Assistant

    Connecting a PS3 gamepad to an ESP32-S3 can be done in two primary ways, leveraging the ESP32-S3's capabilities:

    1. Bluetooth (Wireless) - Generally the best and most common way.
    2. USB (Wired) - A viable alternative, especially if you need reliability or are integrating other USB devices.

    Let's break down each method:


    Method 1: Bluetooth (Wireless - Recommended)

    This is generally considered the "best" way because it leverages the PS3 gamepad's native wireless communication method, offering the most authentic user experience. The ESP32-S3 has built-in Bluetooth Classic (BR/EDR) which is required for the PS3 gamepad.

    Why it's generally the best:

    • Wireless Freedom: No cables tying the gamepad to your project.
    • Native Mode: Uses the gamepad's intended communication method.
    • Dedicated Libraries: There are well-developed libraries that handle the PS3 gamepad's specific Bluetooth implementation.

    Challenges:

    • PS3's Custom Bluetooth: The PS3 gamepad doesn't use standard Bluetooth pairing. It needs to be told the "master" device's Bluetooth MAC address (which will be your ESP32-S3's MAC address). This usually involves a one-time setup step.
    • Power Consumption: The ESP32's Bluetooth radio consumes power, as does the gamepad itself.

    How to do it (using esp32-ps3-controller library):

    1. Prepare your ESP-IDF environment: Ensure you have ESP-IDF installed and configured for your ESP32-S3 board.

    2. Install the Library: The most popular and robust library for this is esp32-ps3-controller (often found on GitHub, e.g., by jvperrin or similar forks). You'll typically add this as a component to your ESP-IDF project.

      cd your_esp_project/components
      git clone https://github.com/jvperrin/esp32-ps3-controller.git ps3_controller
      # Or find a more recent/compatible fork if needed
      
    3. One-Time Pairing Setup (Critical Step):

      • The PS3 controller needs to know the Bluetooth MAC address of your ESP32-S3. You cannot simply "pair" it like a normal Bluetooth device.
      • Get your ESP32-S3's Bluetooth MAC Address: You can print this in your ESP32 code using esp_read_mac(mac, ESP_MAC_BT);
      • Set the Master Address on the PS3 Gamepad:
        • Using a PC: Connect the PS3 gamepad via USB to a computer (Windows, Linux, macOS). Use a tool like ScpToolkit (Windows), sixad (Linux), or a Python script (many available) to set the gamepad's master address to your ESP32-S3's MAC address. This only needs to be done once per gamepad.
        • Using ESP32 (more advanced): Some versions of the esp32-ps3-controller library might have a feature to set the master address, but this often requires the gamepad to be connected via USB to the ESP32 first, which requires the ESP32 to act as a USB Host (see Method 2). The PC method is simpler for initial setup.
    4. ESP32-S3 Code Structure:

      #include <Arduino.h> // If using Arduino as ESP-IDF component
      #include <Ps3Controller.h> // Include the PS3 Controller library
      
      void onConnect() {
        Serial.println("PS3 Controller Connected!");
      }
      
      void onDisconnect() {
        Serial.println("PS3 Controller Disconnected!");
      }
      
      void notify() {
        // This function is called whenever controller data changes
        // You can read button states and joystick values here
        if (Ps3.Ps3Connected) {
          if (Ps3.data.button.up) Serial.println("Up pressed");
          if (Ps3.data.button.down) Serial.println("Down pressed");
          // ... and so on for all buttons (circle, cross, square, triangle, L1, R1, etc.)
      
          // Joystick values (0-255, 127 is center)
          // Ps3.data.analog.stick.lx, Ps3.data.analog.stick.ly (Left Stick X, Y)
          // Ps3.data.analog.stick.rx, Ps3.data.analog.stick.ry (Right Stick X, Y)
          // Ps3.data.analog.button.left, etc. (analog pressure of buttons)
        }
      }
      
      void setup() {
        Serial.begin(115200);
      
        // Initialize the PS3 Controller library
        // Replace with your actual ESP32-S3 Bluetooth MAC address if you set it manually
        Ps3.begin("your_esp32s3_bt_mac_address"); // Optional: set specific BT MAC
      
        Serial.println("Waiting for PS3 Controller to connect...");
      
        // Set callbacks
        Ps3.attach(onConnect);
        Ps3.attachOnDisconnect(onDisconnect);
        Ps3.attachOnNotify(notify); // Call this on every data update
      }
      
      void loop() {
        // Ps3.loop() handles the internal communication and state updates.
        // Make sure to call it regularly, e.g., in your main loop or a FreeRTOS task.
        // If using Arduino framework:
        // Ps3.loop(); // Often handled internally by the library, check specific library docs.
        delay(10); // Small delay to prevent busy-waiting
      }
      
    5. Compile and Upload: Build your project with idf.py build and flash it with idf.py flash.


    Method 2: USB (Wired)

    The ESP32-S3 has a USB OTG (On-The-Go) port, which means it can act as a USB Host. This allows it to connect to and communicate with USB devices like the PS3 gamepad.

    Why you might choose USB:

    • Reliability: Wired connections are generally more robust and less prone to wireless interference.
    • No Pairing Hassle: No MAC address setup needed. Just plug and play (after your code handles it).
    • Power: The ESP32-S3 can potentially power the gamepad (or vice-versa depending on configuration), or the gamepad can draw power directly from the ESP32-S3 while charging.
    • Generic USB HID: If you plan to connect other USB HID devices (keyboards, mice, other gamepads), building a USB Host HID driver for the ESP32-S3 could be a more generic solution.

    Challenges:

    • Requires Cable: Not wireless, which might defeat the purpose for some applications.
    • USB Host Complexity: Implementing a full USB Host stack, enumerating devices, and parsing HID reports can be more complex than using a pre-made Bluetooth library.
    • Cabling: You'll need a USB-C OTG cable or adapter for your ESP32-S3, and a standard USB-A to Mini-B cable for the PS3 gamepad.

    How to do it (using ESP-IDF's USB Host capabilities):

    1. Hardware Connection:

      • Connect a USB-C OTG adapter to your ESP32-S3's USB-C port (the one that also does serial communication).
      • Connect a standard USB-A to Mini-B cable from the OTG adapter to the PS3 gamepad.
    2. ESP-IDF Configuration (sdkconfig):

      • In menuconfig (idf.py menuconfig):
        • Navigate to Component config -> USB Host stack.
        • Enable USB Host stack.
        • Enable USB Host HID Class driver (or similar, depending on ESP-IDF version). This is crucial for gamepads.
        • Ensure USB Serial/JTAG Controller is configured correctly (usually this is the same port as USB OTG).
    3. ESP32-S3 Code Structure:

      This will be significantly more involved than the Bluetooth method, as you'll be working closer to the USB Host stack.

      • Initialize USB Host: Set up the USB Host driver.
      • Enumerate Devices: Detect when a device is plugged in, identify it as a PS3 gamepad (via Vendor ID and Product ID, e.g., Sony's 0x054C and 0x0268 for PS3 Controller).
      • Open HID Interface: Claim the HID interface.
      • Read HID Reports: The gamepad sends data in "HID reports." You'll need to read these reports (typically interrupt transfers) and parse the byte array to extract button states, joystick values, etc. The format of these reports is specific to the PS3 gamepad and may require reverse-engineering or finding documentation.
      #include <stdio.h>
      #include "esp_log.h"
      #include "freertos/FreeRTOS.h"
      #include "freertos/task.h"
      #include "usb/usb_host.h"
      #include "usb/hid_host.h" // For HID class specific operations
      
      static const char *TAG = "PS3_USB";
      
      // PS3 Gamepad Vendor and Product ID (standard)
      #define PS3_VID 0x054C
      #define PS3_PID 0x0268 // Or 0x0268, depending on model/firmware
      
      // Example callback for HID report
      void ps3_hid_report_callback(uint8_t *data, size_t len) {
          // Parse the HID report data here
          // The PS3 controller sends a specific report format.
          // You'll need to look up documentation for the PS3 HID report structure.
          // For example, byte 2 could be button states, bytes 3-6 could be joystick axes.
      
          // Example: Print raw data for debugging
          printf("HID Report (Len: %d): ", len);
          for (int i = 0; i < len; i++) {
              printf("%02X ", data[i]);
          }
          printf("\n");
      
          // Example: Check a specific button (highly simplified, actual bytes vary)
          if (len > 2 && (data[2] & 0x01)) { // This is just an example, actual bit depends on mapping
              ESP_LOGI(TAG, "Cross button pressed (example)");
          }
      }
      
      void usb_host_task(void *arg) {
          usb_host_config_t host_config = {
              .skip_phy_setup = false,
              .controller_listener_config = {
                  .callback = NULL, // No general callback for controller events
                  .arg = NULL,
              },
          };
          ESP_ERROR_CHECK(usb_host_install(&host_config));
          ESP_LOGI(TAG, "USB Host installed");
      
          // Waiting for device to be connected
          while (1) {
              ESP_LOGI(TAG, "Waiting for USB device to be connected...");
              // USB Host library provides functions to enumerate devices
              // This is where you'd iterate connected devices, check VID/PID
              // and if it's a PS3 controller, then set up the HID driver.
      
              // Simplified: Assuming hid_host_install handles connection detection and callback
              hid_host_config_t hid_config = {
                  .dev_desc_match_req = NULL, // Match any HID device
                  .max_event_callbacks = 1,
                  .event_callbacks = {
                      .input_report = ps3_hid_report_callback, // Our report callback
                  },
              };
      
              // This is a simplified call; you might need to enumerate devices
              // first and pass the device handle to a more specific hid_host_install.
              // Refer to ESP-IDF USB Host HID example for full flow.
              if (hid_host_install(&hid_config) == ESP_OK) {
                  ESP_LOGI(TAG, "HID Host driver installed.");
                  // Loop to process USB events and receive reports
                  while (1) {
                      vTaskDelay(pdMS_TO_TICKS(10)); // Keep task alive, HID driver handles events
                      // Real applications might have a loop for specific device state
                  }
              } else {
                  ESP_LOGE(TAG, "Failed to install HID Host driver.");
                  // Uninstall and try again
                  hid_host_uninstall();
                  vTaskDelay(pdMS_TO_TICKS(1000));
              }
          }
      }
      
      void app_main(void) {
          xTaskCreate(usb_host_task, "usb_host", 4096, NULL, 5, NULL);
      }
      

      Note: The USB Host code above is a highly simplified conceptual example. The actual implementation requires careful study of the ESP-IDF USB Host and HID Host examples, handling device enumeration, configuration descriptors, interface claiming, and precise parsing of the PS3 controller's specific HID report format.


    Conclusion: Which way is the best?

    For most users and applications, especially those prioritizing ease of use and the intended wireless experience:

    • Go with Method 1: Bluetooth using the esp32-ps3-controller library.
      • Pros: Wireless, good library support, authentic feel.
      • Cons: One-time pairing setup can be tricky for beginners, specific to PS3 controllers.

    Choose Method 2: USB if:

    • You absolutely require a wired, highly reliable connection.
    • You're already familiar with USB Host programming or need to connect other USB HID devices.
    • You're up for a more involved coding challenge to gain lower-level control.

    Recommendation: Start with the Bluetooth method. It's the most widely used for PS3 gamepads with ESP32 and has good community support.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.