MBC Pathfinder 16-Way × Pybricks built-in blocks

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

On a SPIKE hub the Pathfinder 16-Way shows up as a LEGO color sensor, so Pybricks' own color sensor blocks can read it: two numbers, reflection() and ambient(). The Pathfinder 16-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_line16.py first, but it gives 16 functions (e.g. 16 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 16-Way is on, and name it line16.

Which port is the Pathfinder 16-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 line16 (any English name works) and pick the port above.
2

program: read reflection() and ambient()

Under "program" put "repeat while true", print line16 reflected light (reflection) and line16 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 16-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 first; that works on old and new firmware. Above 3199 it turns negative (on the Pathfinder 16-Way at line position +16), and adding 6399 after making it whole fixes it. Make it whole by truncating toward 0: use "ceiling" for negatives, not "floor" (measured: floor splits into 15, 99; the correct result is 16, 0). ambient(): before the point is fine position + 100, the first decimal is the width (shows at most 9; 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 forward from behind the robot): drag the black line and watch the output on the right change

The simulator is only a sketch: the conversion is simplified, so a real sensor may differ by one or two steps. The number in each circle is the light value of that channel (0 = black, 15 = white). Red ring = that channel counts as on the line.

Block examples for every function

Every function has a set of blocks to copy (name line16, 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. reflection() can turn negative: at line position +16 (line lost on the right) the value passes 3199 and Pybricks shows it as negative. Always make it whole first, then "if r < 0, add 6399", and only then split.
  4. No line: the width is 0; the position and fine position stay at the side the line left (−16 / +16, −100 / +100).
  5. Direction: stand behind the robot and look forward. Line on the left = negative, on the right = positive. Channel 1 is on the far left.
  6. Do not use the "color" block or color names. The Pathfinder 16-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).
  7. 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.