MBC Ranger 2-Way (TOF2) × MBC SPIKE Blocks website

Read the Ranger on the MBC Blocks website in three steps

On the MBC SPIKE Blocks website you do not need to import a library for the Ranger. First, inside "run once at start", tell the website which port the sensor is in. Then add the main loop. Finally, put reading blocks into the main loop. Work through the steps on the left and the blocks on the right grow with you. These block pictures were built on the website and exported, so they look exactly like what you see there.

The website's blocks are in Chinese, so every block below comes with an English explanation. Each block name is written as "Chinese name (English meaning)" so you can match it with what you see on screen. The website is free and needs no login. The hub needs Pybricks firmware (the website's 「SPIKE 主機」 (SPIKE Hub) menu can install it for you). On the website, the Ranger 2-Way blocks are called 「兩路雷射」 (2-way laser).

1

Set up: which port the Ranger is in

The first thing in the program is a 「○ 接 MBC 兩路雷射」 (port ○ has the MBC 2-way laser) block under the yellow 「開機時執行一次」 (run once at start) block. That is how the website knows that port has the Ranger 2-Way.

Which port is the sensor in?
  1. Open the MBC SPIKE Blocks website and press "+" to start a new program.
  2. In the block categories on the left, click 「MBC 感應器」 (MBC sensors) → 「兩路雷射」 (2-way laser). The first block is 「○ 接 MBC 兩路雷射」.
  3. Attach it under 「開機時執行一次」 and pick C in the drop-down.
2

Add the main loop

A sensor has to be read again and again, so you need the yellow 「主迴圈(一直重複)」 (main loop, repeats forever) block. Put a 「等待 100 毫秒」 (wait 100 ms) block inside it first, so it reads every 0.1 seconds and not too fast.

  1. From 「控制」 (Control), drag out 「主迴圈(一直重複)」 and drop it in an empty spot under the set-up block (they do not need to snap together).
  2. Drag a 「等待 100 毫秒」 block into the main loop.

The program the website makes is put into a multitasking structure (async) by itself. You do not need to choose it or write await.

3

Use the reading blocks

「MBC 感應器 → 兩路雷射」 has 17 reading blocks (everything the Pybricks library can do). Each one has a port drop-down in the middle; pick the same port as in step 1 (C). 「左/右雷射距離」 (left/right laser distance) and 「左/右雷射正常」 (left/right laser OK) also have a second drop-down: pick 「左」 (left) or 「右」 (right). 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).

An example for every reading block

17 reading blocks, each with a set of blocks you can copy (port C). Press "Try it" to jump to step 3; the blocks and output on the right change to that example.

Good to know

  1. You only need one set-up block. Each port can be set up only once, and the reading blocks must use the same port as the set-up block; if you pick the wrong port, the website warns you before downloading. You do not need to download a library: when the website downloads your program, it sends the Ranger reading code to the hub as well.
  2. Works the same as the Pybricks library: every function in MBC_laser2.py has a block here (ch(1) / ch(2) = 「左/右雷射距離」, is_ok() = 「左/右雷射正常」), and you get the same values. Unplugging the cable does not stop the program: if the sensor cannot be read, you get the last value it read, and the terminal prints one warning line; everything works again when you plug it back in. Use 「感應器有接好」 (sensor connected) to check whether it is connected (it looks at the result of the last reading, so read a distance first). Tick "Unplug the sensor" on the right to see this. The warning line is in Chinese and English.
  3. A reading of 120 can mean 4 things: there really is nothing in front, the object is farther than 120 cm, the laser is not ready yet right after power-on (about 1 to 2 seconds), or that laser is broken. Use the 「雷射正常」 (laser OK) block to tell them apart. Black, shiny and transparent objects are harder to measure; black objects usually cannot be seen beyond about 30 to 50 cm.
  4. Do not mix centimetres and millimetres in the same loop: the three 「(mm)」 blocks and the centimetre blocks are two different sets of sensor data, and each time you switch sets it takes a few dozen milliseconds more. Millimetres, sensor ID and firmware version need sensor firmware from 2026-10 or later. Read the sensor ID and firmware version once, inside 「開機時執行一次」 (run once at start).
  5. Direction: stand behind the robot and look forward (left and right as seen from behind the sensor: left = J5, right = J3). The 「左」 (left) laser is on the left and the 「右」 (right) laser is on the right. Left minus right = left distance − right distance, so negative = the left side is closer and positive = the right side is closer. 「哪一邊比較近」 (which side is closer) gives −1 = left, 1 = right, 0 = both the same.