Quickstart

The two user-facing MicroPython modules are:

  • hub — on-board peripherals (ports A–D, hub LED, IMU, buttons, LCD, power).

  • lpf2 — the LEGO Powered-Up (LPF2) protocol primitives: ports, devices, hub emulation, remote-hub client, enumerations.

Minimal program

import hub

port = hub.ports.A
hub.on("loop")
def loop():
    if port.isDeviceConnected():
        print("device:", port.getDeviceType())
        break
    hub.sleep_ms(10)

The C firmware polls every port’s update() for you each tick — device detection and value parsing happen in the background, so scripts just read state. See Automatic polling for details.

Event-driven form

When a script is launched from the on-device menu the runner injects hub.on — decorate setup and loop handlers instead of writing your own while True:

import hub

@hub.on("setup")
def setup():
    hub.led.setColor(0, 128, 0)

@hub.on("loop")
def loop():
    ...

The runner drives loop at roughly 10 ms per tick and also calls hub.buttons.poll() for you.

What the modules do

Module

Purpose

hub

Board singletons: hub._ports_module, hub._imu_module, hub._buttons_module, hub._lcd_module, hub._i2c, hub._board_module, hub.powerOff().

lpf2

lpf2.port API, lpf2.hub (BLE client), lpf2.hub_emulation (BLE server), lpf2.battery.

lpf2.local

Concrete lpf2.local.port for on-hub UART sockets.

lpf2.virtual

lpf2.virtual.port and lpf2.virtual.device for Python-implemented emulated devices.

lpf2.devices

Typed device wrappers (lpf2.devices.encoder_motor, lpf2.devices.color_sensor, …).

lpf2.port_expander

Sub-port helpers for the LPF2 port expander accessory.

Enum modules

lpf2.color, lpf2.device_type, lpf2.hub_type, lpf2.alerts, lpf2.battery_type, lpf2.button_state, lpf2.hub_property, lpf2.port_num.