This page has moved into the new guide: MBC EXP6 6-Port Motor Expander › SPIKE

SPIKE beginner examples: MBC EXP6 6-Port Motor Expander (for LEGO® SPIKE™)

Back to the EXP6 product page

Note

The Pybricks programs below are the same files as on the Chinese page, so the comments inside the code are in Chinese. The English explanation under each example tells you what every line does.

This page is written for people who have never written a program before. EXP6 gives one SPIKE hub 6 extra expansion ports, so you can plug in more motors and sensors. The 5 examples start with "one motor turns one full turn" and build up step by step to "a two-wheel car drives a square by itself". Every example comes in 3 versions; just pick the one you have on hand:

Version Who it is for How to open the example file
🟨 The official LEGO SPIKE App (Python) Schools, or anyone who only has the official App First follow "Before you start" below and install the library once → download the .llsp3 → open the SPIKE App → top-left "File" → "Open" → pick the file → press ▶
🟦 The MBC Blocks website (block coding) Anyone who wants blocks instead of typing Open the MBC SPIKE Blocks website → top menu "File → Open" (or press Ctrl+O) → pick the downloaded .abp
🐍 Pybricks (Python text code) Anyone who wants to learn typed programming Open code.pybricks.com → "Import a file" in the file panel on the left → import both the example file and MBC_exp6_obj_Lib.py → click the example → press ▶ to run

Important

  • The official App has no EXP6 blocks, so it is Python only. The examples are already written; download, open and run, no typing needed.
  • The MBC Blocks website and Pybricks need the hub flashed with Pybricks firmware first (on the MBC Blocks website, top menu "SPIKE Hub → Install / update Pybricks firmware" does it, about 3 minutes; you can switch back to the original LEGO firmware later with the SPIKE App).
  • The official App used here is the Education version (SPIKE 3; the library is marked for version 3.4.3). These examples use the SPIKE Prime hub; the 10 Robot Inventor (51515) examples are at the end of the official App guide.
  • Before switching to a different version, power-cycle EXP6 once (turn the hub off or unplug the Host cable). Once EXP6 has run a Pybricks program, it keeps talking the Pybricks way until it reboots, and the official App will not be able to connect to it.

Before you start (3 things)

1. You must connect a battery

  • The motors on EXP6 are powered entirely by EXP6's own battery. With only the Host cable and no battery, nothing will move.
  • Battery: 2S LiPo only (7.4 V, 8.4 V fully charged), with an XT30 connector. Do not use 3S or higher: above 9 V, EXP6 completely cuts power to LEGO Color / Ultrasonic sensors (their lights stay off and the readings mean nothing) while the motors keep working, and the library prints a one-time warning. The battery LEDs are also scaled for 2S, so a 3S pack always shows full.
  • EXP6's power is controlled by the hub: EXP6 powers on when the hub turns on; when the hub turns off, the whole EXP6 board loses power and the motors stop at once, so you do not need to unplug the battery (confirmed by measuring a real board). Exception: once a Pybricks program has connected, EXP6 stays in Pybricks mode; to go back to the official App, unplug the battery and plug it back in.

2. Wiring

  • Plug EXP6's Host cable into hub port C (every example on this page uses port C).
  • Plug motors and sensors into expansion ports 1 to 6 on EXP6; the number in the program is the number printed on the board.
  • What you can plug in:
    • LEGO motors: SPIKE medium, large and small motors, Technic angular motors.
    • LEGO sensors: only the SPIKE Color Sensor, Ultrasonic Sensor and Force Sensor.
    • MBC's own sensors (Line Follower 8/16-ch, Laser Ranger, PS2/PS4 receiver, SharpShot Vision) are not used in these examples. To plug them into the EXP6 with Pybricks, see "MBC sensors on the EXP6" in EXP6 × Pybricks (needs EXP6 firmware from 2026-11-03 or later).

Note

We are still checking port by port on a real board whether expansion port numbers "1 to 6" match the numbers printed on the board on every port. If you plug a motor into port 1 but a different number in the program makes it move, please let us know.

3. Official App: install the library first (only once)

The official App needs the EXP6 library (a toolkit of program code) installed on the hub before any of the examples can use it.

  1. Download install_exp6_lib.llsp3 (the installer program).
  2. Connect the hub to the SPIKE App by USB or Bluetooth.
  3. "File" → "Open" → pick install_exp6_lib.llsp3.
  4. Press ▶ to run it once. When the Console at the bottom shows "Installation Successful!", you are done.
  5. From now on, every EXP6 example runs directly, no need to install again.

Tip

  • After you reinstall / update the hub firmware, the library is wiped and you have to run the installer once more.
  • To remove the library: download the official App library (.zip) and run the Uninstall_MBC_exp6_Lib.py inside it.
  • The MBC Blocks website and Pybricks do not need this step (the MBC Blocks website sends the library to the hub automatically; in Pybricks you import MBC_exp6_obj_Lib.py yourself).

5 rules every example on this page follows

Rule Why How
① Wait for the motors to be ready at the start EXP6 only gets power once the hub turns on, and the motors take about 1 second to get ready; any "turn N degrees" command sent before that is simply ignored MBC Blocks website, Pybricks: wait until "device code = 5 (motor)" before starting. Official App: wait 2 seconds (in the official App the device code is always 5, so it cannot be used as a check)
② After "turn N degrees", wait for it to finish The "turn N degrees" command moves on to the next line right away and does not wait for the turn to finish; if you send another "turn N degrees" before the first one is done, the second one is dropped Put a "wait" in between, or keep reading the angle and move on once it gets there
③ Always "stop all" at the end The official App has no automatic protection: when the program ends, the motors keep doing the last command Every example ends with "stop all". In the official App, if you press the hub's stop button mid-run, the motors may keep turning (to be confirmed on a real board) → turning the hub off stops them. On the MBC Blocks website and Pybricks, when you press stop, EXP6 stops the motors by itself within 0.5 seconds
④ In Pybricks, wait with keep_alive_wait The Pybricks version has a watchdog: if EXP6 hears nothing from the hub for more than 0.5 seconds, it stops the motors by itself Always write waits as exp6.keep_alive_wait(milliseconds), not wait(). The MBC Blocks website handles this automatically, so just use the "wait" block
⑤ Speeds are all in % EXP6 speed is a percentage of the motor's top speed All three versions use -100 to 100 (negative = reverse); the numbers are the same, no conversion needed

Stop modes (the number you pick for "when done" or "stop"): 1 = coast (let go, slows down gradually), 2 = brake (stops right away), 3 = hold (stops and actively resists being turned).


Example 1: One motor turns one full turn

What this example does: the motor on expansion port 1 turns one full turn (360 degrees) and the hub display shows the angle; then it turns one full turn back to where it started and the display shows about 0.

What you need

  • 1 SPIKE hub, 1 EXP6 (with battery connected)
  • 1 LEGO motor

How to wire it: EXP6 Host cable → hub port C; motor → EXP6 expansion port 1.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex01_one_turn.llsp3 ex01_one_turn.abp ex01_one_turn.py + library
🟨 Official LEGO App program (Python)
# EXP6 範例 1:一顆馬達轉一圈再轉回來(官方 SPIKE App・Python)
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → EXP6 擴充孔 1;EXP6 接上電池
# 第一次用:先執行一次 install_exp6_lib.llsp3,把函式庫裝進主機
from hub import port, light_matrix
import time
from MBC_exp6_SPIKE_App_Lib import MBC_EXP6

exp6 = MBC_EXP6(port.C)      # EXP6 接在主機 C 孔
time.sleep(2)                # 等 2 秒,讓擴充孔上的馬達準備好(太早下指令會被忽略)

exp6.motor_run_degrees(1, 50, 360, 2)    # 擴充孔 1:速度 50%,轉 360 度(一圈),到了就煞車(2)
time.sleep(1.5)                          # 等它轉完(還沒轉完就送下一個轉角度指令,會被丟掉)
light_matrix.write(str(exp6.get_motor_angle(1)))   # 螢幕顯示現在的角度(大約 360)
time.sleep(2)

exp6.motor_run_degrees(1, 50, -360, 2)   # 角度寫負的 = 反方向轉一圈,回到原來的位置
time.sleep(1.5)
light_matrix.write(str(exp6.get_motor_angle(1)))   # 大約 0
time.sleep(2)

exp6.stop_all(1)             # 最後一定要全部停止(1 = 滑行)
🟦 MBC Blocks website blocks

Example 1 MBC Blocks website blocks

Pybricks code
# EXP6 範例 1:一顆馬達轉一圈再轉回來(Pybricks)
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → EXP6 擴充孔 1;EXP6 接上電池
from pybricks.hubs import PrimeHub
from MBC_exp6_obj_Lib import MBC_EXP6

hub = PrimeHub()
exp6 = MBC_EXP6(3, False)    # 3 = C 孔(1=A、2=B、3=C、4=D、5=E、6=F);False = 一般寫法

while exp6.get_device_id(1) != 5:        # 等擴充孔 1 的馬達準備好(5 = 馬達,開機大約 1 秒)
    exp6.keep_alive_wait(100)

exp6.motor_run_degrees(1, 50, 360, 2)    # 擴充孔 1:速度 50%,轉 360 度(一圈),到了就煞車(2)
exp6.keep_alive_wait(1500)               # 等它轉完。等待一律用 keep_alive_wait,不要用 wait
hub.display.number(exp6.get_motor_angle(1))   # 螢幕顯示現在的角度(大約 360)
exp6.keep_alive_wait(2000)

exp6.motor_run_degrees(1, 50, -360, 2)   # 角度寫負的 = 反方向轉一圈,回到原來的位置
exp6.keep_alive_wait(1500)
hub.display.number(exp6.get_motor_angle(1))   # 大約 0
exp6.keep_alive_wait(2000)

exp6.stop_all(1)             # 最後全部停止(1 = 滑行)

Understanding the program step by step

  1. Connect to EXP6: tell the hub that EXP6 is on port C. The official App writes port.C; Pybricks writes 3 (1=A, 2=B, 3=C...); on the MBC Blocks website it is the "C connect MBC EXP6 6-way motor expander" block.
  2. Wait for the motor to be ready (rule ①).
  3. Turn 360 degrees: "expansion port 1, speed 50, turn 360 degrees, brake when done".
  4. Wait 1.5 seconds for it to finish turning (rule ②), then show the angle on the hub display.
  5. Turn -360 degrees: a negative angle = the opposite direction, back to where it started.
  6. Stop all (rule ③).

What you should see when it runs: the motor turns one full turn and stops, and the display shows about 360; 2 seconds later it turns back one full turn and the display shows about 0 (being off by a few degrees is normal).

Common problems

  • The motor does not move at all? → ① Is the battery connected and charged? ② Is the motor in port 1? ③ Is the Host cable in hub port C?
  • Only the first turn happened, not the second? → The wait in between is too short; the first turn was not finished yet. Make "wait 1.5 seconds" longer.
  • Want it to turn the other way? → Make the angle negative. The official App library only looks at the sign of the angle, so a negative speed does nothing; to keep all three versions behaving the same, speeds on this page are always positive.

Example 2: Three motors turn together

What this example does: 3 motors turn at the same time at different speeds for 3 seconds (slow, medium, fast), then all reverse together for 3 seconds, then all brake and the hub beeps once.

What you need: the same as example 1, but with 3 motors.

How to wire it: EXP6 Host cable → hub port C; motors → EXP6 expansion ports 1, 2, 3.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex02_three_motors.llsp3 ex02_three_motors.abp ex02_three_motors.py + library
🟨 Official LEGO App program (Python)
# EXP6 範例 2:三顆馬達一起轉(官方 SPIKE App・Python)
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → EXP6 擴充孔 1、2、3;EXP6 接上電池
from hub import port, sound
import time
from MBC_exp6_SPIKE_App_Lib import MBC_EXP6

exp6 = MBC_EXP6(port.C)
time.sleep(2)                # 等馬達準備好

exp6.motor_run(1, 30)        # 擴充孔 1:速度 30%(慢)
exp6.motor_run(2, 50)        # 擴充孔 2:速度 50%
exp6.motor_run(3, 70)        # 擴充孔 3:速度 70%(快)
time.sleep(3)                # 一起轉 3 秒

exp6.motor_run(1, -30)       # 速度寫負的 = 反轉
exp6.motor_run(2, -50)
exp6.motor_run(3, -70)
time.sleep(3)

exp6.stop_all(2)             # 全部停止(2 = 煞車,馬上停)
sound.beep(880, 200)
🟦 MBC Blocks website blocks

Example 2 MBC Blocks website blocks

Pybricks code
# EXP6 範例 2:三顆馬達一起轉(Pybricks)
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → EXP6 擴充孔 1、2、3;EXP6 接上電池
from pybricks.hubs import PrimeHub
from MBC_exp6_obj_Lib import MBC_EXP6

hub = PrimeHub()
exp6 = MBC_EXP6(3, False)    # 3 = C 孔

for m in (1, 2, 3):          # 三顆馬達都要準備好(5 = 馬達)
    while exp6.get_device_id(m) != 5:
        exp6.keep_alive_wait(100)

exp6.motor_run(1, 30)        # 擴充孔 1:速度 30%(慢)
exp6.motor_run(2, 50)        # 擴充孔 2:速度 50%
exp6.motor_run(3, 70)        # 擴充孔 3:速度 70%(快)
exp6.keep_alive_wait(3000)   # 一起轉 3 秒

exp6.motor_run(1, -30)       # 速度寫負的 = 反轉
exp6.motor_run(2, -50)
exp6.motor_run(3, -70)
exp6.keep_alive_wait(3000)

exp6.stop_all(2)             # 全部停止(2 = 煞車,馬上停)
hub.speaker.beep(880, 200)

Understanding the program step by step

  1. Wait for all 3 motors to be ready: the MBC Blocks website and Pybricks check "device code = 5" one motor at a time.
  2. Run continuously: "expansion port 1 speed 30", "expansion port 2 speed 50", "expansion port 3 speed 70". Once a run command is sent, the motor keeps turning and the program moves straight on, so all 3 start almost at the same time.
  3. Wait 3 seconds.
  4. Change the speeds to negative → all 3 reverse together, then wait another 3 seconds.
  5. Stop all, 2 = brake, then one beep.

What you should see when it runs: 3 motors turn together, one slow, one medium, one fast; after 3 seconds they all turn the other way; after another 3 seconds they all stop and the hub beeps.

Common problems

  • One of them does not turn? → That motor is in the wrong port number, or is not plugged in firmly. Move it to a different port to find out whether the problem is the motor or the port.
  • Speed 100 is very fast and very strong? → EXP6 drives the motors straight from the battery; at high speed a motor mounted in a mechanism can easily break something, so start with 50 or lower.

Example 3: Light switch (color sensor controls a motor)

What this example does: hold a sheet of white paper in front of the color sensor (bright) → the motor turns and the hub light turns green; take it away (dark) → the motor stops and the light turns red. Hold the left and right buttons together to end the program.

What you need

  • The same as example 1, plus
  • 1 SPIKE Color Sensor, 1 sheet of white paper

How to wire it: EXP6 Host cable → hub port C; motor → expansion port 1; color sensor → expansion port 4.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex03_light_switch.llsp3 ex03_light_switch.abp ex03_light_switch.py + library
🟨 Official LEGO App program (Python)
# EXP6 範例 3:光控開關(官方 SPIKE App・Python)
# 感應器前面亮(放一張白紙)→ 馬達轉、主機燈變綠;暗 → 馬達停、燈變紅
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → 擴充孔 1;顏色感應器 → 擴充孔 4;EXP6 接上電池
from hub import port, button, light
import color
import time
from MBC_exp6_SPIKE_App_Lib import MBC_EXP6

exp6 = MBC_EXP6(port.C)
time.sleep(2)

while not (button.pressed(button.LEFT) and button.pressed(button.RIGHT)):   # 左右鍵一起按住 → 結束
    bright = exp6.get_color_reflection(4)    # 擴充孔 4 的顏色感應器:反射光 0~100(越亮越大)
    print(bright)                            # 在電腦畫面印出來,方便看數字
    if bright > 50:                          # 亮 → 馬達轉
        exp6.motor_run(1, 50)
        light.color(light.POWER, color.GREEN)
    else:                                    # 暗 → 馬達停
        exp6.motor_stop(1, 2)
        light.color(light.POWER, color.RED)
    time.sleep(0.1)

exp6.stop_all(1)             # 結束前一定要全部停止
🟦 MBC Blocks website blocks

Example 3 MBC Blocks website blocks

Pybricks code
# EXP6 範例 3:光控開關(Pybricks)
# 感應器前面亮(放一張白紙)→ 馬達轉、主機燈變綠;暗 → 馬達停、燈變紅
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → 擴充孔 1;顏色感應器 → 擴充孔 4;EXP6 接上電池
from pybricks.hubs import PrimeHub
from pybricks.parameters import Button, Color
from MBC_exp6_obj_Lib import MBC_EXP6

hub = PrimeHub()
exp6 = MBC_EXP6(3, False)    # 3 = C 孔

while exp6.get_device_id(1) != 5:        # 等擴充孔 1 的馬達準備好
    exp6.keep_alive_wait(100)

while not (Button.LEFT in hub.buttons.pressed() and Button.RIGHT in hub.buttons.pressed()):   # 左右鍵一起按住 → 結束
    bright = exp6.get_color_reflection(4)    # 擴充孔 4 的顏色感應器:反射光 0~100(越亮越大)
    print(bright)
    if bright > 50:                          # 亮 → 馬達轉
        exp6.motor_run(1, 50)
        hub.light.on(Color.GREEN)
    else:                                    # 暗 → 馬達停
        exp6.motor_stop(1, 2)
        hub.light.on(Color.RED)
    exp6.keep_alive_wait(100)

exp6.stop_all(1)

Understanding the program step by step

  1. Repeat until the left and right buttons are held together: everything inside keeps running; pressing both buttons at once jumps out.
  2. Read the reflected light: how bright the color sensor on expansion port 4 sees (0 to 100, bigger = brighter), stored in a variable called "brightness" (a variable is a box with a name that holds one number), and also printed on the computer screen.
  3. If brightness > 50 → the motor turns and the light turns green.
  4. Else → the motor stops (brake) and the light turns red.
  5. Wait 0.1 seconds, then check again.
  6. After jumping out → stop all.

What you should see when it runs: with nothing in front of the sensor: red light, motor not moving. Bring the white paper close to the sensor (1 to 2 cm): green light, motor turns. Take it away and it stops again. Hold the left and right buttons together: the program ends and the motor stops.

Common problems

  • Always red / always green? → Look at the numbers printed on the computer screen: note the value with the paper in place and with it away, and change "50" to a value between the two.
  • The official App's numbers differ from the other two versions? → That is normal. The official App library estimates brightness from the sensor's red, green and blue readings, while Pybricks and the MBC Blocks website read the sensor's true reflected light, so the same sheet of paper gives slightly different numbers.
  • Always reads 0? → Is the color sensor in port 4? EXP6 only recognizes the LEGO SPIKE Color Sensor.

Example 4: Control a motor with the hub buttons

What this example does: hold the left button → the motor turns forward; hold the right button → it reverses; let go → it stops. Hold the left and right buttons together to end the program.

What you need: the same as example 1.

How to wire it: EXP6 Host cable → hub port C; motor → expansion port 1.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex04_button_motor.llsp3 ex04_button_motor.abp ex04_button_motor.py + library
🟨 Official LEGO App program (Python)
# EXP6 範例 4:用主機按鈕控制馬達(官方 SPIKE App・Python)
# 按住左鍵 → 正轉;按住右鍵 → 反轉;放開 → 停;左右鍵一起按 → 結束
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → EXP6 擴充孔 1;EXP6 接上電池
from hub import port, button
import time
from MBC_exp6_SPIKE_App_Lib import MBC_EXP6

exp6 = MBC_EXP6(port.C)
time.sleep(2)

while not (button.pressed(button.LEFT) and button.pressed(button.RIGHT)):   # 左右鍵一起按住 → 結束
    if button.pressed(button.LEFT):          # 按住左鍵
        exp6.motor_run(1, 50)                # 正轉,速度 50%
    elif button.pressed(button.RIGHT):       # 按住右鍵
        exp6.motor_run(1, -50)               # 反轉
    else:                                    # 都沒按
        exp6.motor_stop(1, 2)                # 停止(2 = 煞車)
    time.sleep(0.05)

exp6.stop_all(1)             # 結束前一定要全部停止
🟦 MBC Blocks website blocks

Example 4 MBC Blocks website blocks

Pybricks code
# EXP6 範例 4:用主機按鈕控制馬達(Pybricks)
# 按住左鍵 → 正轉;按住右鍵 → 反轉;放開 → 停;左右鍵一起按 → 結束
# 接線:EXP6 的 Host 線 → 主機 C 孔;馬達 → EXP6 擴充孔 1;EXP6 接上電池
from pybricks.hubs import PrimeHub
from pybricks.parameters import Button
from MBC_exp6_obj_Lib import MBC_EXP6

hub = PrimeHub()
exp6 = MBC_EXP6(3, False)    # 3 = C 孔

while exp6.get_device_id(1) != 5:        # 等擴充孔 1 的馬達準備好
    exp6.keep_alive_wait(100)

while not (Button.LEFT in hub.buttons.pressed() and Button.RIGHT in hub.buttons.pressed()):   # 左右鍵一起按住 → 結束
    if Button.LEFT in hub.buttons.pressed():         # 按住左鍵
        exp6.motor_run(1, 50)                        # 正轉,速度 50%
    elif Button.RIGHT in hub.buttons.pressed():      # 按住右鍵
        exp6.motor_run(1, -50)                       # 反轉
    else:                                            # 都沒按
        exp6.motor_stop(1, 2)                        # 停止(2 = 煞車)
    exp6.keep_alive_wait(50)

exp6.stop_all(1)

Understanding the program step by step

  1. Repeat until the left and right buttons are held together (same as example 3).
  2. If the left button is held → expansion port 1 speed 50 (forward).
  3. Else, if the right button is held → speed -50 (reverse).
  4. Neither pressed → stop (brake).
  5. Wait 0.05 seconds, then check the buttons again.
  6. After jumping out → stop all.

What you should see when it runs: hold the left button and the motor turns, let go and it stops; hold the right button and it turns the other way. Hold both together and the program ends.

Common problems

  • Why not use the center button? → The center button is the hub's power switch and also the stop-program button, so the examples on this page do not use it to control the motor.
  • A short delay after pressing before it turns? → The program checks the buttons every 0.05 seconds, and the official App also takes a moment to send the command, so a small lag is normal.
  • Pressed stop in the official App but the motor is still turning? → Rule ③: the official App has no automatic protection. End the program by "holding the left and right buttons together"; if it really will not stop, turn the hub off.

Example 5: Two-wheel car drives a square

What this example does: a two-wheel car drives straight about 35 cm → turns right 90 degrees in place, repeated 4 times, driving a square and coming back to the start.

What you need

  • The same as example 1, plus
  • A two-wheel car (two motors as wheels, wheel diameter 56 mm, wheel spacing 112 mm, i.e. the standard LEGO car)
  • A flat floor area (about 50 × 50 cm)

How to wire it: EXP6 Host cable → hub port C; left wheel motor → expansion port 1, right wheel motor → expansion port 2.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex05_square.llsp3 ex05_square.abp ex05_square.py + library
🟨 Official LEGO App program (Python)
# EXP6 範例 5:兩輪小車走正方形(官方 SPIKE App・Python)
# 接線:EXP6 的 Host 線 → 主機 C 孔;左輪馬達 → 擴充孔 1;右輪馬達 → 擴充孔 2;EXP6 接上電池
# 小車:輪子直徑 56 mm、兩輪距離 112 mm(樂高標準小車)
from hub import port
import time
from MBC_exp6_SPIKE_App_Lib import MBC_EXP6

exp6 = MBC_EXP6(port.C)
exp6.set_motor_inverted(1, True)   # 左輪是反過來裝的:設成「反裝」,速度正的才會往前
time.sleep(2)                      # 等馬達準備好

for side in range(4):              # 正方形有 4 個邊
    exp6.reset_angle(2)                        # 右輪角度歸零,用來量走了多遠
    exp6.drive(1, 2, 40, 40)                   # 左輪孔 1、右輪孔 2,兩輪都 40% → 直走
    while exp6.get_motor_angle(2) < 720:       # 右輪轉滿 720 度(2 圈,大約 35 公分)
        time.sleep(0.01)
    exp6.stop_all(2)                           # 煞車
    time.sleep(0.3)

    exp6.reset_angle(2)
    exp6.drive(1, 2, 30, -30)                  # 左輪往前、右輪往後 → 原地右轉
    while exp6.get_motor_angle(2) > -180:      # 右輪倒轉 180 度 ≈ 車子轉 90 度
        time.sleep(0.01)
    exp6.stop_all(2)
    time.sleep(0.3)

exp6.stop_all(1)                   # 結束前一定要全部停止
🟦 MBC Blocks website blocks

Example 5 MBC Blocks website blocks

Pybricks code
# EXP6 範例 5:兩輪小車走正方形(Pybricks)
# 接線:EXP6 的 Host 線 → 主機 C 孔;左輪馬達 → 擴充孔 1;右輪馬達 → 擴充孔 2;EXP6 接上電池
# 小車:輪子直徑 56 mm、兩輪距離 112 mm(樂高標準小車)
from pybricks.hubs import PrimeHub
from MBC_exp6_obj_Lib import MBC_EXP6

hub = PrimeHub()
exp6 = MBC_EXP6(3, False)          # 3 = C 孔
exp6.set_motor_inverted(1, True)   # 左輪是反過來裝的:設成「反裝」,速度正的才會往前

for m in (1, 2):                   # 兩顆馬達都要準備好
    while exp6.get_device_id(m) != 5:
        exp6.keep_alive_wait(100)
exp6.keep_alive_wait(300)

for side in range(4):              # 正方形有 4 個邊
    exp6.reset_angle(2)                        # 右輪角度歸零,用來量走了多遠
    exp6.drive(1, 2, 40, 40)                   # 左輪孔 1、右輪孔 2,兩輪都 40% → 直走
    while exp6.get_motor_angle(2) < 720:       # 右輪轉滿 720 度(2 圈,大約 35 公分)
        exp6.keep_alive_wait(10)
    exp6.stop_all(2)                           # 煞車
    exp6.keep_alive_wait(300)

    exp6.reset_angle(2)
    exp6.drive(1, 2, 30, -30)                  # 左輪往前、右輪往後 → 原地右轉
    while exp6.get_motor_angle(2) > -180:      # 右輪倒轉 180 度 ≈ 車子轉 90 度
        exp6.keep_alive_wait(10)
    exp6.stop_all(2)
    exp6.keep_alive_wait(300)

exp6.stop_all(1)

Understanding the program step by step

  1. Set the left wheel as "inverted": the car's two motors are mounted mirror-image, so the left wheel turns the opposite way from the right wheel. Once it is set as inverted, both wheels follow "positive speed = forward".
  2. Wait for both motors to be ready.
  3. Repeat 4 times (4 sides of the square):
    1. Reset the right wheel angle to zero: we will use it to measure how far the car has gone.
    2. Drive both wheels: "left wheel port 1, right wheel port 2, left 40, right 40" → straight ahead. EXP6 keeps the two wheels in sync, so it drives straighter.
    3. Wait until the right wheel has turned 720 degrees (2 turns ≈ 35 cm) → stop all (brake), pause 0.3 seconds.
    4. Reset the right wheel angle again, "left 30, right -30" → left wheel forward, right wheel backward → turn right in place.
    5. Wait until the right wheel has turned back 180 degrees → the car has turned exactly 90 degrees (wheel turns 180 degrees × wheel diameter 56 ÷ wheel spacing 112 = 90 degrees) → stop.
  4. Stop all (coast).

What you should see when it runs: the car drives a stretch, turns right, drives a stretch, turns right... after 4 sides it ends up roughly back where it started.

Common problems

  • The car spins in place and does not go forward? → The left wheel is not set as inverted, or the two motors are swapped (the left wheel must be in port 1).
  • The car drives backward? → The motors are mounted the opposite way from the standard LEGO car: set expansion port 2 as inverted instead (official App / Pybricks: change the 1 in set_motor_inverted(1, True) to 2; MBC Blocks website: change the expansion port in the "mounting direction" block to 2); no need to change the wiring.
  • Turns less than 90 degrees / more than 90? → The floor is slippery or the wheels are not the same size. Make 180 bigger (not turning enough) or smaller (turning too much), 5 to 10 at a time.
  • The sides are not the same length? → Make 720 bigger or smaller. 720 degrees ≈ 35 cm, so every 100 degrees is about 5 cm.

Want to know more

  • Why is the official App version code instead of blocks? → The official App's blocks can only control LEGO's own parts; EXP6 has to be controlled through the library in Python.
  • Do I always have to wait 2 seconds in the official App? → If the hub has been on for a while and the motors are long ready, you can skip it; but right after power-on you must wait, or the first "turn N degrees" will do nothing.
  • "Absolute angle" is different between the two versions? → The official App library reads "absolute angle" as -180 to 179, while Pybricks gives 0 to 359. The examples on this page do not use absolute angle.
  • Full reference for every command: official App section, Pybricks section
Email for a quoteBuying Guide