MBC Ranger 8-Way (TOF8) × Pybricks blocks

Read the Ranger 8-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 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 just like your screen.

Check first: Pybricks block coding needs a licence code or a Patreon membership. Without one, write MicroPython instead; it works exactly the same way (switch the right side 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". It tells Pybricks to use the Ranger 8-Way 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_laser8.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_laser8.py.
  2. In Pybricks Code, click "Import a file" in the file area on the left and import it into the same project. Do not rename the file.
  3. Click "+" to add a new file and choose blocks.
  4. Drag the red "from … import …" block from the block list, attach it under "set up", and pick the two names.
2

Create an object: give the sensor a name

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 it come the port and whether to multitask. Every time you read a value, you pick this name.

Which port is the sensor plugged into?

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 means port C.

3

Use a function

Put a red function block under "program": pick the name laser8 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.

Simulated sensor (seen from above, robot facing up): drag the obstacle and watch the output on the right

The simulation is only a sketch: direction and distance come from a simple position-based estimate, so a real sensor may differ by a step or two. Red sector = that sensor sees something; grey sector = it reads 120 (nothing that way). Sensors 4 and 5 both face straight ahead.

A block example for every function

16 functions, each with blocks you can copy (name laser8, multitasking true). Click "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_laser8.py is not imported, you cannot pick MBC_LASER8 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 fails on the hub.
  3. Behind the blocks is MicroPython. The code pane on the right of Pybricks Code is the program the blocks make, 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 it cannot read the sensor, it returns the last value; use the sensor_ok function to check the connection. A reading of 120 can mean nothing there, more than 120 cm away, just switched on (wait 2–4 s) or that sensor is faulty; use is_ok to spot a faulty one. is_ok, init_status, fault_status, read_all, device_id and firmware_version are SPIKE only; the millimetre group (nearest_mm, front_mm, nearest_dir) needs sensor firmware from 2026-10 or later.
  5. Direction: stand behind the robot and look forward: left is negative, right is positive. Sensor 1 points straight left, sensors 4 and 5 point straight ahead, sensor 8 points straight right. The nearest direction is −7 ~ +7, and ±8 = nothing around, so check "is anything there?" with min_distance greater than or equal to 120.