Hub Built-in Sensors
The hub itself is a bundle of sensors: a 6-axis gyro and two buttons, plus two output devices - the light matrix and speaker. None of them use a port.
Gyro / IMU
A six-axis inertial measurement unit, the core of precise FLL navigation. Three angles:
| Angle | Meaning | FLL use |
|---|---|---|
| Yaw | Robot heading (horizontal rotation) | Turns and straight driving - 90% of usage |
| Pitch | Nose up/down | Detect ramps / being lifted |
| Roll | Lean left/right | Rarely used |
Word Blocks
(yaw angle) ← Sensors category reporter
reset yaw angle [0]
<hub [shaken]>
wait until <yaw angle > [88]>Key behaviors
- Yaw counts from the heading at start; in Word Blocks turning right increases it (clockwise positive), range -180 to 180. The Python API's raw value runs the opposite way (left-positive) - see the code comment below.
reset yaw angle 0defines the current heading as 0; later readings are relative to it.- Gyros drift slightly: readings creep while standing still. Reset before every launch, and re-reset mid-program when you get a chance (while squared against a wall).
Example: gyro right turn 90°
reset yaw angle [0]
start moving [steering 100]
wait until <yaw angle > [88]> ← stop 2° early; momentum covers the rest
stop movingWhy 88 and not 90: the robot coasts a little after the stop command. Measure your own lead value - details in Gyro Driving & Turns.
Hub buttons
Left and right buttons are programmable (the center button belongs to the system: start/stop programs):
when [left] button [pressed]
<[right] button [pressed]>Uses: toggling debug modes, manual attachment zeroing, pit-side confirmation.
Light Matrix (output)
5x5 LEDs - your debugging display:
display image [SMILE]
write [Go]
turn off pixels
set pixel [x] [y] to [100] %The most useful trick: display a live sensor value.
forever
write (color sensor [C] reflected light)Speaker (output)
play beep [60] for [0.2] seconds ← 60 is pitch (MIDI note number), not frequencyPut different beeps at key points in the program: your eyes watch the robot, your ears track the program.
Python
import runloop, motor_pair
from hub import port, motion_sensor, light_matrix, button, sound
async def main():
motion_sensor.reset_yaw(0)
angles = motion_sensor.tilt_angles() # (yaw, pitch, roll) in DECIdegrees!
yaw = -angles[0] / 10 # raw is left-positive; negate to match Word Blocks' right-positive
await light_matrix.write("Hi")
sound.beep(440, 200, 100) # 440 Hz, 200 ms, volume 100
if button.pressed(button.LEFT): # returns ms held, 0 = not pressed
pass
runloop.run(main())Python angles are in 0.1-degree units
tilt_angles() returns yaw in decidegrees: a reading of 900 = 90°. Divide by 10 before using.
Python API quick reference
All from the hub package: from hub import motion_sensor, light_matrix, button, sound, light, port.
| Function | Returns / effect | Notes |
|---|---|---|
motion_sensor.tilt_angles() | (yaw, pitch, roll) in decidegrees | Raw yaw is left-positive - negate for the Word Blocks convention |
motion_sensor.reset_yaw(0) | sets the current heading as the reference | Do it at launch and after wall squaring |
light_matrix.write("Hi") | scrolls text (awaitable) | await it, or it scrolls in the background |
light_matrix.show_image(light_matrix.IMAGE_HAPPY) | shows a built-in image | Many IMAGE_* constants |
light_matrix.set_pixel(x, y, 100) | one pixel, brightness 0-100 | x, y in 0-4 |
light_matrix.clear() | all pixels off | |
button.pressed(button.LEFT) | int, ms held (0 = not pressed) | Truthy in conditions |
sound.beep(440, 200, 100) | hub speaker beep (awaitable) | frequency Hz, duration ms, volume; await it to wait for the beep to finish, omit await to play in the background |
light.color(light.POWER, color.GREEN) | center button light | needs import color |
More examples
A reusable gyro turn with timeout
Uses the negated yaw so positive angles turn right like Word Blocks. The 4-second timeout means a stalled robot gives up instead of spinning forever:
import runloop, motor_pair
from hub import port, motion_sensor
motor_pair.pair(motor_pair.PAIR_1, port.A, port.E)
def yaw():
# raw yaw is left-positive; negate to match Word Blocks' right-positive
return -motion_sensor.tilt_angles()[0] / 10
async def gyro_turn(angle, velocity=200):
motion_sensor.reset_yaw(0)
steering = 100 if angle > 0 else -100
motor_pair.move(motor_pair.PAIR_1, steering, velocity=velocity)
if angle > 0:
await runloop.until(lambda: yaw() > angle - 2, 4000)
else:
await runloop.until(lambda: yaw() < angle + 2, 4000)
motor_pair.stop(motor_pair.PAIR_1)
async def main():
await gyro_turn(90)
await gyro_turn(-90)
runloop.run(main())Live yaw readout on the console
Turn the robot by hand and watch the sign convention with your own eyes:
import runloop
from hub import motion_sensor
async def main():
while True:
print("yaw (right-positive):", -motion_sensor.tilt_angles()[0] / 10)
await runloop.sleep_ms(100)
runloop.run(main())Sensors complete. Now put them to work: Advanced Word Blocks.