# Build HAT
The Python Build HAT library supports the Raspberry Pi Build HAT, an add-on board for the Raspberry Pi computer, which allows control of up to four LEGO® TECHNIC™ motors and sensors included in the SPIKE™ Portfolio.
## Hardware
The Build HAT 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.
## Documentation
Library documentation: https://buildhat.readthedocs.io
Hardware documentation: https://www.raspberrypi.com/documentation/accessories/build-hat.html
Projects and inspiration: https://projects.raspberrypi.org/en/pathways/lego-intro
## Installation
To install the Build HAT library, enter the following commands in a terminal:
pip3 install buildhat
## Usage
See the [detailed documentation](https://buildhat.readthedocs.io/en/latest/buildhat/index.html) for the Python objects available.
```python
import time
from signal import pause
from buildhat import Motor
motor = Motor('A')
motor.set_default_speed(30)
print("Position", motor.get_aposition())
def handle_motor(speed, pos, apos):
print("Motor", speed, pos, apos)
motor.when_rotated = handle_motor
print("Run for degrees")
motor.run_for_degrees(360)
print("Run for seconds")
motor.run_for_seconds(5)
print("Run for rotations")
motor.run_for_rotations(2)
print("Start motor")
motor.start()
time.sleep(3)
print("Stop motor")
motor.stop()
pause()
```
## Building locally
Using [asdf](https://github.com/asdf-vm/asdf):
```
asdf install
```
Then:
```
pip3 install . --user
```
### Building the documentation
Instructions for regenerating the documentation can be found in
`docs/README.md`. Briefly, assuming you have the appropriate python
modules installed:
```
$ (cd docs; make html)
```
will rebuild the documentation. The doc tree starts at `docs/build/html/index.html`
Raw data
{
"_id": null,
"home_page": "https://github.com/RaspberryPiFoundation/python-build-hat",
"name": "buildhat",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": "Raspberry Pi Foundation",
"author_email": "web@raspberrypi.org",
"download_url": "https://files.pythonhosted.org/packages/3f/72/f2cf19c35056cbb6777b0fd8d6303b799a2c2392591f8252adff15cb5291/buildhat-0.8.0.tar.gz",
"platform": null,
"description": "# Build HAT\n\nThe Python Build HAT library supports the Raspberry Pi Build HAT, an add-on board for the Raspberry Pi computer, which allows control of up to four LEGO\u00ae TECHNIC\u2122 motors and sensors included in the SPIKE\u2122 Portfolio.\n\n## Hardware\n\nThe Build HAT provides four connectors for LEGO\u00ae Technic\u2122 motors and sensors from the SPIKE\u2122 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.\n\nThe Build HAT fits all Raspberry Pi computers with a 40-pin GPIO header, including\u2009\u2014\u2009with the addition of a ribbon cable or other extension device\u2009\u2014\u2009Raspberry Pi 400. Connected LEGO\u00ae Technic\u2122 devices can easily be controlled in Python, alongside standard Raspberry Pi accessories such as a camera module.\n\n## Documentation\n\nLibrary documentation: https://buildhat.readthedocs.io\n\nHardware documentation: https://www.raspberrypi.com/documentation/accessories/build-hat.html\n\nProjects and inspiration: https://projects.raspberrypi.org/en/pathways/lego-intro\n\n## Installation\n\nTo install the Build HAT library, enter the following commands in a terminal:\n\n pip3 install buildhat\n\n## Usage\n\nSee the [detailed documentation](https://buildhat.readthedocs.io/en/latest/buildhat/index.html) for the Python objects available.\n\n```python\nimport time\nfrom signal import pause\nfrom buildhat import Motor\n\nmotor = Motor('A')\nmotor.set_default_speed(30)\n\nprint(\"Position\", motor.get_aposition())\n\ndef handle_motor(speed, pos, apos):\n print(\"Motor\", speed, pos, apos)\n\nmotor.when_rotated = handle_motor\n\nprint(\"Run for degrees\")\nmotor.run_for_degrees(360)\n\nprint(\"Run for seconds\")\nmotor.run_for_seconds(5)\n\nprint(\"Run for rotations\")\nmotor.run_for_rotations(2)\n\nprint(\"Start motor\")\nmotor.start()\ntime.sleep(3)\nprint(\"Stop motor\")\nmotor.stop()\n\npause()\n```\n\n## Building locally\n\nUsing [asdf](https://github.com/asdf-vm/asdf):\n\n```\nasdf install\n```\n\nThen:\n\n```\npip3 install . --user\n```\n\n### Building the documentation\n\nInstructions for regenerating the documentation can be found in\n`docs/README.md`. Briefly, assuming you have the appropriate python\nmodules installed:\n\n```\n$ (cd docs; make html)\n```\n\nwill rebuild the documentation. The doc tree starts at `docs/build/html/index.html`\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Build HAT Python library",
"version": "0.8.0",
"project_urls": {
"Bug Tracker": "https://github.com/RaspberryPiFoundation/python-build-hat/issues",
"Homepage": "https://github.com/RaspberryPiFoundation/python-build-hat"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "974337a05199cb27916db4f7301d11cd8d2516d856bdecd1a501b9fbf5b3cb29",
"md5": "5cefbc9ca8d8c84b908a85849752956a",
"sha256": "57940d80557f3f024494de8c62da35ea99c08ee49cd9a65be8d39ee4989a24ab"
},
"downloads": -1,
"filename": "buildhat-0.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5cefbc9ca8d8c84b908a85849752956a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 59985,
"upload_time": "2025-07-09T13:53:44",
"upload_time_iso_8601": "2025-07-09T13:53:44.738565Z",
"url": "https://files.pythonhosted.org/packages/97/43/37a05199cb27916db4f7301d11cd8d2516d856bdecd1a501b9fbf5b3cb29/buildhat-0.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "3f72f2cf19c35056cbb6777b0fd8d6303b799a2c2392591f8252adff15cb5291",
"md5": "a0a91eb71924660c1688018995c6291b",
"sha256": "1742272c311377e0745c79cd6cfd5517b6551dd570f033174cf68c12383b0c1b"
},
"downloads": -1,
"filename": "buildhat-0.8.0.tar.gz",
"has_sig": false,
"md5_digest": "a0a91eb71924660c1688018995c6291b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 159783,
"upload_time": "2025-07-09T13:53:46",
"upload_time_iso_8601": "2025-07-09T13:53:46.134325Z",
"url": "https://files.pythonhosted.org/packages/3f/72/f2cf19c35056cbb6777b0fd8d6303b799a2c2392591f8252adff15cb5291/buildhat-0.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-09 13:53:46",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "RaspberryPiFoundation",
"github_project": "python-build-hat",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "buildhat"
}