MBC Pathfinder 8-Way × Pybricks built-in blocks

Read the Pathfinder 8-Way with Pybricks built-in blocks, no library

On a SPIKE hub the Pathfinder 8-Way shows up as a LEGO color sensor, so Pybricks' own color sensor blocks can read it: two numbers, reflection() and ambient(). The Pathfinder 8-Way packs values into each; split them with math blocks and use them. Work down the steps on the left and watch the blocks grow on the right; the block pictures are exported from Pybricks Code itself.

How this differs from Pybricks blocks (with the library)
  • This page: nothing to download or import, just open Pybricks; but it gives only 2 numbers (line position, fine position and width), and you split them yourself with // and %.
  • With the library: import MBC_line8.py first, but it gives 16 functions (e.g. 8 light values, which channels are on the line, forks, cable check), one block per function.

Check first: Pybricks block coding needs a license code or a Patreon membership. Without one, write the MicroPython directly; it is exactly the same (switch the right panel to "MicroPython").

1

set up: create the color sensor and pick the port

No library: under the yellow "set up" put Pybricks' own color sensor block (ColorSensor), pick the port the Pathfinder 8-Way is on, and name it line8.

Which port is the Pathfinder 8-Way on?
  1. In Pybricks Code press "+" to add a file and choose blocks.
  2. From "Sensors" drag the color sensor setup block (sensor icon) under "set up", rename it line8 (any English name works) and pick the port above.
2

program: read reflection() and ambient()

Under "program" put "repeat while true", print line8 reflected light (reflection) and line8 ambient light (ambient) inside, then "wait 100 ms". Reading reflection switches the sensor to SPIKE mode 5 and ambient to mode 7 automatically; the Pathfinder 8-Way puts one set of numbers in each:

The simulator reads now:

On Pybricks 4.1, reflection() carries one decimal with sensor firmware before 2026-11-01 (newer firmware sends an exact integer), so always make it whole before splitting; that works on old and new firmware. On the Pathfinder 8-Way it is at most 168 and never negative. ambient(): before the point is fine position + 100, the first decimal is the width (firmware 2026-10-30 or later). Tested with Pybricks Code blocks on Pybricks 4.1.2, 2026-10-29 to 11-01.

3

Split with math blocks and use it

Store the reading in a variable with "set to" from Variables: r = reflection, a = ambient (as is). Then split it with // (whole part of ÷) and % (remainder) from Math, and print or test the values. Click a function and the blocks and output change to its example.

Sensor simulator (looking from behind the robot): drag the black line and watch the output on the right

The simulator is only a picture. The maths is simpler than the real thing, so a real sensor may be off by a channel or two. Light value 0 = black, 100 = white. A red ring = that channel sees the line.

Block examples for every function

Every function has a set of blocks to copy (name line8, port C). "Try it" jumps to step 3, and the blocks and output on the right change to that example.

For more values use Pybricks blocks with the library or MicroPython.

Good to know

  1. Only 2 numbers. reflection() and ambient() hold one each; everything else is out of reach (listed under the step 2 table). For those use Pybricks blocks with the library.
  2. Pybricks 4.1 readings carry decimals: reflection() has one decimal (e.g. 81.3), so make it whole first (floor if ≥ 0, ceiling if negative) before // and %; ambient() keeps only one decimal, so split the integer part and the first decimal separately (floor, round(a × 10) % 10).
  3. No line: the width is 0; the position and fine position stay at the side the line left (−8 / +8, −100 / +100).
  4. Direction: stand behind the robot and look forward. Line on the left = negative, on the right = positive. Channel 1 is on the far left.
  5. Do not use the "color" block or color names. The Pathfinder 8-Way sends numbers: store them in variables and compare numbers. Unplugging the sensor makes the built-in blocks raise an error and stop the program (the library version does not).
  6. Behind the blocks is MicroPython. The code pane on the right of Pybricks Code shows the program the blocks make; it is the same as "MicroPython" on this page.