Pybricks and Powered Up: A Recipe For Bluetooth Py

I recently saw a video on the RacingBrick YouTube channel called “Revolutionising LEGO RC control with Pybricks and Xbox.” It showed me a simple solution for the biggest problem with LEGO Powered Up: controlling cars.

I'm old-school. I played with my first remote-controlled car around 1980, and I controlled it with a remote with two joysticks. My second one had a remote with a joystick and a steering wheel. LEGO has released various remotes with similar controls over the years (watch this space for a full article!), but the Powered Up system is controlled from a phone or tablet.

I had this remote control car in the early 80s

That means sliding your fingers over a piece of glass, and as others have pointed out in reviews: that's just no good. There’s no haptic feedback from the glass. If your finger slides into the wrong part of the glass suddenly you're no longer steering your car but blowing the horn instead. So you need to look at your fingers while also looking at your model.

So how does Pybricks solve this problem? I’d seen the name before but never really looked into it. But let me tell you a story about LEGO programming before we get to the recipe for Bluetooth Py.


Python and Mindstorms

Let's start with a little history. Don't worry, it'll be quick. 

Mindstorms RCX brick, image from Wikipedia

Around 1985 MIT and LEGO started collaborating on a robotics programme. The first product was the Technic Control Centre, followed by Mindstorms in 1998. The first generation of Mindstorms, the RCX, while mindblowing for its time, was fairly limited in what it could do, so fans created alternative programming languages such as NQC (Not Quite C) and LeJOS. That continued with the next two generations of Mindstorms, the NXT and EV3.

Around 1990, a Dutch developer started working on a new programming language. He decided to call it Python, after Monty Python. It slowly gained popularity and eventually became the second most popular programming language. In 2014 an Australian developer created MicroPython, a compact version of Python, meant to run on a Pyboard microcomputer (not to be confused with the Raspberry Pi!).

Pybricks logo

Then in April 2019, these two stories crossed paths with the release of MicroPython for LEGO Mindstorms EV3. This is also where the story of Pybricks starts.

Laurens Valk is a robotics engineer and the author of multiple LEGO Mindstorms books. David Lechner is the developer of ev3dev, a Linux-based alternative operating system for Mindstorms EV3. Both are life-long LEGO and Mindstorms fans, and when they saw LEGO’s (then new) Powered Up system, they realised it had more potential than what LEGO was doing.

While the hardware in the various hubs is almost the same, the play experience is fragmented, with different apps and controls, and very little integration. They decided to join forces to create Pybricks as the one OS to rule them all.

Family Portrait of programmable hubs: City, Technic, Boost, Mindstorms, Spike Prime, Spike Essential

They had to start with a lot of reverse engineering. In the past, LEGO released schematics and source code for the NXT and EV3 for hackers to play with, but they didn't do that for any of the Powered Up hubs. So, like the original RCX hackers, they had to reverse-engineer everything. They started with the MicroPython on the EV3 and then moved on to the Powered Up hardware, gradually making it easier to use for beginning users without losing sight of experienced ones. Thanks to browser support for Bluetooth you can now program your Powered Up hubs directly from a browser.


The Apple of My Py

Let's take a look at what Pybricks is and how it's different from the LEGO programming and control of the Powered Up Hubs (City, Technic, Boost, Mindstorms and Spike).

When you control a Powered Up hub with your phone, the program runs on your phone and it 'talks' to the hub using Bluetooth. There's another program running on the hub that translates the commands from the phone into signals on the output ports to run motors, and that sends signals from sensors back to the phone.

What Pybricks does is replace the program on the hub with something smarter that doesn't need a phone to tell it what to do. Instead, it can make its own decisions, or it can pair with the Powered Up remote. So now you can control all those Technic cars, like the 4x4 X-treme Off-Roader, the Top Gear rally car and the Off-Road Buggy, using the same remote as the City trains—a nice physical remote that you don't have to look at to know where the buttons are.

Now, as nice as this physical remote is, it doesn't have as many buttons as, say, an Xbox controller. The Pybricks team apparently thought the same, so they recently invited Xbox controllers to the party! In a few minutes you can make your Technic Hub listen to an Xbox Bluetooth controller. Piece of cake... (or pie, I should say).

Now that we’ve gotten through the story, let me show you how to set up Pybricks remote control with this recipe.


Recipe for Bluetooth Py

Feeds 1 AFOL. Cooking time 10 minutes.

Blueberry Pie by KosBrick on Flickr

Ingredients

  • 1 Powered Up Technic set, like 42099

  • 1 laptop with Bluetooth

  • 1 Powered Up controller, 88010

  • A working internet connection

  • Some fresh batteries for the hub

Steps

  • Preheat the oven to 180C/ 350F (nah, LEGO and heat don't mix well—skip this step)

  • Make sure the hub is turned off.

  • Turn on the laptop and visit https://code.pybricks.com.

  • Click "Install Pybricks Firmware" and follow these other steps:

    • Select your hub.

    • Accept the license.

    • Name your hub.

  • Press the button on the hub and hold it.

  • When the hub starts blinking pink, click "Install".

  • In the pop-up click "LEGO Bootloader", then click "Pair".

  • The firmware should be installing on your hub. Let it run for a minute until complete.

At this point, you already have control over your hub, but keyboard control is not what we were after so there are a few more steps:

  • Click the file icon, then the + icon to create a new program.

  • Make sure the Python code option on the right is selected, name the program 42099 and turn off the template. Hit Create.

  • In a second window, go to https://pybricks.com/projects/sets/technic/42099-off-roader/powered-up-remote/.

  • Copy the Python program.

  • Go back to the Pybricks Code window and paste the code you just copied (where it says "Write your program here").

  • Click the Bluetooth button, select your hub and click "Pair".

  • Click the Play button to run the program. You'll see "Downloading" for a few seconds.

  • Press the green button on the remote to turn it on. Its light should go blue when the hub connects to it.

And that's it! Your Bluetooth py should be done and ready to use! You now control the Technic 4x4 Off-roader with the remote control and you can turn off your laptop. If you want to play with it next time, just turn on the hub, turn on the remote, and press the green button on the remote to start the program. Easy as pie!


Time To Get Cooking

So what else can Pybricks do for you? A lot actually. If you followed the recipe, you now have a very simple program to just do forward/reverse and left/right. Now that you have that program, you can add functions. You can give it two speeds, or give it an emergency brake. All it takes is a few lines of code. And if writing code isn't your thing, Pybricks also supports "block coding".

Pybricks block coding example

This makes it much easier for a lot of people to "write" programs. Enabling block coding does require a small donation though - a lot of time and effort obviously went into building the block coding.

Ball-Balancing-Bot using only Mindstorms Robot Inventor parts and Pybricks

Not every model is a car that needs a remote though. Many GBC modules run on Powered Up hubs, and using Pybricks means you can put the program on the hub. That means your GBC module can keep itself entertained for hours. Huw at Brickset has written an article about how he does just that.

And we're not done yet. Because the program that runs on the hub doesn't need to talk to your phone all day long—it can run much quicker. That means you can do things that would be impossible with phone control, like this self-balancing robot.

Hubs can't just communicate with phones and remotes, they can talk to each other. For example, you can have two robots reacting to each other. Or you can make the hub in a train ask the hub at the station to set the points for a specific platform.


As I mentioned at the top, I’ve only recently started looking into Pybricks, but I’ve certainly got a taste for it now. I love what I’ve seen so far, and I can’t wait to find out what else I can use it for, in my train layouts and in other MOCs.

If you have a Powered Up hub, give Pybricks a go. The possibilities are endless! And in the meantime, enjoy some pie on Pi Day as you learn more about Pybricks!


What other LEGO coding languages should we feature? Let us know in the comments below.

Do you want to help BrickNerd continue publishing articles like this one? Become a top patron like Charlie Stephens, Marc & Liz Puleo, Paige Mueller, Rob Klingberg from Brickstuff, John & Joshua Hanlon from Beyond the Brick, Megan Lum, Andy Price, Lukas Kurth from StoneWars, Wayne Tyler, LeAnna Taylor, Monica Innis, Dan Church, and Roxanne Baxter to show your support, get early access, exclusive swag and more.