MBC Ranger 8-Way (TOF8) × Arduino

Read the Ranger 8-Way with Arduino in three steps

An Arduino program goes in this order. First wire it up and paste the helper. Then, in setup(), start talking and wait until the sensor is ready. Last, in loop(), read values with mbcRead(). Follow the steps on the left, and the program on the right grows with you. Drag the obstacle direction and distance under the sensor simulator to see what the Serial Monitor shows.

1

Wire it up and paste the helper

Arduino talks to the sensor over I2C (4 wires). You do not need to install a library. Write 2 lines at the very top and paste a "helper" at the very bottom. Then you can read values with mbcRead() and send commands with mbcWrite().

Which board do you use? (Only the SDA / SCL pins change. The program is the same.)

  1. Unplug the USB cable before wiring. Use the Universal I2C edition (the SPIKE and EV3 editions have different firmware and cannot connect to Arduino).
  2. Sensor VCC → 5V, GND → GND
  3. Sensor SDA → A4, SCL → A5
  4. Mount the sensor at the front of the robot. Standing behind the robot, looking forward: laser 1 points straight left, lasers 4 and 5 point straight ahead, laser 8 points straight right, 30° apart.
  5. Open the Arduino IDE: File → New, and delete what is already there.
  6. Write lines 1 and 2 at the very top. Paste the "helper" at the very bottom (marked ① in the program on the right; "Copy program" copies it too).
2

Start talking and wait until the sensor is ready

setup() runs only once, when the board starts. It opens the serial port, starts I2C and waits until the sensor says "I am ready". ADDR is the sensor's "house number" (its I2C address). You write it every time you read a value.

The sensor's I2C address:

If the sensor is not found:

3

Use a function

To read a value, write "mbcRead(ADDR, id)". To send a command, write "mbcWrite(ADDR, command)". Click a function, and the program and the Serial Monitor change to its example.

Simulated sensor (seen from above, robot facing up): drag the obstacle and watch the Serial Monitor on the right

The simulation is only a sketch: direction and distance come from a simple position-based estimate, so a real sensor may differ by a step or two. Red sector = that sensor sees something; grey sector = it reads 120 (nothing that way). Sensors 4 and 5 both face straight ahead. When the sensor is unplugged, every value the Arduino reads is -32768.

An example for every function

21 functions (values, plus the shared ids and commands every MBC sensor has). Each one has a short example. Press "Try it" to jump to step 3. The program and the Serial Monitor on the right change to that example.

Full examples

Press "Copy" to get the whole program (the helper is already at the bottom). Paste it into the Arduino IDE and upload.

Obstacle-avoiding robot (L298N motor driver)

        

!Only change the "things you can change" at the top. The robot drives straight while the way is clear. When something is within TURN_CM straight ahead, it turns away from it; when it is about to hit (within BACK_MM), it backs up first. A wheel turns the wrong way: set its REVERSE to true. Connect the L298N GND to the Arduino GND.

Radar bar chart

        

!Read the 8 lasers in cm (0x10–0x17) in one go and draw 8 bars: a longer bar = more open that way, a short bar = something is in the way. It also reads the 0x08 fault bitmap and marks a broken laser with XX.

Which way is the nearest object?

        

!The sensor compares the 8 lasers itself: 0x01 nearest distance and 0x02 nearest direction. It draws a little ruler and marks the direction with * (+ = straight ahead). A sumo robot "finds the opponent" like this. Always check the distance first: with nothing within 120 cm, the direction is ±8 and cannot be trusted.

Good to know

  1. Unplugging does not stop the program. When the sensor cannot be read, mbcRead() gives -32768 (MBC_NONE) and mbcReadMany() gives false. Plug it back in and it works again. To check the cable, see if the value is MBC_NONE. Tick "Unplug the sensor" on the right to see this. An obstacle-avoiding robot should stop when it cannot read the sensor (the full example does this).
  2. Wait 2–4 seconds at power-on. The sensor wakes up the 8 lasers one by one. Until it is ready every distance is 0, which looks like an obstacle right in front of it. That is why mbcWaitReady() in step 2 waits up to 6 seconds (the same as the website examples). If you pick "Keep going", do not trust the 0s in the first few seconds.
  3. Direction: stand behind the robot and look forward: left is negative, right is positive. When something is seen, the direction is −7 ~ +7 (−7 straight left, 0 straight ahead, +7 straight right). ±8 = nothing around (it keeps the side where it last saw something). So check the distance first, then the direction: only look at the direction when the distance is below 120.
  4. Reading 120 (or 1200 mm): there really is nothing that way, it is farther than 120 cm, or that laser is broken. A broken laser keeps reading 120 and looks "most open"; read the 0x08 fault bitmap to tell. Black, shiny and see-through things are harder to measure, so keep your distance checks within about 60 cm.
  5. Read neighbours together: 0x01–0x09, 0x10–0x17 (8 lasers in cm) and 0x18–0x1F (8 lasers in mm) are each a run of neighbouring ids. Read each run with one mbcReadMany() call (up to 16 ids at a time), so the values come from the same moment. Do not delete delay(2) in the helper, and set the Serial Monitor to 115200. The Ranger 2-Way and 8-Way both leave the factory at 0x29; to use them together, give one a new address with F8 first.