MBC Ranger 2-Way (TOF2) × EV3 Pybricks

Read the Ranger on the EV3 with MicroPython in three steps

MicroPython on the EV3 always goes in this order. First import. Then create an object and give the sensor a name. Last, use that name to read values. First pick Pybricks v4 or EV3 MicroPython v2 above. Then follow 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 software:
1

Import the library

The first thing your program does is tell 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 the same way. 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. Open code.pybricks.com in Chrome. Follow the steps on the site to put the Pybricks firmware on the EV3 (the release version already includes the EV3 firmware).
  3. Press "Import a file" in the file list on the left. Import the library into the same project.
  4. Do not rename the file, and do not put it in a sub-folder.
  5. Go back to your main program. Write the lines below at the very top (marked ① in the program on the right).
2

Create an object: name your sensor

A robot can have several sensors, so you "create an object". It tells the program which port the sensor is on, and gives it a name. Every time you read a value, you use this name.

Which port is the sensor on?

Turn on multitasking?

The 1 in the program is the port number: EV3 1~4 = S1~S4. You can also write Port.S1, but then you also need from pybricks.parameters import Port.

3

Use a function

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

Full examples


        

!


        

!


        

!

Good to know

  1. Unplugging works differently in the two programs. Pybricks v4 with the MBC library: when the sensor cannot be read, you get the last value it read (or None if it has never been read). The computer prints one warning line, and the program keeps running. Plug it back in and it works again by itself. To check the cable, use sensor_ok(). If it comes out while avoiding obstacles, the robot keeps driving with the last distance. EV3 MicroPython v2 with Ev3devSensor: you get an OSError and the program stops. Plug the sensor back in and run the program again. Tick "Unplug the sensor" on the right to see both.
  2. 120 cm (1200 mm, 100 steps) 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 s), or that laser is broken. The EV3 cannot tell whether it is broken (is_ok() is SPIKE only). Black, shiny and see-through objects are harder to measure.
  3. Check the unit. On the EV3 the MBC library reads everything from mode 5: cm = mm ÷ 10 (dropping the decimals), so reading cm and mm together is not slower. When you read the modes yourself with EV3 MicroPython v2, modes 0 and 1 use "1 step = 1.2 cm" (0 to 100), and modes 3 and 5 use millimetres. The EV3 needs about 1.1 s to change mode, so use only one mode in the same loop.
  4. 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 is closer, +1 = right is closer, 0 = both the same.
  5. What the EV3 cannot read: is_ok(), init_status(), fault_status(), read_all(), device_id() and firmware_version() are SPIKE only. Mode 5 (used by the library on the EV3) and mode 3 need sensor firmware v4.0 or later. With older firmware, read modes 0 and 1 with EV3 MicroPython v2.