1 month ago

Hack Lego Boost with Raspberry Pi

The Lego Boost robotics kit is designed to make learning to build and program robots fun and easy. Let’s hack it with some Python

The External Motor is attached to Port D of the Move Hub. The Colour sensor is attached to Port C of the Move Hub 

The Lego Boost is designed to be run from an app on an Android or iPad tablet, using a graphics programming language not unlike Scratch. It makes a good job of this and is easy for kids to pick up, but these sorts of languages have their limitations. They can be inflexible and difficult to read, especially as the code gets bigger. By using Python, much more complex programs can created, many that are not possible with graphics-based code. So, give your Boost a boost by letting Python do the controlling.

This article was written by Mike Cook and first appeared in The MagPi issue 80. Subscribe to our newsletter to get a free digital edition of The MagPi delivered to your inbox every month. Or subscribe in print to get The MagPi magazine delivered to your door. Mike is a veteran magazine author from the old days, writer of the Body Build series, plus co-author of Raspberry Pi for Dummies, Raspberry Pi Projects, and Raspberry Pi Projects for Dummies.

See also:

Three Lego Boost Raspberry Pi projects

What is Lego’s Boost?

Unlike the previous Mindstorms robotics systems from Lego, the Boost system has no controlling brick to run code – instead, instructions representing the program are sent one at a time directly, over Bluetooth, in real-time to the Move Hub. This has built-in motors, LEDs, tilt sensor, and push-button. You can also plug into the Hub a smaller motor and a distance sensor or colour sensor. Also, Mindstorms uses Lego Technic beams for most constructions, known as a studless system, whereas the Boost uses the more conventional, studful brick form of construction. See Figure 1.

Lego Boost fig 1

Figure 1 Studful bricks on the left, studless beams on the right

Lego’s software

The standard Lego software provides a good structured learning system where the user builds a bit, programs it, and then moves on to building more. The big project is Vernie the robot and we’d recommend you do this first because the parts are already bagged up to make construction of this project easy. Vernie is built up in stages and the code you are asked to run at each stage gets increasingly complex. You can’t progress to the next level without trying, or pretending to try, the current phase. We recommend you try this first, not least for the firmware updates it can offer to your Hub – see Figure 2.

Lego Boost fig 2

Figure 2 The Lego Boost program environment running on an iPad or Android

Hack a Lego boost: kit you’ll need

Set up a Lego Boost with Raspberry Pi

There are a few libraries for connecting Boost to Python – see a summary from Jorge Pe, author of the original library. He hacked the system by using a Bluetooth protocol sniffer. These days, Lego has released the protocol documentation, but it’s a heavy read. We tried a few libraries and it seems the most developed and actively supported one is pylgbst from Andrey Pokhilko. In fact, during the course of writing this, a new version was released. It is still not finished but, with a little tweaking, it is good.

Lego Boost fig 3

Figure 3 The top of the minimum demo build

Lego Boost fig 4

Figure 4 The front of the minimum demo build

Lego Boost fig 5

Figure 5 The end of the minimum demo build

Installing the pylgbst library

First off, we need to install some dependences. Open a Terminal window and enter:

sudo apt-get install python3-pip
sudo pip3 install pexpect
sudo pip3 install pygatt

Next, get the library:

wget https://github.com/undera/pylgbst/archive/master.zip
unzip master.zip
cd pylgbst-master
sudo python3 setup.py install

Fixing the colours

There seems to be an error in one of the files defining the colours for the RGB LED. Fix this by opening a Terminal and entering:

sudo nano /usr/local/lib/python3.5/dist-packages/pylgbst/constants.py

Scroll down using the cursor keys until you see:


…and change it to:


To save the changes, press CTRL+X, then Y, and ENTER.


The bulk of the documentation is in the README.md file and we found that not all of it works with the Raspberry Pi. But, it does contain some short single-function examples. Basically, to get the Move Hub to do things, you send it a command, as you might expect; however, to get information back from the Move Hub, you have to subscribe to the appropriate stream. When you do, you specify the name of a function that will be called when there is new data from whatever sensor you subscribed from. You must unsubscribe from the sensor before your Python program finishes.

Demo example

In the library’s examples folder is a demo.py program; unfortunately, this will not run without error as is – it seems to be more designed for fault finding on the Bluetooth connections than showing things working. We have rewritten this to add extra functions and correct the syntax errors – see the mike’s_demo.py listing. While it will run with any Move Hub, incorporated into a model or not, we found it best to use a basic setup that didn’t run away off the desk when the motors moved. Also, adding wheels to the motors made it easy to turn them when testing the motor read angle function.

Preparing the Hub

Refer to Figures 3 to 5 with these instructions. Get the Move Hub and fix on top of it the sensor (part 6182145) and the motor (part 6181852) and plug them into ports C and D. It doesn’t matter which port you use, the software will find them. Next, put a red axle stub (part 4142865) in each of the two Hub motors, and the external motor. Fit a wheel (part 4662228) on each of the Move Hub’s axles, and a wheel and tyre (parts 6092256 and 4619323) onto the external motor. Finally, add two long beams (part 4508661) under the hub to stop the wheels from moving it.
Running Mike’s demo

Make sure Bluetooth is turned on by clicking on the Bluetooth icon on the top menu bar, run the Python code, and immediately push the green button on the Move Hub. The LED next to this will start flashing to indicate that the Bluetooth Hub is looking for something to pair with. This will turn a steady blue when connected and the demo will then start, printing out instructions on the screen. After the battery’s voltage and current have been read out, the program will ask you to press the green button; you need to do this for the program to advance.

More demo instructions

After the green button is pressed, the distance sensor is tested; this repeats for 100 measurements. You can change the distance sensor reading by waving your hand in front of it: it returns a value in inches, to the nearest inch, for distances above one inch; for smaller distances, it returns finer values. The program converts this into millimetres for display. The tilt sensor tests require you to pick up and turn the Hub, and the motor angle read requires you to turn the motor wheels by hand. When no movement of the wheels has been detected over the last five seconds, the demo ends.

Further examples

The library’s examples folder also contains other examples. Unfortunately, they don’t work on the Raspberry Pi due to the method used to include packages. The from . import * line, which means from the current directory import everything, returns an error message saying the parent directory is missing. This might have something to do with the author not using a Raspberry Pi in his testing. Note, there is another problem but this is down to Lego, as it also happens on the tablets running the official software. Colours set or reported as cyan are actually green, so we have to cope with this in our code.

Move Hub connection problems

Here at the Bakery we have two Raspberry Pi boards: one with the current Raspbian release, and the other the previous one. Oddly enough, we found that running normally was fine with the older release, but with the newer one the code needed to be run in supervisor mode in order for it to connect. This can be done by typing sudo IDLE3 in a Terminal window. If you want to permanently run IDLE in the supervisor mode from the desktop, go to the IDLE 3 menu entry, and right-click. Then choose the Properties menu, and select the Desktop Entry tab. Now edit the command box to put ‘sudo ’ in front of what is there. The same applies for Thonny – see Figure 6.

Lego Boost fig 6a

Figure 6a Changing the command so that IDLE will run in supervisor mode

fig 6b

Figure 6a Changing the command so that Thonny will run in supervisor mode

Lego Boost conclusion

Now we’ve got the basics going, we’re all ready to do some interesting stuff. In The MagPi magazine issue 81, we’ll make a game showing how we can use our Lego Boost with the Pygame framework.