├── IMG_20201130_125630_1.jpg ├── IMG_20201130_125801_1.jpg ├── IMG_20201130_125918_1.jpg ├── LICENSE ├── README.md ├── analog_joystick.jpeg ├── schematics.png └── src ├── ble_advertising.py ├── joystick.py ├── joystick_ble.py └── main.py /IMG_20201130_125630_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insighio/esp32-microgamepad-ble/658381bc263c03bc18ef986967cf909e24398ea6/IMG_20201130_125630_1.jpg -------------------------------------------------------------------------------- /IMG_20201130_125801_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insighio/esp32-microgamepad-ble/658381bc263c03bc18ef986967cf909e24398ea6/IMG_20201130_125801_1.jpg -------------------------------------------------------------------------------- /IMG_20201130_125918_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insighio/esp32-microgamepad-ble/658381bc263c03bc18ef986967cf909e24398ea6/IMG_20201130_125918_1.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 insigh.io 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esp32-microgamepad-ble 2 | Dual analog joystick on ESP32 over BLE (Nordic UART Service - NUS) using micropython 3 | 4 | The purpose of this project is to create a dual-joystick analog gamepad that will be transmitting over BLE the values using an ESP32 device. 5 | It is an ideal controller for robot projects. Joystick 1 is used for steering and Joystick 2 is used for controlling camera movement. 6 | 7 | # Components 8 | * ESP32 with micropython firmware 9 | * ideally use one of the latest firmwares as BLE was recently supported. Tested with: esp32-idf4-20200902-v1.13.bin 10 | * Breadboard / Prototyping board 11 | * Resistor 10κΩ x2 12 | * Analog Joystick 5-Pin x2 13 | 14 | ![img](https://github.com/insighio/esp32-microgamepad-ble/blob/main/analog_joystick.jpeg) 15 | 16 | # Wiring 17 | Allthough the analog joysticks label the input as 5V, they perfectly work with 3.3V which will be used in our schematic. Also J1, J2 will be abreviations for Joystick 1 and Joystick 2. 18 | 19 | To enable the digital input of the button click when the joystick is pressed, a resistor of 10kΩ is connected to the 3.3V of the ESP32 one the one hand, and to the respective GPIO where the SW pins of the joystics are connected (GPIO25, GPIO26) 20 | 21 | | Component PIN | ESP PIN | 22 | | ------------- | ------- | 23 | | J1 5V | 3.3V | 24 | | J2 5V | 3.3V | 25 | | Resistor 1 | 3.3V | 26 | | Resistor 2 | 3.3V | 27 | | J2 GND | GND | 28 | | J1 GND | GND | 29 | | J1 VRx | GPIO34 | 30 | | J1 VRy | GPIO35 | 31 | | J1 SW | GPIO25 | 32 | | J2 VRx | GPIO32 | 33 | | J2 VRy | GPIO33 | 34 | | J2 SW | GPIO26 | 35 | | Resistor 1 | GPIO25 | 36 | | Resistor 2 | GPIO26 | 37 | 38 | ![complete schematic](https://github.com/insighio/esp32-microgamepad-ble/blob/main/schematics.png) 39 | 40 | # On Raspberri Pi 41 | An interesting alternative on how to read the analog values of the Joysticks is the use of a Raspberry Pi with an MCP3008 analog-to-digital converter. 42 | 43 | link: https://tutorials-raspberrypi.com/raspberry-pi-joystick-with-mcp3008/ 44 | 45 | # references 46 | * https://medium.com/@codeyourventurefree/diy-iot-joytick-to-play-online-claw-machines-175206679fc9 47 | * https://esp32.com/viewtopic.php?t=14803 48 | * https://docs.micropython.org/en/latest/library/ubluetooth.html 49 | * https://github.com/micropython/micropython/tree/master/examples/bluetooth 50 | -------------------------------------------------------------------------------- /analog_joystick.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insighio/esp32-microgamepad-ble/658381bc263c03bc18ef986967cf909e24398ea6/analog_joystick.jpeg -------------------------------------------------------------------------------- /schematics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insighio/esp32-microgamepad-ble/658381bc263c03bc18ef986967cf909e24398ea6/schematics.png -------------------------------------------------------------------------------- /src/ble_advertising.py: -------------------------------------------------------------------------------- 1 | 2 | from micropython import const 3 | import struct 4 | import ubluetooth 5 | 6 | # Advertising payloads are repeated packets of the following form: 7 | # 1 byte data length (N + 1) 8 | # 1 byte type (see constants below) 9 | # N bytes type-specific data 10 | 11 | _ADV_TYPE_FLAGS = const(0x01) 12 | _ADV_TYPE_NAME = const(0x09) 13 | _ADV_TYPE_UUID16_COMPLETE = const(0x3) 14 | _ADV_TYPE_UUID32_COMPLETE = const(0x5) 15 | _ADV_TYPE_UUID128_COMPLETE = const(0x7) 16 | _ADV_TYPE_UUID16_MORE = const(0x2) 17 | _ADV_TYPE_UUID32_MORE = const(0x4) 18 | _ADV_TYPE_UUID128_MORE = const(0x6) 19 | _ADV_TYPE_APPEARANCE = const(0x19) 20 | 21 | 22 | # Generate a payload to be passed to gap_advertise(adv_data=...). 23 | def advertising_payload(limited_disc=False, br_edr=False, name=None, services=None, appearance=0): 24 | payload = bytearray() 25 | 26 | def _append(adv_type, value): 27 | nonlocal payload 28 | payload += struct.pack("BB", len(value) + 1, adv_type) + value 29 | 30 | _append( 31 | _ADV_TYPE_FLAGS, 32 | struct.pack("B", (0x01 if limited_disc else 0x02) + (0x18 if br_edr else 0x04)), 33 | ) 34 | 35 | if name: 36 | _append(_ADV_TYPE_NAME, name) 37 | 38 | if services: 39 | for uuid in services: 40 | b = bytes(uuid) 41 | if len(b) == 2: 42 | _append(_ADV_TYPE_UUID16_COMPLETE, b) 43 | elif len(b) == 4: 44 | _append(_ADV_TYPE_UUID32_COMPLETE, b) 45 | elif len(b) == 16: 46 | _append(_ADV_TYPE_UUID128_COMPLETE, b) 47 | 48 | # See org.ubluetooth.characteristic.gap.appearance.xml 49 | if appearance: 50 | _append(_ADV_TYPE_APPEARANCE, struct.pack("