MBC Ranger 2-Way (TOF2) × Arduino

Read the Ranger 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 left and right obstacle distances 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 with the lasers pointing forward. "Left" and "right" are as you see them standing behind the robot, looking forward.
  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.

Sensor simulator (seen from above, front of the robot facing up): drag the left and right obstacle distances and watch the Serial Monitor 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). When the sensor is unplugged, every value the Arduino reads is -32768.

An example for every function

19 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.

Park precisely with mm (L298N motor driver)

        

!Only change the "things you can change" at the top. The robot drives forward, slows down within SLOW_MM of the wall and stops at STOP_MM (200 mm = 20 cm by default). A wheel turns the wrong way: set its REVERSE to true. Connect the L298N GND to the Arduino GND.

Is the left or the right nearer? (bar chart)

        

!Ids 0x01–0x03 are next to each other, so one mbcReadMany() call reads them all. Move your hand from left to right and watch the two bars: a longer bar = farther away, one mark per 10 cm.

Laser health check

        

!A broken laser keeps reading 120 / 1200, which looks just like "nothing in front". The 0x06 init bitmap and the 0x07 fault bitmap tell you which it is. This example does not wait for "ready" on purpose, so you can still see which laser failed to start. Run it before a competition.

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. A robot should stop when it cannot read the sensor (the parking robot below does this).
  2. Reading 120 (or 1200 mm) can mean 3 things: there really is nothing in front, the object is farther than 120 cm, or that laser is broken. Read the 0x07 fault bitmap to tell them apart. Black, shiny and see-through things are harder to measure; a black object is usually lost beyond about 30–50 cm.
  3. The two lasers take 1–3 seconds to start. That is why mbcWaitReady() in step 2 waits up to 4 seconds. Do not delete delay(2) in the helper. Set the Serial Monitor to 115200 baud, or you will see strange symbols.
  4. Read neighbours together: ids 0x01–0x08 are next to each other. Read them with one mbcReadMany() call, so they all come from the same moment. A laser only measures about every 0.1 s, so loop() does not need to read faster than that.
  5. Sides and units: standing behind the robot (left and right as seen from behind the sensor: left = J5, right = J3), the left laser is 0x01 (cm) and 0x04 (mm); the right laser is 0x02 and 0x05. Millimetres are 10 times finer than centimetres, so use mm to park precisely. 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.