Building Rasp Pi into Mindstorms projects with BrickPi

Programmed Stuff

To get access to the robot and thus start scripts that you have programmed yourself, you should use the headless capabilities of the Rasp Pi and log onto the BrickPi via SSH or VNC.

If have built your own robot, it will still have to receive instructions by means of software. The whole point of an on-board processor is to write programs that use the Rasp Pi to control the robot's motors and sensors. You will, therefore, need to set up the Raspberry Pi to support your favorite programming language. Listing 1 shows how to download and install the necessary tools for using Python.

Listing 1

Setting up BrickPi for Python

$ git clone
$ cd BrickPi_Python
$ sudo apt-get install python-setuptools
$ sudo python install

If you write your own Python script for the robot, you have to import the library for controlling the BrickPi functions with the command from BrickPi import * and also initialize the serial port with the command BrickPiSetup(). Because the robot almost always uses servo motors, you will need to call the following command for each of the motors:

BrickPi.MotorEnable[Port] = 1

Depending on which of the four motor ports of the BrickPi the cable of each servo motor has been plugged into, you will have to replace the Port placeholder with one of the PORT_A to PORT_D identifiers.

In the last initialization step, you need to inform the BrickPi library of the types of sensors used and define any ports that are connected to sensors. For an ultrasound sensor plugged into port 1, the corresponding line will read:


All in all, five ports are reserved for sensors. In contrast to the motors, sensors do not receive letters as identifiers. Instead, they are numbered in ascending order. Each sensor type is associated with a predefined constant. For example, the constant indicating a color sensor is TYPE_SENSOR_COLOR_FULL. To finish the sensor initialization process, invoke the BrickPiSetupSensors() command.

Communicating with the individual robot parts is easy while the program is running. A servo motor is controlled via

BrickPi.MotorSpeed[Port] = rate_of_speed

For the rate of speed, you can enter a whole number between -255 and +255, with 0 bringing the motor to a stop. Positive and negative values designate the direction for turning. To make the robot turn, you can slow down the motor of one wheel to zero while the other motor continues to turn the other wheel.

For interrogating sensors, you can call BrickPiUpdateValues() to obtain the current values. By subsequently invoking BrickPi.Sensor[Port], you obtain a numeric value for a particular sensor. For example, an ultrasound sensor will return the distance to the nearest object.

Using the C language, you can program the BrickPi according to the same scheme. At the beginning of the program, import the required header files (Listing 2). The C names for constants and methods in the BrickPi library correspond exactly to those for the Python version, except that you call the methods according to the rules of C syntax. Fans of Scratch will find an implementation that makes graphical building blocks available for building the control functions.

Listing 2

Including Header Files in C

#include <wiringPi.h>
#include "BrickPi.h"


If your Mindstorms set was purchased before September 2013, you will easily see many advantages of the BrickPi over the NXT. The BrickPi has considerably more computing power, more RAM, and more mass storage, without any dependency on an external PC. You can easily integrate every type of software and hardware available for the Rasp Pi into the robot. Moreover, the BrickPi opens up the possibility of installing not only Mindstorms sensors but also the less expensive Arduino sensors.

The BrickPi also offers advantages for those who possess the new EV3 system. Granted, the EV itself already has a clock speed of over 300MHz, 64MB of RAM, a USB host mode, and an operating system based on Linux. However, the Rasp Pi offers even more options.

If you still do not have a Mindstorms set, you won't have to go to the expense of buying an expensive new starter package. You can build out the BrickPi with separately purchased sensors, motors, and Lego Technic building blocks. Despite its benefits, the BrickPi is not a particularly easy system for beginners; you will fare better if you have some basic knowledge of Mindstorms and Linux.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content