ESP32 Bluetooth Gamepad

Last Updated on 9 February 2024 by Suffocation

Sometimes a remote control or a joystick is needed to test a project or simply to have fun. The following describes how to connect an ESP32 via Bluetooth. A small analogue joystick and two potentiometers are connected for control.

Facts

Required hardware

  • ESP32 BLE Board (ESP32, ESP32C3, ESP32S3)
  • Sensors, Joysticks, Potentiometers …
  • Cable for wiring
  • (Optional) Breadboard
  • (Optional) I²C Expansion Board
  • (Optional) USB dongle for the computer

Required Software

  • Arduino GUI
  • https://github.com/h2zero/NimBLE-Arduino
  • https://github.com/lemmingDev/ESP32-BLE-Gamepad
  • (Optional) Custom Keyboard Library
  • (Optional for Linux) bluez

Areas of application

  • Remote control
  • RC control
  • Joystick for games

Views

Joystick test rig – wiring will be described in more detail below

Programming

Libraries

The following two libraries are required. These can be done by downloading the zip from Git or directly in the Arduino GUI. The third is not necessary for Bluetooth operation but is used in my examples.

NimBLE-Arduino

ESP32 BLE Gamepad

CustomKeyboard

Connection test

The first test is intended to show whether a connection to the PC is possible. To do this, I plugged a USB dongle into the PC and grabbed an ESP32 Wroom. The test programme is run at the ESP32 BLE Gamepad It comes with a library and is called Gamepad. The programme simulates pressing and releasing key 5 (or 4) at half-second intervals, as well as setting the axis values.

Compile and upload the sketch for the Arduino. How the controller in Windows or in Linux can be found in the linked posts.

This is how the simulator should look in the respective operating system.

Periphery test

To preliminarily check if the potentiometers and joystick are functioning, I have created a peripheral test that simply queries the corresponding ports and outputs them to the console. It is based on my CustomKeyboard Library. My experimental setup is as follows.

Experimental setup

Connection diagram

Pin assignment

Here are the pins used. These are basically freely selectable. Analogue input pins are required for the pots and the axes, and since ADC1 does not function when WiFi or BLE is active, I have used pins from ADC2.

PeripheryESP GPIO
Pot 1 Middle pin25
Pot 2 Middle Pin26
Joystick X-axis14
Joystick Y-axis27
Joystick button12
Pin configuration on the ESP32 Wroom

ATTENTION Don’t forget to connect the other two pins on the potentiometers to the supply voltage and earth, and remember to connect the joystick to the power supply as well.

Source code

The source code for the test can be found at the following link:

https://github.com/sfambach/esp32/tree/6e8ab27afbf1394bf51c2333cddb67b81479e9fd/project/ESP32BLE-Joystick/src/ESP32-BLE-Gamepad_InputTest

The output should look like this:

Peripheral test output

All together

There's not much more left to do than to combine the two tests and transmit the values read by the physics department via Bluetooth.

The example code can be found here:

https://github.com/sfambach/esp32/tree/e442fb75d588c93a59c3316685182d3358aa51df/project/ESP32BLE-Joystick/src/ESP32-BLE-Gamepad_FinalTest

Three axes X, Y, RX and RY, as well as button 0 are transmitted.

The whole thing can be tested again in Windows in the „Game Controller Setup“ and under Linux with the jstest.gtk tool (see chapter Connection test).

Problems

Not yet

Miscellaneous

Connect controllers on Windows

Integrating controllers under Linux

Conclusion

As always, step by step towards the goal. First, establish the Bluetooth functionality, then connect the peripherals, and finally combine both. This made the implementation easy. The Bluetooth functionality of the ESP appears mature. The poor analogue/digital conversion must be improved through averaging, but it should be sufficient for playing around.

Related Posts

Sources

NimBLE-Arduino

NimBLE-Arduino New User Guide

NimBLE-Arduino API

https://github.com/lemmingDev/ESP32-BLE-Gamepad

https://en.wikipedia.org/wiki/List_of_Bluetooth_profiles

https://en.wikipedia.org/wiki/USB_human_interface_device_class

https://www.usb.org/sites/default/files/documents/hid1_11.pdf

https://github.com/nilutpolkashyap/esp32_microros_joystick/blob/main/cmd_vel_publisher_udp/cmd_vel_publisher_udp.ino

2 thoughts on “ESP32 BlueTooth Gamepad

  1. Hello Karl-Heinz
    In the current case, I haven't set anything specific. I'm using Ubuntu, but I've also had problems with Bluetooth and other Linux distributions like Proxmox. Unfortunately, I haven't found a solution.

    LG Stefan

  2. Good day, ,
    I have a question. On Windows and Android I can easily pair and connect. On Linux /Bacotera/Recalbox/Retropie I can't connect. Or rather, it's constantly disconnected. So, it's not usable. Is there a trick for Linux? Could you help me with that?

Leave a Reply

Your email address will not be published. Required fields are marked *