MBC Ranger 2-Way (TOF2) × MATRIX Mini R4 (MATRIXblock)

Read the Ranger 2-Way (TOF2) with MATRIXblock in three steps

In MATRIXblock, the software for the MATRIX Mini R4, the Ranger 2-Way (TOF2) shows up as a stock "colour sensor" (I2C address 0x22), so the built-in blocks can read it and no library is needed. First put Color Sensor Initialize in Setup of "Mini Begin", then a wait in Loop, and finally the colour sensor reading blocks in the loop. Follow the steps on the left and the blocks on the right build up; these pictures are drawn by MATRIXblock's own block code, so they look the same as in the software.

MATRIXblock is the free block software from MATRIX (installed in C:\matrixblock-r4\ by default). Buy the MATRIX edition of the sensor and connect it to I2C1 or I2C2 with the stock 4-pin sensor cable.

1

Set up: which port the Ranger 2-Way (TOF2) uses

The first thing in the program is a "Color Sensor ○ Initialize" block inside "Setup" of the blue "Mini Begin" block, so the MATRIX starts talking to the Ranger.

Which port is the sensor plugged into?
  1. Open MATRIXblock. A new project already has a blue "Mini Begin" block (if not, drag one from the "Mini Core" category).
  2. Click the "Sensing" category and find "Color Sensor I2C1 Initialize" (not "Color Sensor V3").
  3. Put it inside "Setup" and pick I2C1 in the dropdown, the same port the sensor cable is plugged into.
2

Put a wait in the loop

Blocks inside "Loop" repeat forever. First add "wait 100 millisecond" so it reads every 0.1 seconds and the Information Window does not scroll too fast.

  1. Drag "wait 1000 millisecond" from "Control" into "Loop" and change the number to 100.

Leave "UART: On" and "Baud: 9600" on "Mini Begin" as they are: the examples use "USB Serial Print … with New Line" to show values in the Information Window (set the Information Window to 9600 too).

3

Use the reading blocks

The "Color Sensor I2C1 Red" block in "Sensing" has 7 usable slots in its dropdown: Red, Green, Blue, Cyan, Magenta, Yellow and Key. Each slot holds one value from the Ranger; the cards below tell you what each slot is and how to work it out. Pick the same port as in step 1 (I2C1). Click a function and the blocks and output change to its example; the example goes inside "Loop", above the wait.

Simulated sensor (seen from above, front of the robot at the top): drag the left and right obstacle distances and watch the output 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

7 examples you can copy block by block (port I2C1); they go inside "Loop", above the wait. Press "Try it" to jump to step 3; the blocks and output on the right change to that example.

Things to know

  1. Use the seven slots only: in the "Color Sensor" dropdown pick Red, Green, Blue, Cyan, Magenta, Yellow or Key (R, G, B, C, M, Y, K), each 0 to 255. Do not pick ColorID, Hue, Saturation or Value (the controller works those out from Red, Green and Blue, so they mean nothing for MBC products), and do not use the "Color Sensor V3" blocks (they are for another MATRIX sensor and cannot read the Ranger 2-Way (TOF2)).
  2. Unplugging does not stop the program: when the MATRIX library cannot reach the sensor, all seven slots read 255 and the program keeps running. Use the "Is it plugged in?" card to check; plug it back in and it works again.
  3. Only the first three slots have data: Red = left, Green = right, Blue = nearer, all in cm from 0 to 120. Cyan, Magenta, Yellow and Key are 0. MATRIXblock cannot read millimetres, init or fault data; use Arduino, mBlock or micro:bit for those.
  4. A reading of 120 can mean 4 things: nothing in front, something farther than 120 cm, the laser has just powered on (about 1 to 2 seconds), or that laser is broken. Black, shiny and see-through things are harder to measure.
  5. Direction: stand behind the robot and look forward: the left laser is on the left, the right laser on the right (as seen from behind the sensor: left = J5, right = J3).