---
agent:
  instructions: "Use this search API to find relevant content across this site (products with prices and stock, collections, guides, articles, courses, help and pages) with keywords or natural-language queries, instead of crawling individual pages. Replace {query} with your URL-encoded query. Results are returned in Markdown."
  endpoint: "https://piaustralia.com.au/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Raspberry Pi Build HAT

Drive LEGO Education SPIKE motors and sensors from a Raspberry Pi. What the Build HAT is, the power it needs, switching on the serial port, and your first motor program in Python.

## What it is

The Build HAT connects a Raspberry Pi to LEGO Technic motors and sensors. It has four LEGO connectors, labelled **A** to **D**, and its own RP2040 microcontroller (the chip from the Raspberry Pi Pico) that handles the fast, fiddly work of running motors and reading sensors. Your Pi sends it simple commands over the serial port.

It takes parts from the **LEGO Education SPIKE** range and LEGO MINDSTORMS Robot Inventor, plus most other LEGO devices that use the same connector, including:

- the SPIKE Small, Medium and Large Angular Motors;
- the SPIKE Colour Sensor, Distance Sensor and Force Sensor;
- the 3 × 3 Colour Light Matrix;
- older Technic motors (Medium Linear, Large and XL), and simpler motors such as the train motor, which run without position feedback.

All the components are on the underside of the board, which leaves the top flat for LEGO pieces or a small breadboard.

## Which Pi it fits

It fits any Raspberry Pi with the 40-pin GPIO header, including the Zero range (with a header fitted). The Pi 400 and 500 keyboards need a ribbon cable or other extension to reach it, and need their own power supply.

The Python library has supported the **Raspberry Pi 5** since version 0.6.0.

**Check your OS version.** When we last checked (October 2026), Raspberry Pi said the Build HAT wasn't yet supported on Raspberry Pi OS **Trixie**. Use the **Bookworm** release for now.

## Power

Motors need more than the Pi's GPIO pins can give, so the Build HAT has its own barrel-jack power input:

- **8 V DC (±10%)**, able to deliver **48 W**;
- a 5.5 × 2.1 mm barrel plug, centre positive.

The official **Build HAT Power Supply** (8 V, 6 A, 48 W) is made for it. With that plugged in, the Build HAT powers the Pi as well through the header, so you don't need the Pi's own USB-C supply (the Pi 400 and 500 are the exceptions).

Without the barrel supply, the Pi's own power is enough to read motor positions and the Force Sensor, but **not** to turn motors or run the Colour and Distance Sensors.

## Fitting it

Fit 9 mm spacers to the underside and press the board onto the header. If you also want to reach the Pi's other GPIO pins, use a tall stacking header with 15 mm spacers instead.

The Build HAT uses these pins, so don't connect anything else to them:

| GPIO | Physical pin | Used for |
|---|---|---|
| 0 and 1 | 27 and 28 | HAT ID EEPROM |
| 4 | 7 | Resetting the RP2040 |
| 14 | 8 | Serial TX |
| 15 | 10 | Serial RX |

The Python library also uses GPIO22 (physical pin 15) when it loads the board's firmware. Our [GPIO header guide](/docs/gpio-pinout) shows where these are.

## Turn on the serial port

The Pi talks to the Build HAT over its serial port, which is off by default:

1. Run `sudo raspi-config`.
2. Choose **Interface Options**, then **Serial Port**.
3. When asked about a login shell over serial, answer **No**.
4. When asked to enable the serial port hardware, answer **Yes**.
5. Reboot.

On the desktop you can do the same from the Raspberry Pi configuration tool's **Interfaces** tab: turn **Serial Port** on and leave **Serial Console** off.

## Install the Python library

```
sudo apt update
sudo apt install python3-build-hat
```

The first time a program uses the Build HAT after the Pi starts, there's a short pause while the library loads firmware onto the board. When it's ready, the red LED goes out and the green one lights.

## Your first motor program

Plug a motor into port A, connect the power supply, and run this:

```python
from buildhat import Motor

motor = Motor('A')

motor.run_for_seconds(2, speed=50)    # forwards at half speed
motor.run_for_seconds(2, speed=-50)   # backwards
motor.run_for_rotations(1)            # one full turn
motor.run_to_position(0)              # back to the motor's zero mark
```

Speeds run from −100 to 100; a negative number reverses the motor. `run_to_position()` takes an angle from −180 to 180 and can be told which way to turn with `direction='clockwise'` or `'anticlockwise'`.

Each command waits until the movement finishes. To move two motors at once, add `blocking=False` to the first:

```python
left = Motor('A')
right = Motor('B')

left.run_for_rotations(2, blocking=False)
right.run_for_rotations(2)
```

For a motor that keeps going, use `motor.start(40)` and `motor.stop()`. `motor.get_aposition()` tells you where the shaft is pointing, in degrees.

## Adding sensors

A Force Sensor on port B as a start button:

```python
from buildhat import Motor, ForceSensor

motor = Motor('A')
button = ForceSensor('B')

while True:
    button.wait_until_pressed()
    motor.start(40)
    button.wait_until_released()
    motor.stop()
```

A Colour Sensor on port C, printing each new colour it sees:

```python
from buildhat import ColorSensor

sensor = ColorSensor('C')

while True:
    print(sensor.wait_for_new_color())
```

The library also has classes for the Distance Sensor, the Colour Light Matrix and passive motors.

## Parts in this guide

- [Raspberry Pi Build HAT](https://piaustralia.com.au/products/raspberry-pi-build-hat.md)
- [Raspberry Pi Build HAT Power Supply - 48W](https://piaustralia.com.au/products/raspberry-pi-build-hat-power-supply-48w.md)
- [GPIO Stacking Header for Pi A+/B+/Pi 2/Pi 3 [Extra-long 2x20 Pins]](https://piaustralia.com.au/products/gpio-stacking-header-for-pi-a-b-pi-2-pi-3-extra-long-2x20-pins.md)

Written by Pi Australia from Raspberry Pi's Build HAT documentation, Build HAT product page, Build HAT Python library reference, Build HAT Python library changelog.
