Racebox Micro


The Raspberry Pi Build HAT - Complete Guide

 The πŸ›’Raspberry Pi Build HAT is an add-on board that connects to your Raspberry Pi's 40-pin GPIO header and was created in partnership with LEGO® Education to make controlling LEGO® TechnicTM motors and sensors with Raspberry Pi computers simple.

The Raspberry Pi Build HAT  - Complete Guide

It includes four connectors for SPIKETM Portfolio LEGO® TechnicTM motors and sensors. A distance sensor, a colour sensor, and a versatile force sensor are among the sensors available. The angular motors are available in a variety of sizes and have built-in encoders that may be used to determine their location.

Preparing your Build HAT
Preparing Raspberry Pi Build HAT

The Build HAT is supported by the LEGO® Education SPIKETM Prime Set 45678 and SPIKETM Prime Expansion Set 45681, both of which are available separately from LEGO® Education resellers.

NOTE: All 40-pin GPIO πŸ›’Raspberry Pi boards, including the πŸ›’Raspberry Pi 4 and πŸ›’Raspberry Pi Zero 2 W, are compatible with the HAT. It can also be utilised with Raspberry Pi 400 with the addition of a ribbon cable or other extension device.

  • Up to four LEGO® TechnicTM motors and sensors from the SPIKETM Portfolio can be controlled.
  • Control your LEGO® TechnicTM devices with this simple Python library.
  • Any Raspberry Pi computer with a 40-pin GPIO header will fit.
  • Low-level control of LEGO® TechnicTM devices is managed by an onboard RP2040 microcontroller.
  • A separate 8V PSU is available to power both the Build HAT and the Raspberry Pi.

NOTE: The Rapsberry Pi 400 cannot be powered by the Build HAT since it does not support GPIO headers.

You can also follow along with the Raspberry Pi Foundation's projects for Build HAT.

Getting the Raspberry Pi Ready for the Build HAT

Set up your Raspberry Pi and install the newest version of the operating system using Raspberry Pi Imager before you begin working with your Raspberry Pi Build HAT.

Attach 9mm spacers to the board's bottom. The Raspberry Pi Build HAT should be installed on your Raspberry Pi. Make sure it's the proper way up when you put it on. All of the components are on the bottom, unlike other HATs, allowing room on top for a breadboard or LEGO® pieces.

GPIO (General Purpose Input/Output) Pins

If you wish to access the Raspberry Pi's GPIO pins, you may use 15 mm spacers and an optional tall header.

The Build HAT uses the following pins, and you should not connect anything to them.



ID prom













Getting your Raspberry Pi set up

After the Raspberry Pi has booted, access the Raspberry Pi Configuration tool by selecting "Preferences" and then "Raspberry Pi Configuration" from the Raspberry Menu button.

Adjust the Serial settings as indicated below by clicking on the "interfaces" tab and adjusting the Serial parameters as given:

Using your Raspberry Pi as a Headless Computer

  • If you're using raspi-config to run your Raspberry Pi headless, choose "Interface Options" from the first option.
  • Then "P6 Serial Port" appears.
  • Disable the serial console by doing the following:
  • Also, make sure the serial port hardware is turned on.
  • This is how the final settings should appear.
  • If you've made any modifications, you'll need to reboot at this point.

The Build HAT's Power Source

Connect an external power supply — the original Raspberry Pi Build HAT power supply is suggested — although the Build HAT will be powered by any dependable +8V10 percent power supply capable of producing 48W through a DC 5521 centre positive barrel connection (5.5mm 2.1mm 11mm). Unless you're using a Raspberry Pi 400, you won't need to attach an additional USB power supply to the Raspberry Pi.

NOTE: The Raspberry Pi 400 cannot be powered by the Build HAT since it does not support GPIO headers.
Raspberry Pi Build HAT Powering Up

NOTE: Because the LEGO® TechnicTM motors are so strong, you'll need an extra 8V power supply to run them. If you wish to read from motor encoders and the SPIKETM force sensor, you may use the Raspberry Pi's USB power connector to power your Raspberry Pi and Build HAT. Like the motors, the SPIKETM colour and distance sensors require an external power supply.

Putting the Build HAT Software Together

Install the Python library Build HAT. To get started, open a Terminal window and type,
$ pip3 install buildhat
For additional information about the Build HAT Python Library visit.


There are a variety of motors that may be used with the Build HAT.

How to Connect a Motor to Raspberry Piu Build HAT

Connect a motor to the Build HAT's port A. The LPF2 connections must be placed in the proper orientation. If the connector is difficult to insert, twist it 180 degrees and try again.

Motors in the Workplace

To begin, open the Thonny IDE. Add the following code to your programme:
from buildhat import Motor

motor_a = Motor('A')

Click the play/run button to start the application. There will be a brief pause as the firmware is transferred to the board if this is the first time you've executed a Build HAT application after the Raspberry Pi has launched. The red LED should turn off and the green LED should turn on. This delay will not be required for further Python programme running.

  • For 5 seconds, crank your motor clockwise. 
  • Rerun your programme after changing the final line.
motor_a.run_for_seconds(5, speed=50)
  • The motor should now spin more quickly. Make another alteration:
motor_a.run_for_seconds(5, speed=-50)
  • The motor should rotate in the opposite direction (anti-clockwise).
By using the + button in Thonny, you may create a new programme. Add the following code to your page:
from buildhat import Motor

motor_a = Motor('A')

While True:
    print("Position: ", motor_a.get_aposition())
  • Run the application. Turn the shaft of the engine using your hand. The numbers written in the Thonny REPL should be changing.

Raspberry Pi Buld HAT Sensors

Sensors The Raspberry Pi Build HAT is compatible with a wide selection of sensors.

The Use of Sensors

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

Make a second new programme:

from signal import pause
from buildhat import ForceSensor, ColorSensor

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

def handle_pressed():

def handle_released():
Run it and press the Force sensor plunger while holding a coloured item (LEGO® elements are good) in front of the colour sensor. The sensor's LED should turn on, and the nearest color's name should appear in the thonny REPL.

Posts You May like

Popular Posts