MBC Pathfinder 8-Way (Line8) × Pybricks blocks

Read the Pathfinder 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. Last, use that name under "program" to call a function. Follow 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 the same as on your screen.

Check first: Pybricks block coding needs a licence code or a Patreon membership. Without one, you can 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 library.

Pick one library. The blocks are the same. Only the name after "from" is different.

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

Create an object: name your sensor

Drag a red "… is a …" block and attach it under the import block. The far-left box 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 a function

Put a red function block under "program". In the first box pick the name line8, in the middle pick await, and in the last box pick the function. 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

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

Good to know

  1. Import the library file into the project first. If MBC_line8.py is not imported, you cannot pick MBC_LINE8 in the red blocks.
  2. await and call must match multitasking: multitasking true → pick await; multitasking false → pick call. If you mix them, the program gives an error 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 one extra line, await wait(0), which Pybricks adds by itself).
  4. Unplugging does not stop the program: when the sensor cannot be read, you get the last value it read. Use the sensor_ok function to check the cable. When the line is lost, pos8 gives −8 / +8 and pos100 gives −100 / +100 (the side where the line left). To check "I really cannot see a line", use width equals 0.
  5. Direction: stand behind the robot and look forward. Line on the left = negative, line on the right = positive. Channel 1 is on the far left.