MBC Ranger 2-Way (TOF2) × Pybricks blocks

Read the Ranger 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. Work through the steps on the left and the blocks on the right grow with you. These block pictures were built in Pybricks Code and exported, so they look exactly like what you see on your screen.

Check first: block mode in Pybricks needs a licence code or a Patreon membership. Without one, you can write MicroPython instead; it works exactly the same way (switch to "Matching MicroPython" on the right 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 Ranger library.

Pick 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_laser2.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_laser2.py.
  2. In Pybricks Code, click "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. 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. The left-most part is the name you give the sensor; after it come the port and whether to use multitasking. From now on, every time you read a distance, you pick this name.

Which port is the sensor in?

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 the functions

Put red function blocks under "program": pick the name laser2 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 (seen from above, front of the robot facing up): drag the left and right obstacle distances and watch the output on the right change

The simulator is only a picture: the distance comes straight from where the obstacle is on the screen. A real laser can be off by 1 to 2 cm and only updates about every 0.1 seconds. 120 = nothing in front (or farther than 120 cm).

Block examples for every function

17 functions, each with a set of blocks you can copy (name laser2, 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_laser2.py is not imported, you cannot pick MBC_LASER2 in the red blocks.
  2. await and call must match multitasking: multitasking true means await, multitasking false means 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 is 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 the cable does not stop the program: if the sensor cannot be read, you get the last value it read. Use the sensor_ok function to check whether it is connected. A reading of 120 can mean nothing in front, too far away, not ready yet right after power-on, or that laser is broken; use is_ok to tell them apart. Do not mix centimetres (left and so on) and millimetres (left_mm and so on) in the same loop; each time you switch, it takes a few dozen milliseconds more.
  5. Direction: stand behind the robot and look forward (left and right as seen from behind the sensor: left = J5, right = J3). Laser 1 is on the left and laser 2 is on the right. diff = left minus right, so a negative number means the left side is closer.
  6. The library's own messages are in Chinese and English, for example MBC: Port C 讀不到感應器,先回傳上一次的值 / no sensor, using the last value.