MBC EXP6 6-Port Motor Expander (for LEGO® SPIKE™) × LEGO MINDSTORMS Robot Inventor

Control EXP6 with Python in the Robot Inventor App in three steps

A Python program in the Robot Inventor App always goes in this order: first import the library, then create an object and give EXP6 a name, and only then use that name to call functions (turn motors, read sensors). Work through the steps on the left and the program on the right grows with you. On the simulated EXP6, choose what is plugged into each expansion port to see how the motors turn and what the program prints.

Your hub: LEGO MINDSTORMS Robot Inventor (51515), programmed in Python in the Robot Inventor App 10.x (the App's word blocks cannot use this library). EXP6 needs firmware dated 2026-11-03 or later and its own battery, or the motors will not move. On SPIKE Prime, see the LEGO SPIKE App version.
1

Import the library

The Robot Inventor App cannot import files the way Pybricks does, so you install the EXP6 library on the hub once (it goes into /projects/mbc_exp6/ on the hub and survives power-off); after that every Python project just needs three import lines at the top. Without them the hub does not know what MBC_EXP6 means and you get an ImportError or NameError.

  1. Download MBC_EXP6_Robot_Inventor_Lib.zip (installer, uninstaller and 10 examples). The installer is Install_MBC_exp6_Lib.py; the library it installs is called MBC_exp6_SPIKE_App_Lib.
  2. Turn the hub on and connect it in the Robot Inventor App: USB cable or Bluetooth, either works (USB is steadier and recommended).
  3. Make a new Python project, paste the whole of Install_MBC_exp6_Lib.py and run it. After about 6 seconds the console prints "安裝成功! (Installed OK)" (after listing all 3 files as OK).
  4. You only do this once. A hub firmware update wipes the library, so install it again afterwards. To remove it, run Uninstall_MBC_exp6_Lib.py from the download pack.
  5. Make a new Python project and type the three lines below at the very top (marked ① in the program on the right).

First time? Step-by-step version: Robot Inventor install guide. You can install over USB or Bluetooth; USB is recommended.

Only the import line differs from the LEGO SPIKE App (Education): there it is from MBC_exp6_SPIKE_App_Lib import MBC_EXP6; on Robot Inventor it is from projects.mbc_exp6.MBC_exp6_SPIKE_App_Lib import MBC_EXP6.

2

Create an object: give EXP6 a name

Now you "create an object": you tell the program which hub port the EXP6 Host cable is plugged into, and you give it a name. From then on, you use this name every time you turn a motor or read a sensor. The Robot Inventor version has no multitasking: the program runs line by line and waits with time.sleep(). You do not need a heartbeat; the library handles it in the background.

Which hub port is the EXP6 Host cable plugged into?

The port.C in the program is the hub port: port.A to port.F (the Robot Inventor App uses letters, or you can write 'C'; only Pybricks writes the number 3). The first number inside the brackets of the functions later is the expansion port on EXP6, 1 to 6. Do not mix them up.

3

Use the functions

You always write "exp6.function_name(expansion port, …)": the name, a dot, then the function. The first number in the brackets is usually the expansion port on EXP6, 1 to 6. Click a function and the program and output switch to its example. The motors on the simulated EXP6 move to match.

Simulated EXP6 (seen from above): choose what is plugged into each expansion port, then watch the motors turn and see what is printed on the right

At the start, expansion ports 1 and 2 have motors, port 4 a colour sensor, port 5 an ultrasonic sensor and port 6 a force sensor, the same ports the examples use. The simulation is only an illustration: 1% motor speed counts as 10 degrees per second (a real motor depends on its model), and the sensor values are simplified. The red pointer shows which way the motor shaft points; ↻ = clockwise. The motors stop by themselves after the program ends: if a motor is still running when the program ends (or you press stop), it stops within about 10 seconds (the simulator does the same); to stop at once, end with exp6.stop_all(1). Colours follow this library, which works them out from red, green and blue (green is 5 and "no colour" is 0, unlike Pybricks); the ultrasonic sensor reads -1 when nothing is in front.

An example for every function

33 functions (the same as the LEGO SPIKE App version; the Pybricks-only connected and run_task are not here), each with a short example. Actions such as turning or stopping a motor usually go outside the loop and run once. Readings go inside the loop and are read again and again. Click "Try it" to jump to step 3; the program and output on the right switch to that example.

Complete examples

Two-wheel car drives a square (left wheel port 1, right wheel port 2)

        

!Only change the "Things to change" part at the top. Turns less than 90 degrees: make TURN a bit bigger; turns too far: make it smaller. Sides too long or too short: change SIDE (720 degrees is about 35 cm). Car drives backwards: change the 1 in set_motor_inverted(1, True) to 2.

10 ready-made Robot Inventor examples (.py, all run on a real hub)
  1. 01_Motor_Basic.py: Motor basics: speed, forward/back, stop
  2. 02_Motor_Advanced.py: Encoder angle, turn by degrees, absolute angle
  3. 03_Drive_Base.py: Two-motor drive base (straight, turns)
  4. 04_Sensor_Distance_Force.py: LEGO distance and force sensors
  5. 05_Sensor_Color_Pro.py: LEGO colour sensor: colour code, reflection, RGB, HSV
  6. 06_System_Misc.py: Voltage, device codes, raw values
  7. 07_Motor_Inverted.py: Motor mounted normally / reversed
  8. 08_Stop_Modes.py: The 4 stop modes compared
  9. 09_MBC_Sensors.py: MBC sensors: Line Follower, Laser Ranger 2-ch, PS4 gamepad, SharpShot Vision
  10. 10_MBC_Commands.py: Send commands to a Line Follower 8-ch: change the threshold, failed, rejected, restore

!Install the library first, then paste an example into a Python project in the Robot Inventor App and run it. In the examples EXP6 is in hub port F and the motors are in EXP6 ports 1 and 2; in 09 and 10 the MBC sensor is in EXP6 port 2. The examples print their messages in Chinese. All of them are also in MBC_EXP6_Robot_Inventor_Lib.zip.

MBC sensors on the EXP6 (Line Follower, Laser Ranger, Controller, SharpShot)

When you run out of hub ports, plug MBC sensors straight into EXP6 ports 1–6, just as you would into the hub; there is nothing to set up. The EXP6 needs firmware dated 2026-11-03 or later. On Robot Inventor the one library installed above already includes the MBC sensors; there are no product files to add. The first time you use a kind of sensor, the hub takes about 1 second to load that part of the library. Same three steps as above: pick the sensor, pick the EXP6 port it is in, pick a function, and the program on the right follows along.

1

Import

Which sensor is on the EXP6?

The Robot Inventor library MBC_exp6_SPIKE_App_Lib already includes the MBC sensors. Once it is installed on the hub, every project just needs the three lines at the top (from hub import port, import time, from projects.mbc_exp6.MBC_exp6_SPIKE_App_Lib import MBC_EXP6).

    2

    Create the objects: EXP6 first, then the sensor

    Which EXP6 port is the sensor in?

    3

    Use the functions: same names as on the hub

    Send commands to an MBC sensor: cmd(), wait_done(), cmd_state()

    Line Follower 8-ch: change the binarisation threshold (E4)

    Besides reading values, you can send commands to an MBC sensor through EXP6, with the same codes and parameters as when the sensor is on the hub (for the Line Follower 8-ch see the command reference). cmd(code, params…): code 0xE0 to 0xEE, up to 5 parameters (-9999 to 9999), returns a number for the command; the same command with the same parameters is not sent again unless you add again=True. wait_done(): waits for the result, True = done, False = failed, rejected or timed out (10 seconds by default); cmd_state(): 0 done, 1 busy, 2 failed (bad parameter), 3 rejected (this sensor has no such command); busy(): True while it is not finished. Each command takes about 0.1 to 0.2 seconds to come back.

    from hub import port
    import time
    from projects.mbc_exp6.MBC_exp6_SPIKE_App_Lib import MBC_EXP6
    
    exp6 = MBC_EXP6(port.C)
    line8 = exp6.line8(5)          # Line Follower 8-ch in EXP6 port 5
    
    seq = line8.cmd(0xE4, 60)      # binarisation threshold to 60 (1 to 99; back to 50 on power-up)
    if line8.wait_done():          # wait for the sensor: True = done
        print("Threshold changed, number", seq)
    else:
        print("Did not work, state", line8.cmd_state())   # 2 failed, 3 rejected
    print("Black/white:", line8.bin_raw())
    line8.cmd(0xE4, 50)            # back to the default 50
    line8.wait_done()

    !Black/white calibration E2 / E3 and the 5-second calibration E7 are saved in the sensor, so do not send them while practising; E4 and E9 go back to default on power-up. So far only the Line Follower 8-ch has been tested with commands. Failed and rejected commands are shown in the example 10 (10_MBC_Commands.py).

    Things to know

    1. EXP6 stays in Robot Inventor mode until you take out its battery. Once a program has created MBC_EXP6, EXP6 keeps talking the Robot Inventor way, and the App's device screen cannot see it. To use it with a SPIKE Prime hub again, or to let the App see it, take the EXP6 battery out and put it back.
    2. No heartbeat to write. The library keeps EXP6 alive in the background.
    3. The motors stop by themselves about 10 seconds after the program ends. If a motor must keep running for more than 10 seconds while your program calls nothing, wait with exp6.keep_alive_wait(ms), for example exp6.keep_alive_wait(15000).
    4. Always connect the EXP6 battery, and do not mix up the two kinds of port. port.C in MBC_EXP6(port.C) is the hub port; the 1 in exp6.motor_run(1, 50) is the EXP6 expansion port. A turn command moves on right away; wait, or keep reading the angle until it gets there.
    5. If something goes wrong: ImportError means the library is not installed (or a firmware update wiped it), or the import line is the SPIKE App one; "讀不到擴充板資料" means the hub port in MBC_EXP6(port.X) is wrong or EXP6 has no power; if the hub switches off during install, connect by USB and install again; MemoryError means the program is too big: split it and restart the hub. All core functions use about 26 KB and the MBC sensors up to another 19 KB, leaving about 37 KB of the App's ~98 KB for your program.