MBC Ranger 2-Way (TOF2) × Pybricks

Read the Ranger with MicroPython in three steps

A Pybricks program always goes in this order: first import the library, then create an object and give the sensor a name, and only then use that name to call a function. Work through the steps on the left and the program on the right grows with you. Drag the left and right obstacle distances under the sensor simulator to see what the program prints.

Your hub:
1

Import the library

The first thing in the program tells Pybricks "I want to use the Ranger library". Without this line the hub does not know what MBC_LASER2 means, and you get a NameError.

Pick one library (both work exactly the same; only the import line 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.
  3. Do not rename the file, and do not put it in a subfolder.
  4. Go back to your main program and type the lines below at the very top (marked ① in the program on the right).
2

Create an object: give the sensor a name

A robot can have several sensors, so you "create an object": you tell the program which port the sensor is in and give it a name. After that, every time you want a distance, you use this name.

Which port is the sensor in?

Turn on multitasking?

The 3 in the program is the port number: SPIKE 1~6 = A~F. You can also write Port.C, but then you also need to import from pybricks.parameters import Port.

3

Use the functions

You always write "laser2.function_name()": the name, a dot, then the function. Click a function and the program and the 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 function

17 functions, each with a short example you can put straight into a loop. Press "Try it" to jump to step 3; the program and output on the right change to that example.

Full examples

Back up and turn when something is in front (SPIKE, left wheel E, right wheel F)

        

!Only change the "settings to change" at the top. If the robot hits things before it stops, make NEAR bigger or SPEED smaller. If the robot drives backwards, move Direction.COUNTERCLOCKWISE to the other motor. Mount the sensor on the front of the robot, facing straight ahead, at least 5 cm above the floor.

Multitasking version (advanced)

        

!Write True as the 2nd value when you create the object, and create the object outside run_task(). Put await in front of every function.

Use it with other MBC sensors

        

!Switch to the all-in-one library: only change the import line and import MBC_sensors.py. Do not import both libraries at the same time.

Good to know

  1. Unplugging the cable does not stop the program. If the sensor cannot be read, you get the last value it read. If it has never read anything, you get None. The computer screen prints one warning line, and everything works again when you plug it back in. Use sensor_ok() to check whether the sensor is connected. Tick "Unplug the sensor" on the right to see this. If the cable comes out while avoiding obstacles, the robot keeps driving using the last distance.
  2. 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 is_ok() (SPIKE only) 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, and objects very close (within a few cm) can also read as 120.
  3. SPIKE: do not mix centimetres and millimetres in the same loop. The centimetre group (left(), right(), ch(), average(), min_distance(), min_side(), diff(), is_ok(), init_status(), fault_status(), read_all()) and the millimetre group (left_mm(), right_mm(), average_mm()) are two different sets of sensor data. Each time you switch sets, it takes a few dozen milliseconds more. Put device_id() and firmware_version() outside the loop. Millimetres need sensor firmware from 2026-10 or later. On EV3, centimetres and millimetres come in the same set, so this does not matter there.
  4. No need to read very fast. The lasers measure about once every 0.1 seconds, so wait(20) to wait(50) in the loop is enough. The same set of data is read from the sensor at most once every 10 ms, so calling several functions in a row is fine.
  5. Direction: stand behind the robot and look forward (left and right as seen from behind the sensor: left = J5, right = J3). Laser 1 (ch(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. min_side() gives −1 = left side is closer, +1 = right side is closer, 0 = both the same.
  6. The library's own messages are in Chinese and English, for example MBC: Port C 讀不到感應器,先回傳上一次的值 / no sensor, using the last value.