The PyBricks team released a PyBricks Blocks version that allows you to create drag and drop programs for LEGO smart hubs, such as the SPIKE Prime or Robot Inventor hub. In this post, we will show how to use a Bluetooth gamepad with PyBricks Block Coding. In the past, you had to use Python to create lagless remote control with gamepads. Since Pybricks block coding, that limitation is gone: any kid can tweak remote control programs now. The only things you need are an LMS-ESP32 board and any Bluetooth gamepad controller.
Remote control needs to be lagless. The Robot Inventor implementation by LEGO required a PC in the middle and introduced a lot of lag. Pybricks can do brick-to-brick Bluetooth messaging, but that’s slightly laggy too. That’s why we built a Bluepad version for our LMS-ESP32 expansion board that can talk to Pybricks in real-time. You’ll have very precise control over your MINDSTORMS robot. To make this work, you need PyBricks-compatible Lego smart bricks and our LMS-ESP32 board.
Step 1: Flash your LMS-ESP32 board with BluePad32 LPF2 for PyBricks
For this tutorial, we assume you are the proud owner of an LMS-ESP32 board. By default, it comes with MicroPython firmware. For this tutorial, we need special firmware that connects to gamepads. You can simply flash it with a browser and a USB wire.
To install the new firmware, link your LMS-ESP32 module to your computer over USB and visit our firmware webpage. Choose the BluePad32 LPF2 for PyBricks projects option, select the correct serial port, and initiate the firmware installation.
Step 2: Flash Pybricks onto your LEGO SPIKE or MINDSTORMS hub
Pybricks offers an easier and more powerful block language than LEGO. You can easily revert to LEGO firmware later. To flash Pybricks, go to beta.pybricks.com and click the gear icon. You are now able to code your hub with Python. For this tutorial, we will be using the new Pybricks block language, however. This is a paid add-on. To use the block language, you need to unlock the blocks by purchasing a license with PyBricks. Follow the instructions. After completing this process, connect the LMS-ESP32 to your LEGO hub.
Step 3: Install PUPRemote and Bluepad libraries in PyBricks
In the PyBricks programming environment, you need to upload a few libraries.
- Download the PUPRemote GitHub repository by clicking the green Code button and selecting ‘download zip’. If you are fluent with git, you can of course clone the repository.
- Unzip and import three files into pybricks. Use the import butten above the file list to import.
The files you need are in the ‘src’ directory. They are:
pupremote.pyby hovering over the filename and clicking the pencil icon.
Step 4: Take the BluePad Blocks template as a starting point
bp_template.py file from the zip into PyBricks. It is in the ‘examples’ directory. You should see the following in your PyBricks IDE.
controller variable initialization according to your controller. If you use a Nintendo controller, change this to
"nintendo". You can also use “ps4” or “xbox”.
Step 5: Pair your gamepad to the LMS-ESP32 module
Connect your Gamepad to the LMS-ESP32 by pressing the pairing button. This differs per type of gamepad. For the PS4 gamepad, you have the press the PS and share buttons at the same time.
Step 6: Run test code to see if Pybricks receives gamepad data
When you now run the code is the
bp_template file, you should see data printed in the terminal in the lower part of the screen.
Here you see on one line the positions of the left and right joysticks and on the next line the Buttons and the DPad keys that are pressed. In this case the ‘A’ button and the ‘L’ DPad..
Step 7: Driving NeoPixels or Servo motors
Stap 8: Have Fun with gamepad Pybricks block coding
Now it is time to build your favorite robot and start programming it with the Blocks PyBricks. Equip your robot with NeoPixels or Servo Motors and connect a Bluetooth gamepad. Take the template or one of the example files as a starting point. For background information and a description of all pybricks gamepad blocks, we made a separate post.
Watch the Pybricks gamepad Blocks in action
You can see the usage of Blocks PyBricks in the video tutorial. All the code is available in our PUPRemote Github repository.