MBC Ranger 2-Way (TOF2) × micro:bit (MakeCode)

Read the Ranger 2-Way with a micro:bit in three steps

In MakeCode for micro:bit, first add the MBC extension, then wait for the sensor in "on start", and last put a distance block into "forever". Follow the steps on the left, and the blocks on the right grow with you. The block pictures come from MakeCode's own renderer, so they look the same as in the editor. Switch to JavaScript at the top right, just like the switch in MakeCode.

MakeCode is free and needs no login. Open it in Chrome or Edge. Click "Download" to save a .hex file and drag it onto the micro:bit.

1

Add the MBC extension and wire it up

The micro:bit does not know the Ranger yet. After you add the MBC extension, an "MBC Sensors" category appears, and the Ranger blocks are in its "Distance" group (the ones that say "2-ch" are for this sensor). Pick your micro:bit version first; the hardware check below changes with it.

Which micro:bit do you have? (V2 has "V2" printed on the back)
  1. Open MakeCode and click "New Project".
  2. At the bottom of the toolbox, click "Advanced" → "Extensions".
  3. Paste https://github.com/contactmbcrobot/pxt-mbc-sensors into the search box, press Enter and click mbc-sensors.
  4. The blocks are in English. (Traditional Chinese is also included in the extension.)
2

On start: wait for the sensor

The two lasers need 1 to 2 seconds to start. The program first waits for them: tick = connected, cross = not connected. You find wiring problems at once.

I2C address of the sensor
  1. From "Logic", drag "if … then … else" into "on start".
  2. From "MBC Sensors" → "more", drag "wait for device at … ready, timeout … ms" into the condition. Pick 41 (0x29) and type 3000.
  3. From "Basic", drag two "show icon" blocks: one tick, one cross.
  4. Put "pause 100 ms" into "forever".

3

Use the distance blocks

In "MBC Sensors → Distance", the blocks that say "2-ch" are for this sensor: left and right distance, nearest, average, laser OK and an event when something comes close. The "Device" group has shared blocks that every MBC sensor has. Pick one, and the blocks and the output switch 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

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 block

Every Ranger 2-Way block and every Device block has an example you can copy (address 0x29). Click "Try it" to jump to step 3, and the blocks and output on the right switch to that example.

Good to know

  1. Unplugging does not stop the program: when the sensor cannot be read, distance blocks return 0 (true/false blocks return false), and nothing is printed. Careful: 0 looks like "something right in front", so the example "nearest < 20 → cross" also shows a cross when unplugged. Use "device at … is ready" to tell them apart. The "on distance 2-ch nearer than …" event only counts distances above 0, so unplugging does not fire it.
  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 "distance 2-ch … sensor is OK" 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.
  3. Centimetres and millimetres: pick "cm" in a distance block for 0 to 120, or "mm" for 0 to 1200. The average distance is only in mm.
  4. The 2-way and 8-way both use 0x29: to connect both to the same P19 / P20, first move one with "change device at … to address …", then pick "changed address" in step 2.
  5. JavaScript extras: mbc.readValue(41, 0x01) reads any I2C value directly (−32768 = not connected). The value ids are in the I2C reference on the product website.