Raspberry Pi Documentation

Build HAT

Introducing the Build HAT

The Raspberry Pi Build HAT is an add-on board that connects to the 40-pin GPIO header of your Raspberry Pi, which was designed in collaboration with LEGO® Education to make it easy to control LEGO® Technic™ motors and sensors with Raspberry Pi computers.

build hat
Note
A full list of supported devices can be found in the Device Compatibility section.

It provides four connectors for LEGO® Technic™ motors and sensors from the SPIKE™ Portfolio. The available sensors include a distance sensor, a colour sensor, and a versatile force sensor. The angular motors come in a range of sizes and include integrated encoders that can be queried to find their position.

The Build HAT fits all Raspberry Pi computers with a 40-pin GPIO header, including — with the addition of a ribbon cable or other extension device — Raspberry Pi 400. Connected LEGO® Technic™ devices can easily be controlled in Python, alongside standard Raspberry Pi accessories such as a camera module.

The Raspberry Pi Build HAT power supply (PSU), which is available separately, is designed to power both the Build HAT and Raspberry Pi computer along with all connected LEGO® Technic™ devices.

psu

The LEGO® Education SPIKE™ Prime Set 45678 and SPIKE™ Prime Expansion Set 45681, available separately from LEGO® Education resellers, include a collection of useful elements supported by the Build HAT.

Note
The HAT works with all 40-pin GPIO Raspberry Pi boards, including Raspberry Pi 4 and Raspberry Pi Zero. With the addition of a ribbon cable or other extension device, it can also be used with Raspberry Pi 400.
  • Controls up to 4 LEGO® Technic™ motors and sensors included in the SPIKE™ Portfolio

  • Easy-to-use Python library to control your LEGO® Technic™ devices

  • Fits onto any Raspberry Pi computer with a 40-pin GPIO header

  • Onboard RP2040 microcontroller manages low-level control of LEGO® Technic™ devices

  • External 8V PSU available separately to power both Build HAT and Raspberry Pi

Note

The Build HAT can not power the Rapsberry Pi 400 as it does not support being powered via the GPIO headers.

Preparing your Build HAT

Note
Before starting to work with your Raspberry Pi Build HAT you should set up your Raspberry Pi, install the latest version of the operating system using Raspberry Pi Imager.

Attach 9mm spacers to the bottom of the board. Seat the Raspberry Pi Build HAT onto your Raspberry Pi. Make sure you put it on the right way up. Unlike other HATs, all the components are on the bottom, leaving room for a breadboard or LEGO® elements on top.

fitting build hat

Access the GPIO Pins

If you want to access the GPIO pins of the Raspberry Pi, you can add an optional tall header and use 15 mm spacers.

tall headers

The following pins are used by the Build HAT itself and you should not connect anything to them.

GPIO Use Status

GPIO0/1

ID prom

GPIO4

Reset

GPIO14

Tx

GPIO15

Rx

GPIO16

RTS

unused

GPIO17

CTS

unused

Setting up your Raspberry Pi

Once the Raspberry Pi has booted, open the Raspberry Pi Configuration tool by clicking on the Raspberry Menu button and then selecting “Preferences” and then “Raspberry Pi Configuration”.

Click on the “interfaces” tab and adjust the Serial settings as shown below:

setting up

Using your Raspberry Pi Headless

If you are running your Raspberry Pi headless and using raspi-config, select “Interface Options” from the first menu.

raspi config 1

Then “P6 Serial Port”.

raspi config 2

Disable the serial console:

raspi config 3

And enable the serial port hardware.

raspi config 4

The final settings should look like this.

raspi config 5

You will need to reboot at this point if you have made any changes.

Powering the Build HAT

Connect an external power supply — the official Raspberry Pi Build HAT power supply is recommended — however any reliable +8V±10% power supply capable of supplying 48W via a DC 5521 centre positive barrel connector (5.5mm × 2.1mm × 11mm) will power the Build HAT. You don’t need to connect an additional USB power supply to the Raspberry Pi as well, unless you are using a Raspberry Pi 400.

Note

The Build HAT can not power the Raspberry Pi 400 as it does not support being powered via the GPIO headers.

powering build hat
Note

The LEGO® Technic™ motors are very powerful; so to drive them you’ll need an external 8V power supply. If you want to read from motor encoders and the SPIKE™ force sensor, you can power your Raspberry Pi and Build HAT the usual way, via your Raspberry Pi’s USB power socket. The SPIKE™ colour and distance sensors, like the motors, require an external power supply.

Installing the Software

Install the Build HAT Python library. Open a Terminal window and type,

$ pip3 install buildhat

For more information about the Build HAT Python Library see ReadTheDocs.

Motors

There are a number of motors that work with the Build HAT.

Connecting a Motor

Connect a motor to port A on the Build HAT. The LPF2 connectors need to be inserted the correct way up. If the connector doesn’t slide in easily, rotate by 180 degrees and try again.

connect motor

Working with Motors

Start the Thonny IDE. Add the program code below:

from buildhat import Motor

motor_a = Motor('A')

motor_a.run_for_seconds(5)

Run the program by clicking the play/run button. If this is the first time you’re running a Build HAT program since the Raspberry Pi has booted, there will be a few seconds pause while the firmware is copied across to the board. You should see the red LED extinguish and the green LED illuminate. Subsequent executions of a Python program will not require this pause.

blinking light

Your motor should turn clockwise for 5 seconds.

turning motor

Change the final line of your program and re-run.

motor_a.run_for_seconds(5, speed=50)

The motor should now turn faster. Make another change:

motor_a.run_for_seconds(5, speed=-50)

The motor should turn in the opposite (anti-clockwise) direction

Create a new program by clicking on the plus button in Thonny. Add the code below:

from buildhat import Motor

motor_a = Motor('A')

While True:
    print("Position: ", motor_a.get_aposition())

Run the program. Grab the motor and turn the shaft. You should see the numbers printed in the Thonny REPL changing.

Sensors

There is a large range of sensors that work with the Build HAT.

Working with Sensors

Connect a Colour sensor to port B on the Build HAT, and a Force sensor to port C.

Note
If you’re not intending to drive a motor, then you don’t need an external power supply and you can use a standard USB power supply for your Raspberry Pi.

Create another new program:

from signal import pause
from buildhat import ForceSensor, ColorSensor

button = ForceSensor('C')
cs = ColorSensor('B')

def handle_pressed():
    cs.on()
    print(c.get_color())

def handle_released():
    cs.off()

Run it and hold a coloured object (LEGO® elements are ideal) in front of the colour sensor and press the Force sensor plunger. The sensor’s LED should switch on and the name of the closest colour should be displayed in the thonny REPL.

Further Resources

You can download documentation on the,

and full details of the Python Library documentation can also be found on ReadTheDocs.

You can also follow along with projects from the Raspberry Pi Foundation,

Device Compatibility

The Build HAT library supports all the LEGO® Technic™ devices included in the SPIKE™ Portfolio, along with those from the LEGO® Mindstorms Robot Inventor kit and other devices that use a PoweredUp connector.

Important
The product code for the SPIKE™ Prime Expansion Set that includes the Maker Plate is 45681. The original Expansion Set is 45680 and does not include the Maker Plate.
Description Colour LEGO Item Number Supported in FW Supported in Python Alt Number BrickLink Available In Set Numbers Class Type Device ID

Large Angular Motor

White/Cyan

45602

Yes

Yes

45602

Link

SPIKE Prime Set, SPIKE Prime Expansion Set

45678, 45680

Motor

Active

30

Medium Angular Motor

White/Cyan

45603

Yes

Yes

45603

Link

SPIKE Prime Set

45678

Motor

Active

31

Medium Angular Motor

White/Grey

6299646, 6359216

Yes

Yes

436655

Link

Mindstorms Robot Inventor

51515

Motor

Active

4B

Small Angular Motor

White/Cyan

Yes

Yes

SPIKE Essentials Set

Motor

Active

41

Light/Colour sensor

White/Black

6217705

Yes

Yes

Link

SPIKE Prime Set, SPIKE Prime Expansion Set, Mindstorms Robot Inventor, SPIKE Essentials

45678, 45680, 51515

ColorSensor

Active

3D

Distance Sensor

White/Black

6302968

Yes

Yes

Link

SPIKE Prime Set, Mindstorms Robot Inventor

45678, 51515

DistanceSensor

Active

3E

System medium motor

White/Grey

6138854

Yes

Yes

Wedo 2.0, LEGO Ideas Piano, App controlled Batmobile

76112

Passive

1

Force Sensor

White/Black

6254354

Yes

Yes

45606

Link

SPIKE Prime Set

45678

ForceSensor

Active

3F

3×3 LED

White/Cyan

Yes

Yes

SPIKE Essentials

Active

40

System train motor

Black

88011

Yes

Yes

28740, 88011-1

Link

Cargo Train, Disney Train and Station, Passenger Train

Passive

2

PoweredUp LED lights

Black

88005

Yes

Link

Passive

8

Medium linear motor

White/Grey

88008

Yes

Yes

26913, 88008-1

Link

Boost, Droid Commander

Motor

Active

26

Technic large motor

Grey/Grey

88013

Yes

Yes

22169

Link

Active

2E

Technic XL motor

Grey/Grey

88014

Yes

Yes

22172, 88014

Link

Active

2F

Colour + distance sensor

White/Grey

88007

Partial

?

26912

Link

Active

25

WeDo 2.0 Motion sensor

White/Grey

45304

5003423-1

Link

Active

35

WeDo 2.0 Tilt sensor

White/Grey

45305

5003423-1

Link

Active

34

Mechanical Drawings

Mechanical drawing of the Raspberry Pi Build HAT.

mech build hat