MBC Pathfinder 16-Way × Pybricks blocks

Read the Pathfinder 16-Way with Pybricks blocks in three steps

Blocks follow the same order as MicroPython: first import the library under "set up", then create an object and give the sensor a name, and finally use that name under "program" to call functions. Work through the steps on the left and the blocks on the right grow with you. These block pictures were built and exported straight from Pybricks Code, so they look exactly like your screen.

Check first: the Pybricks block mode needs a licence code or a Patreon membership. Without one, write MicroPython instead. It works exactly the same way (switch the right panel to "Matching MicroPython" to see it).

1

Import the library

The first thing in a block program is a red "from … import …" block under the yellow "set up" block. It tells Pybricks to use the Pathfinder 16-Way library.

Choose one library (the blocks are exactly the same; only the name after from is different):

  1. Download the library (save the file and keep its name): MBC_line16.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_line16.py.
  2. In Pybricks Code, press "Import a file" in the file panel on the left and import it into the same project. Do not rename the file.
  3. Press "+" to add a new file and choose blocks.
  4. From the block list, drag out the red "from … import …" block, attach it under "set up", and pick the two names.
2

Create an object: give the sensor a name

Next, drag a red "… is a …" block and attach it under the import block. On the far left is the name you give the sensor. After that come the port and whether to use multitasking. Every time you read a value, you pick this name.

Which port is the sensor on?

Give it a name (in Pybricks you can use any English name; here are three examples):

Turn on multitasking? (the green true / false on the block)

The port box takes a number: SPIKE 1~6 = A~F, so 3 is port C.

3

Use the functions

Under "program", place the red function block: pick the name line16 in the first box, await in the middle, and the function in the last box. 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 example for each function

16 functions, each with a set of blocks you can copy (name line16, multitasking true). Press "Try it" to jump to step 3. The blocks and output on the right change to that example.

Things to know

  1. Import the library file into the project first. If MBC_line16.py is not in the project, you cannot pick MBC_LINE16 in the red blocks.
  2. await and call must match multitasking: multitasking true → choose await; multitasking false → choose call. If you mix them, the program fails on the hub.
  3. 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 "Matching MicroPython" on this page (the multitasking version has an extra await wait(0) line that Pybricks adds by itself).
  4. Unplugging does not stop the program: if the sensor cannot be read, you get the last value it read. The library prints one warning line in Chinese and English (MBC: Port C 讀不到感應器… / no sensor…). Use the sensor_ok function to check if it is connected. When the line is lost, pos16 gives −16 / +16 and pos100 gives −100 / +100 (the side where the line left). To check "really no line", check that width equals 0.
  5. Light values are 0 to 15: on SPIKE, each of the 16 channels only goes from 0 to 15 (not 0 to 100); below 8 counts as black. For a crossroads, check whether width is 11 or more; junction (line segments) is still 1 at a crossroads.
  6. Direction: stand behind the robot and look forward. A line on the left gives a negative number, a line on the right gives a positive number. Channel 1 is on the far left, channel 16 on the far right.