Gamepad remote control with PyBricks Block coding

Ste7an

Updated on:

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.

  1. 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.
  2. 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:
    • blocks_bp.py
    • pupremote_hub.py
    • bluepad.py
  3. Rename pupremote_hub.py to pupremote.py by hovering over the filename and clicking the pencil icon.

Step 4: Take the BluePad Blocks template as a starting point

Upload the bp_template.py file from the zip into PyBricks. It is in the ‘examples’ directory. You should see the following in your PyBricks IDE.

Template for BluePad in Blocks PyBricks IDE.

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

-231,-278,0,21
A,L

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

The BluePad library also allows you to drive NeoPixels and Servo motors connected to the LMS-ESP32. We provide two example templates for doing so: bp_neo.py and bp_servo.py.

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.

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

Leave a Reply

Item added to cart.
0 items - 0.00