This page has moved into the new guide: Pathfinder 8-Way › SPIKE

SPIKE beginner examples: Pathfinder 8-Way (LineSensor8)

Back to the Pathfinder 8-Way 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. The 5 examples start with "seeing a number" and build up step by step to "the robot follows the black line on its own and stops at the crossroads". Every example comes in 3 versions; just pick the one you have:

Version Who it's for How to open the example file
🟨 The official LEGO SPIKE App (blocks) Schools, first-time SPIKE users Download the .llsp3 → open the SPIKE App → top-left "File" → "Open" → pick the file
🟦 The MBC Blocks website (blocks, Chinese interface) Anyone who wants to use the Chinese-language blocks or needs to read more sensor data 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 to write code by typing Open code.pybricks.com → "Import a file" in the file panel on the left → import both the example file and MBC_line8_obj_Lib.py → open the example → press ▶ to run

Important

  • For the MBC Blocks website and Pybricks, the hub must be switched to Pybricks firmware first (on the MBC Blocks website, top menu "SPIKE Hub → Install/Update Pybricks firmware" flashes it, about 3 minutes; you can switch back to the original LEGO firmware with the SPIKE App later). The official LEGO App does not need this.
  • The official LEGO App used here is the Education edition (SPIKE 3). The home edition (Robot Inventor) reads the values differently; see the home edition notes.

Before you start: calibrate (only once)

The Pathfinder has to "get to know" the black and white on your mat first, or the numbers will be off. Calibration is saved inside the sensor and survives a restart.

  1. Plug in the sensor and turn on the hub (the sensor's light comes on).
  2. Press and release the calibration button on the sensor once (don't hold it down; holding it makes the sensor keep re-calibrating).
  3. Once the green calibration light comes on, within 5 seconds sweep the sensor back and forth between the black line and the white floor a few times, so all 8 sensor points see both black and white.
  4. Green light goes off = calibration done.

How do I know it worked? Run example 1 below: on the white floor the "line width" is 0, and on a normal black line it is 2 to 3.

The 3 values used on this page

Name Range Meaning
Line position (coarse) -8 to 8 0 = line dead center; negative = line on the left, positive = line on the right
Line position (fine) -100 to 100 Same as above, just in finer steps, so line following is smoother
Line width 0 to 8 How many sensor points are on the black line. 0 = no line found; a normal black line is 2 to 3; 6 or more = crossroads (the whole row is on black)

Tip

When the line disappears, "line position" stays at the last value it saw; it does not go back to 0. So to check "is there a line?", always look at whether line width is 0.

In the official LEGO App the Pathfinder shows up as a "Color Sensor", so you read it with the Color Sensor blocks:

Official block What you get Conversion
reflected light line position (coarse) + 8 subtract 8 from the reading
color line width use as is
raw red (under "More Sensors") line position (fine) + 100 subtract 100 from the reading

Example 1: See the black line

What this example does: the hub display keeps showing the line position (-8 to 8). Slowly move the black line from left to right and the number goes from negative to positive.

What you need

  • 1 SPIKE hub
  • 1 Pathfinder 8-Way (calibrated)
  • A sheet of white paper with a black line drawn on it (or a competition mat)

How to connect: Pathfinder on port C.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex01_show_position.llsp3 ex01_show_position.abp ex01_show_position.py + library
🟨 Blocks in the official LEGO App

Example 1 official App blocks

🟦 Blocks on the MBC Blocks website

Example 1 MBC Blocks website blocks

Pybricks code
# 循行者 8 路 範例 1:看見黑線(顯示線位置)
# 接線:循行者 8 路 → C 埠
from pybricks.hubs import PrimeHub
from pybricks.tools import wait
from MBC_line8_obj_Lib import MBC_LINE8

hub = PrimeHub()
line8 = MBC_LINE8(3)          # 3 = C 埠(1=A、2=B、3=C、4=D、5=E、6=F)

while True:
    pos = line8.pos8()        # 線位置:-8(線在左)~ 0(中間)~ 8(線在右)
    hub.display.number(pos)   # 主機螢幕顯示這個數字
    print(pos)                # 電腦畫面也印出來
    wait(200)                 # 等 0.2 秒再讀下一次

Step by step through the program

  1. Repeat forever: the blocks inside keep running until you press stop.
  2. Read the line position: in the official App it is "reflected light minus 8"; the MBC Blocks website and Pybricks have "line position (-8~8)" directly.
  3. Show it on the display: the official App uses the "write" block, the MBC Blocks website uses "show number on display", and Pybricks uses hub.display.number().
  4. Wait 0.2 seconds: lets the number sit still long enough for your eyes to read it.

What you should see

  • Line right under the sensor: the display shows 0 (-1 and 1 count as center too).
  • Move the black line to the left: the number goes negative, closer to -8 the farther left you go.
  • Move it to the right: it goes positive, closer to 8 the farther right you go.

Common problems

  • The number is stuck at 0 and never changes? → First check whether "line width" is 8 (every point thinks it sees black): that means the sensor is not calibrated yet; go back to "calibrate" above.
  • Left and right are swapped? → The sensor is mounted upside down (the side with the components must face down, toward the floor).
  • The official App shows 8 instead of 0? → You forgot to "subtract 8".

Example 2: Black line alarm

What this example does: when it sees the black line, the hub's center light turns green; when it can't find the line, the light turns red and the hub keeps beeping.

What you need: same as example 1.

How to connect: Pathfinder on port C.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex02_line_alarm.llsp3 ex02_line_alarm.abp ex02_line_alarm.py + library
🟨 Blocks in the official LEGO App

Example 2 official App blocks

🟦 Blocks on the MBC Blocks website

Example 2 MBC Blocks website blocks

Pybricks code
# 循行者 8 路 範例 2:黑線警報(壓到線亮綠燈;找不到線亮紅燈並嗶一聲)
# 接線:循行者 8 路 → C 埠
from pybricks.hubs import PrimeHub
from pybricks.parameters import Color
from pybricks.tools import wait
from MBC_line8_obj_Lib import MBC_LINE8

hub = PrimeHub()
line8 = MBC_LINE8(3)                  # 3 = C 埠

while True:
    if line8.width() > 0:             # 線寬 = 有幾路踩在黑線上;大於 0 就是有看到線
        hub.light.on(Color.GREEN)
    else:                             # 線寬 = 0:一路都沒踩到 → 找不到線
        hub.light.on(Color.RED)
        hub.speaker.beep(880, 100)    # 頻率 880 Hz,響 0.1 秒
    wait(100)

Step by step through the program

  1. Read the line width: in the official App use the "color" block; what it returns is the line width.
  2. If line width > 0 then…: at least one sensor point is on the black line → green light.
  3. Else…: line width is 0, meaning not a single point is on the line → red light and a beep.
  4. Wait 0.1 seconds, then check again.

What you should see

  • Sensor on the black line: green light.
  • Move it onto the white floor: red light + nonstop beeping.

Common problems

  • Always green, even on white paper? → Not calibrated yet (every point is being read as black).
  • How do I pick the light color in the official App? → Click the color dot on the "set Power Button light to" block to change it.

Example 3: Which side is the line on

What this example does: the hub display uses an arrow to tell you where the line is: line on the left → ⬅, line on the right → ➡, in the center → ⬆, no line found → ✕.

What you need: same as example 1.

How to connect: Pathfinder on port C.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex03_which_side.llsp3 ex03_which_side.abp ex03_which_side.py + library
🟨 Blocks in the official LEGO App

Example 3 official App blocks

🟦 Blocks on the MBC Blocks website

Example 3 MBC Blocks website blocks

Pybricks code
# 循行者 8 路 範例 3:線在哪一邊(主機螢幕顯示箭頭)
# 接線:循行者 8 路 → C 埠
from pybricks.hubs import PrimeHub
from pybricks.parameters import Icon
from pybricks.tools import wait
from MBC_line8_obj_Lib import MBC_LINE8

hub = PrimeHub()
line8 = MBC_LINE8(3)                      # 3 = C 埠

while True:
    pos = line8.pos8()                    # 負 = 線在左,正 = 線在右
    if line8.width() == 0:                # 沒看到線(這時位置會停在最後一次的值,不能拿來判斷)
        hub.display.icon(Icon.FALSE)      # 顯示 ✕
    elif pos < -1:
        hub.display.icon(Icon.ARROW_LEFT)
    elif pos > 1:
        hub.display.icon(Icon.ARROW_RIGHT)
    else:                                 # -1、0、1 都算在中間
        hub.display.icon(Icon.ARROW_UP)
    wait(100)

Step by step through the program

  1. First read the line position and line width and store them in two "variables" (a variable = a box with a name that can hold one number).
  2. If line width = 0 → show ✕ (no line found; the line position is not reliable right now, so this check comes first).
  3. Else, if line position < -1 → show the left arrow.
  4. Else, if line position > 1 → show the right arrow.
  5. Otherwise (-1, 0, 1) → show the up arrow, meaning the line is in the center.

What you should see: slowly move the line from left to right and the arrow changes ⬅ → ⬆ → ➡; lift the sensor off the line and it becomes ✕.

Common problems

  • Why is the center -1 to 1 instead of just 0? → The line has some width, so being slightly off still counts as center; otherwise the arrow would flicker back and forth all the time.
  • How do I change the picture in the official App? → Click the little grid image on the "turn on" block and light up whichever pixels you want.

Example 4: The simplest line following

What this example does: the robot follows the black line on its own. Line on the left → turn left; line on the right → turn right; line in the center → go straight.

What you need

  • Everything from example 1, plus
  • A two-wheel robot car (two motors as wheels)
  • A closed loop of black line (about 2 cm wide, no sharp turns)

How to connect

  • Pathfinder on port C, mounted at the front of the car, about 0.5 to 1 cm above the floor
  • Left wheel motor on E, right wheel motor on F

Download the program

Official LEGO App MBC Blocks website Pybricks
ex04_simple_follow.llsp3 ex04_simple_follow.abp ex04_simple_follow.py + library
🟨 Blocks in the official LEGO App

Example 4 official App blocks

🟦 Blocks on the MBC Blocks website

Example 4 MBC Blocks website blocks

Pybricks code
# 循行者 8 路 範例 4:最簡單的循線(線在左就左轉、線在右就右轉、中間就直走)
# 接線:循行者 8 路 → C 埠;左輪馬達 → E 埠;右輪馬達 → F 埠
from pybricks.parameters import Direction, Port
from pybricks.pupdevices import Motor
from pybricks.robotics import DriveBase
from pybricks.tools import wait
from MBC_line8_obj_Lib import MBC_LINE8

line8 = MBC_LINE8(3)                                   # 3 = C 埠
left = Motor(Port.E, Direction.COUNTERCLOCKWISE)       # 左輪:反過來裝,所以設成逆時針才是往前
right = Motor(Port.F)                                  # 右輪
robot = DriveBase(left, right, wheel_diameter=56, axle_track=112)   # 輪子直徑 56 mm、兩輪距離 112 mm

while True:
    pos = line8.pos8()
    if pos < -1:                  # 線在左邊 → 一邊前進一邊左轉
        robot.drive(100, -60)     # 速度 100 mm/秒,每秒左轉 60 度
    elif pos > 1:                 # 線在右邊 → 右轉
        robot.drive(100, 60)
    else:                         # 線在中間 → 直走
        robot.drive(100, 0)
    wait(10)

Step by step through the program

  1. Tell the hub where the wheels are: in the official App it is "set movement motors to E+F"; on the MBC Blocks website and in Pybricks it is "drive base: left wheel E, right wheel F".
  2. Set the speed: 30% in the official App; 100 mm per second in the other two. Start slow, and speed up once it works.
  3. Repeat forever: read the line position.
  4. If line position < -1 (line on the left) → turn left while driving.
  5. Else if line position > 1 (line on the right) → turn right.
  6. Else → go straight.

What you should see: the car follows the line, wobbling left and right as it goes (that is normal; example 5 improves it).

Common problems

  • It shoots off right away and gets farther and farther from the line? → The turn directions are reversed: swap the sign of the two turn numbers (official App: swap -40 and 40; the other two: swap -60 and 60).
  • The car drives backward? → The motors are mounted the opposite way from LEGO's standard car. Official App: mount the motors the same way as LEGO's "Driving Base" model; MBC Blocks website: in the "drive base" block, flip the "forward" direction of both the left and right wheels; Pybricks: change the left wheel to Direction.CLOCKWISE and the right wheel to Motor(Port.F, Direction.COUNTERCLOCKWISE).
  • It overshoots and runs off on sharp turns? → Lower the speed.

Example 5: Smooth line following + stop at the crossroads

What this example does: follows the line using "turn as much as you are off the line", which is smoother than example 4; when it reaches a crossroads (the whole row of sensor points is on black), it stops and beeps.

What you need: same as example 4, and the black line needs one crossroads.

How to connect: Pathfinder on port C; left wheel motor on E, right wheel motor on F.

Download the program

Official LEGO App MBC Blocks website Pybricks
ex05_smooth_follow_stop.llsp3 ex05_smooth_follow_stop.abp ex05_smooth_follow_stop.py + library
🟨 Blocks in the official LEGO App

Example 5 official App blocks

🟦 Blocks on the MBC Blocks website

Example 5 MBC Blocks website blocks

Pybricks code
# 循行者 8 路 範例 5:平順循線,遇到十字路口就停車
# 接線:循行者 8 路 → C 埠;左輪馬達 → E 埠;右輪馬達 → F 埠
from pybricks.hubs import PrimeHub
from pybricks.parameters import Direction, Port
from pybricks.pupdevices import Motor
from pybricks.robotics import DriveBase
from pybricks.tools import wait
from MBC_line8_obj_Lib import MBC_LINE8

hub = PrimeHub()
line8 = MBC_LINE8(3)                                   # 3 = C 埠
left = Motor(Port.E, Direction.COUNTERCLOCKWISE)
right = Motor(Port.F)
robot = DriveBase(left, right, wheel_diameter=56, axle_track=112)

while True:
    if line8.width() >= 6:        # 6 路以上同時踩到黑線 → 十字路口
        robot.stop()
        hub.speaker.beep(880, 500)
        break                     # 跳出迴圈,程式結束
    pos = line8.pos100()          # 更精細的位置:-100 ~ 100
    robot.drive(120, pos * 1.5)   # 偏多少就轉多少:偏越多轉越快
    wait(10)

Step by step through the program

  1. Read the fine line position (-100 to 100): in the official App it is "raw red minus 100".
  2. Read the line width.
  3. If line width ≥ 6 (the official App writes it as "line width > 5", same meaning) → crossroads: stop → beep → end the program.
  4. Else → turn amount = line position × a number (0.5 in the official App, 1.5 in the other two). The farther off the line, the harder it turns; slightly off means only a slight turn, so it doesn't wobble left and right.

What you should see: the car glides smoothly along the line and stops with a beep at the crossroads.

Common problems

  • Still wobbling left and right? → Make "the multiplier" a bit smaller (for example 0.5 → 0.3).
  • Can't keep up on turns and runs off the line? → Make "the multiplier" a bit bigger, or lower the speed.
  • Stops before reaching the crossroads? → The line is too wide or the sensor is too close to the floor; change "6" to 7.
  • Can't find the "raw red" block in the official App? → Opening the file should automatically add the "More Sensors" block category; if it is missing, click the "+" at the bottom-left of the block palette and add "More Sensors".

Next steps

Email for a quoteBuying Guide