MBC Pathfinder 16-Way (Line16) × Arduino

Read the Pathfinder 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 black line 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. On the robot, keep the bottom of the sensor about 0.5–1 cm above the floor for the best readings.
  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 (looking from behind the robot): drag the black line and watch the Serial Monitor on the right

The simulator is only a picture. The maths is simpler than the real thing, so a real sensor may be off by a channel or two. The number in each circle is the light value of that channel (on Arduino: 0 = black, 100 = white). A red ring = that channel sees the line (light value below the threshold 40). When the sensor is unplugged, every value the Arduino reads is -32768.

An example for every function

25 functions (values, commands, and the shared ids 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.

Line-following robot (L298N motor driver)

        

!Only change the "things you can change" at the top. Robot wobbles left and right: make KP smaller. Robot cannot make the turn: make it bigger. A wheel turns the wrong way: set its REVERSE to true. Connect the L298N GND to the Arduino GND.

Read many ids at once (mbcReadMany)

        

!Ids 0x01–0x08 are next to each other. One call gives them all from the same moment, and it is faster than 8 calls. v[0] is 0x01 and v[7] is 0x08.

Use it with other MBC sensors

        

!Several sensors can share one I2C bus (SDA, SCL) as long as their addresses are different: Pathfinder 0x16, Ranger 0x29. Paste the helper only once.

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 line-following robot should stop when it cannot read the sensor.
  2. The sensor updates every 10 ms. Read ids that are next to each other (for example 0x01–0x08 or 0x10–0x1F) with one mbcReadMany() call. Then they all come from the same moment.
  3. Do not delete delay(2) in the helper. After writing the id, wait 2 ms before reading. Set the Serial Monitor to 115200 baud, or you will see strange symbols.
  4. Direction: stand behind the robot and look forward. Line on the left = negative, line on the right = positive. Channel 1 is on the far left (id 0x10), channel 16 on the far right (0x1F).
  5. Losing the line: to check "I really cannot see a line", use 0x03 line width == 0 (or 0x04 line segments == 0). 0x06 last exit direction tells you which side the line was on, so turn that way to find it again (the line-following robot below does this).
  6. Crossroads: when the whole row is on black, it is one piece of line, so 0x04 line segments is still 1. Check 0x03 line width >= 11 instead. Tick "Crossroads" on the right to try it.