MBC Ranger 8-Way (TOF8) × micro:bit (MakeCode)

Read the Ranger 8-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 or direction 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 "8-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 8 lasers need 2 to 4 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 5000 (the 8 lasers need 2 to 4 seconds).
  3. From "Basic", drag two "show icon" blocks: one tick, one cross.
  4. Put "pause 100 ms" into "forever".

3

Use the distance and direction blocks

In "MBC Sensors → Distance", the blocks that say "8-ch" are for this sensor: how far and which way the nearest object is, where it is most open, the distance straight ahead and the distance of each laser. 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, robot facing up): drag the obstacle and watch the output on the right

The simulator is only a picture: direction and distance come from a simple position-based estimate, so a real sensor may differ by a step or two; the mm values are just cm × 10. Red sector = that laser sees something; grey sector = it reads 120 (nothing that way). Lasers 4 and 5 both face straight ahead.

An example for every block

Every Ranger 8-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. External 5 V is required: the 8 lasers use a lot of current, so do not power them from the micro:bit 3V pin. Connect the GND of the external supply to the micro:bit GND. All 8 lasers are ready about 2 to 4 seconds after power-on.
  2. Unplugging does not stop the program: when the sensor cannot be read, distance and direction blocks return 0 (true/false blocks return false), and nothing is printed. Careful: 0 looks like "something right in front", so the example "front < 20 → cross" also shows a cross when unplugged. Use "device at … is ready" to tell them apart. The "on distance 8-ch nearer than …" event only counts distances above 0, so unplugging does not fire it.
  3. Direction: stand behind the robot and look forward. Negative = left, positive = right, 0 = straight ahead. Laser 1 is on the far left, laser 8 on the far right, and lasers 4 and 5 face straight ahead. A nearest direction of ±8 means nothing is around.
  4. A reading of 120: nothing that way, the object is farther than 120 cm, or that laser is broken. A broken laser always reads 120, so it looks "most open". Check it with "distance 8-ch sensor … is OK".
  5. 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. JavaScript extras: mbc.readValue(41, 0x01) reads any I2C value directly (−32768 = not connected).