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

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

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.
| Periphery | ESP GPIO |
|---|---|
| Pot 1 Middle pin | 25 |
| Pot 2 Middle Pin | 26 |
| Joystick X-axis | 14 |
| Joystick Y-axis | 27 |
| Joystick button | 12 |
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:
The output should look like this:

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:
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
- PS3 controller under Ubuntu 2x.04
- PS3 controller on Windows 10
- Bluetooth game controller on Ubuntu 20.04
- Bluetooth game controllers on Windows 10
Sources
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
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
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?