List all connected devices¶
Ensure your device works with this simple test that initialize the Build HAT and list all the connected devices
examples/simpletest.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2024 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5"""
6Example that initialize the Build HAT and list all the connected devices
7Having debug=True it also print in the output console all steps during hat initialization
8"""
9
10import board
11
12from buildhat.hat import Hat
13
14# Pins for Waveshare RP2040-Zero.
15# Change the pins if you are using a different board
16tx_pin = board.TX
17rx_pin = board.RX
18reset_pin = board.GP23
19
20buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
21for port in range(4):
22 device = buildhat.get_device(port)
23 if device:
24 print(f"Port {port}: {device.name}")
Active motor¶
Run a motor in the different modes: relative, absolute, position and with different speed measuring unit: degrees per second, revoluitions per minutes
examples/active_motor.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2024 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5import asyncio
6
7import board
8
9from buildhat.hat import Hat
10from buildhat.motors.direction import Direction
11from buildhat.motors.speedunit import SpeedUnit
12
13# BuildHAT port where the motor is connected
14motor_port = 3
15
16# Pins for Waveshare RP2040-Zero.
17# Change the pins if you are using a different board
18tx_pin = board.TX
19rx_pin = board.RX
20reset_pin = board.GP23
21
22buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
23
24stop = False
25
26
27async def buildhat_loop(hat):
28 while not stop:
29 hat.update()
30 await asyncio.sleep(0)
31
32
33async def data_loop(hat):
34 motor = buildhat.get_device(motor_port)
35 motor.data_update_interval = 10
36
37 while not stop:
38 if motor.has_absolute_position:
39 print(f"Speed: {motor.actual_speed} °/s, Pos: {motor.actual_position}°, APos: {motor.actual_absolute_position}°")
40 else:
41 print(f"Speed: {motor.actual_speed} °/s, Pos: {motor.actual_position}°")
42 await asyncio.sleep(0.1)
43
44
45async def motor_loop(hat):
46 motor = buildhat.get_device(motor_port)
47
48 motor.power_limit = 1
49 motor.release_after_run = True # The motor is free to spin after the movement is done
50
51 while not stop:
52 motor.run_command_speed_unit = SpeedUnit.DGS # degrees per second
53 motor.run_command_default_speed = 120 # Default speed when not specified in the run command
54 await motor.run_for_rotations(2) # Use default speed (90 DGS)
55 await motor.run_for_rotations(2, speed=180) # Run at speed 4180 DGS
56 await motor.run_for_degrees(90) # Turn 90 degree from current position
57 await motor.run_for_degrees(-90, speed=180) # Turn back 90 degree from current postion
58
59 motor.actual_position = 0 # Preset actual_position and change it to 0
60 await motor.run_to_position(720, speed=360) # Run the motor at the position 720 degree
61
62 motor.run_command_speed_unit = SpeedUnit.RPM # revolutions per second
63 if motor.has_absolute_position:
64 # Not all motors have the absolute position
65 # Absolute position cover one turn from -180 to 180 degree
66 await motor.run_to_absolute_angle(0, speed=60)
67 await motor.run_to_absolute_angle(-30, speed=120, direction=Direction.CW)
68 await motor.run_to_absolute_angle(-170, speed=90, direction=Direction.CCW)
69 await motor.run_to_absolute_angle(170, speed=120, direction=Direction.SHORTEST)
70
71 await motor.run_for_seconds(5) # Spin the motor for 5 seconds
72 motor.start(speed=120) # Start the motor to run indefinitely
73 await asyncio.sleep(2)
74 motor.stop() # Stop and leaving floating
75 await asyncio.sleep(1)
76
77 motor.pwm(1) # Spin the motor open loop at max speed. All previous mode were close loop
78 await asyncio.sleep(2)
79 motor.coast() # Let the motor coast to a stop
80 await asyncio.sleep(2)
81
82 motor.reverse_direction = not motor.reverse_direction
83
84
85async def main():
86 buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
87 data_loop_task = asyncio.create_task(data_loop(buildhat))
88 motor_loop_task = asyncio.create_task(motor_loop(buildhat))
89
90 await asyncio.gather(buildhat_loop_task, data_loop_task, motor_loop_task)
91
92
93asyncio.run(main())
Color sensor¶
Reads all the measurable values: color, ambient light, reflected light It also show how to switch off and on the sensor
examples/color_sensor.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2024 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5"""
6Test code for color sensor
7Part number: 6217705
8"""
9
10import asyncio
11
12import board
13
14from buildhat.hat import Hat
15
16sensor_port = 0
17
18# Pins for Waveshare RP2040-Zero.
19# Change the pins if you are using a different board
20tx_pin = board.TX
21rx_pin = board.RX
22reset_pin = board.GP23
23
24buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
25
26stop = False
27
28
29async def buildhat_loop(hat):
30 while not stop:
31 hat.update()
32 await asyncio.sleep(0)
33
34
35async def read_loop(hat):
36 sensor = buildhat.get_device(sensor_port)
37
38 while not stop:
39 color = await sensor.get_color()
40 print(f"Color: {color}")
41 ambient_light = await sensor.get_ambient_light()
42 print(f"Ambient light: {ambient_light}")
43 reflected = await sensor.get_reflected_light()
44 print(f"Reflected light: {reflected}")
45 sensor.off()
46 print(f"Sensor off")
47 await asyncio.sleep(1)
48 sensor.on()
49 print(f"Sensor on")
50 await asyncio.sleep(1)
51
52
53async def main():
54 buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
55 read_loop_task = asyncio.create_task(read_loop(buildhat))
56
57 await asyncio.gather(buildhat_loop_task, read_loop_task)
58
59
60asyncio.run(main())
Color + distance sensor¶
Reads all the measurable values: color, ambient light, reflected light, distance, counter It also show how to switch off and on the sensor
examples/color_distance_sensor.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2024 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5"""
6Test code for Boost color & distance sensor
7Part number: 88007
8"""
9
10import asyncio
11
12import board
13
14from buildhat.devices.color import Color
15from buildhat.hat import Hat
16
17sensor_port = 1
18
19# Pins for Waveshare RP2040-Zero.
20# Change the pins if you are using a different board
21tx_pin = board.TX
22rx_pin = board.RX
23reset_pin = board.GP23
24
25buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
26
27stop = False
28
29
30async def buildhat_loop(hat):
31 while not stop:
32 hat.update()
33 await asyncio.sleep(0)
34
35
36async def read_loop(hat):
37 sensor = buildhat.get_device(sensor_port)
38
39 while not stop:
40 color = await sensor.get_color()
41 print(f"Color: {color}")
42 ambient_light = await sensor.get_ambient_light()
43 print(f"Ambient light: {ambient_light}")
44 reflected = await sensor.get_reflected_light()
45 print(f"Reflected light: {reflected}")
46 distance = await sensor.get_distance()
47 print(f"Distance: {distance}")
48 counter = await sensor.get_counter()
49 print(f"Counter: {counter}")
50
51 sensor.off()
52 print(f"Sensor off")
53 await asyncio.sleep(1)
54 sensor.on()
55 print(f"Sensor on")
56 await asyncio.sleep(1)
57
58
59async def main():
60 buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
61 read_loop_task = asyncio.create_task(read_loop(buildhat))
62
63 await asyncio.gather(buildhat_loop_task, read_loop_task)
64
65
66asyncio.run(main())
Ultrasonic distance sensor¶
Use several tasks to continuosly read the distance and animate the eyes lights
examples/distance_sensor.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2024 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5"""
6Test code for Lego® Technic Distance Sensor
7Part number: 6302968
8"""
9
10import asyncio
11
12import board
13
14from buildhat.hat import Hat
15
16sensor_port = 0
17
18# Pins for Waveshare RP2040-Zero.
19# Change the pins if you are using a different board
20tx_pin = board.TX
21rx_pin = board.RX
22reset_pin = board.GP23
23
24buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
25
26stop = False
27
28
29async def buildhat_loop(hat):
30 while not stop:
31 hat.update()
32 await asyncio.sleep(0)
33
34
35async def read_loop(hat):
36 sensor = buildhat.get_device(sensor_port)
37
38 while not stop:
39 d = sensor.distance
40 if d > 0:
41 print(f"Distance {d} mm")
42 else:
43 print("Please put an obstacle in front of the ultrasonic sensor")
44 await asyncio.sleep(0.2)
45
46
47async def eyes_loop(hat):
48 sensor = buildhat.get_device(sensor_port)
49 pause = 0.02
50
51 while not stop:
52 # Drive all four eyes together
53 for i in range(100):
54 sensor.eyes(i)
55 await asyncio.sleep(pause)
56
57 # Drive all four eyes one a the time
58 for i in range(100):
59 sensor.eyes(i, 0, 0, 0)
60 await asyncio.sleep(pause)
61 for i in range(100):
62 sensor.eyes(0, i, 0, 0)
63 await asyncio.sleep(pause)
64 for i in range(100):
65 sensor.eyes(0, 0, i, 0)
66 await asyncio.sleep(pause)
67 for i in range(100):
68 sensor.eyes(0, 0, 0, i)
69 await asyncio.sleep(pause)
70
71
72async def main():
73 buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
74 read_loop_task = asyncio.create_task(read_loop(buildhat))
75 eyes_loop_task = asyncio.create_task(eyes_loop(buildhat))
76
77 await asyncio.gather(buildhat_loop_task, read_loop_task, eyes_loop_task)
78
79
80asyncio.run(main())
3x3 Color Light matrix¶
Show how to drive the display in the different mode and how to apply transitions
examples/light_matrix.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2024 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5"""
6Test code for Lego® Technic 3x3 Color Light Matrix
7Part number: 45608
8"""
9
10import asyncio
11
12import board
13
14from buildhat.devices.matrixcolor import MatrixColor
15from buildhat.devices.matrixtransition import MatrixTransition
16from buildhat.hat import Hat
17
18matrix_port = 2
19
20# Pins for Waveshare RP2040-Zero.
21# Change the pins if you are using a different board
22tx_pin = board.TX
23rx_pin = board.RX
24reset_pin = board.GP23
25
26buildhat = Hat(tx=tx_pin, rx=rx_pin, reset=reset_pin, debug=True)
27
28stop = False
29
30
31async def buildhat_loop(hat):
32 while not stop:
33 hat.update()
34 await asyncio.sleep(0)
35
36
37async def matrix_loop(hat):
38 while not stop:
39 matrix = buildhat.get_device(matrix_port)
40
41 matrix.display_single_color(MatrixColor.YELLOW)
42 await asyncio.sleep(1)
43
44 for i in range(10):
45 matrix.display_level_bar(i)
46 await asyncio.sleep(0.3)
47
48 matrix.set_display_image_transition(MatrixTransition.SWIPE_RTL)
49 matrix.display_single_color(MatrixColor.YELLOW)
50 await asyncio.sleep(2)
51 matrix.display_single_color(MatrixColor.BLUE)
52 await asyncio.sleep(2)
53 matrix.display_single_color(MatrixColor.RED)
54 await asyncio.sleep(2)
55
56 matrix.set_display_image_transition(MatrixTransition.FADE_IN_OUT)
57 matrix.fill_pixels(MatrixColor.YELLOW, 5)
58 matrix.set_pixel(1, 1, MatrixColor.BLUE, 10)
59 matrix.display_pixels()
60 await asyncio.sleep(2)
61 matrix.fill_pixels(MatrixColor.YELLOW, 5)
62 matrix.set_pixel(0, 0, MatrixColor.BLUE, 10)
63 matrix.set_pixel(2, 2, MatrixColor.BLUE, 10)
64 matrix.display_pixels()
65 await asyncio.sleep(2)
66 matrix.fill_pixels(MatrixColor.YELLOW, 5)
67 matrix.set_pixel(0, 0, MatrixColor.BLUE, 10)
68 matrix.set_pixel(1, 1, MatrixColor.BLUE, 10)
69 matrix.set_pixel(2, 2, MatrixColor.BLUE, 10)
70 matrix.display_pixels()
71 await asyncio.sleep(2)
72 matrix.set_display_image_transition(MatrixTransition.NONE)
73 matrix.fill_pixels(MatrixColor.YELLOW, 5)
74 matrix.set_pixel(0, 0, MatrixColor.BLUE, 10)
75 matrix.set_pixel(0, 2, MatrixColor.BLUE, 10)
76 matrix.set_pixel(2, 2, MatrixColor.BLUE, 10)
77 matrix.set_pixel(2, 0, MatrixColor.BLUE, 10)
78 matrix.display_pixels()
79 await asyncio.sleep(2)
80
81
82async def main():
83 buildhat_loop_task = asyncio.create_task(buildhat_loop(buildhat))
84 matrix_loop_task = asyncio.create_task(matrix_loop(buildhat))
85
86 await asyncio.gather(buildhat_loop_task, matrix_loop_task)
87
88
89asyncio.run(main())