MBC Pathfinder 16-Way (Line16) × EV3 Pybricks

Read the Pathfinder 16-Way on the EV3 with MicroPython in three steps

MicroPython on the EV3 always goes in this order. First import. Then create an object and give the sensor a name. Last, use that name to read values. First pick Pybricks v4 or EV3 MicroPython v2 above. Then 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 program prints.

Your software:
1

Import the library

The first thing your program does is tell Pybricks: "I want to use the Pathfinder library". Without this line, the hub does not know what MBC_LINE16 means, and you get a NameError.

Pick one library. Both work the same way. Only the import line is different.

  1. Download the library (save the file and keep its name): MBC_line16.py (only this sensor) or MBC_sensors.py (the all-in-one library). You picked MBC_line16.py.
  2. Open code.pybricks.com in Chrome. Follow the steps on the site to put the Pybricks firmware on the EV3 (the release version already includes the EV3 firmware).
  3. Press "Import a file" in the file list on the left. Import the library into the same project.
  4. Do not rename the file, and do not put it in a sub-folder.
  5. Go back to your main program. Write the lines below at the very top (marked ① in the program on the right).
2

Create an object: name your sensor

A robot can have several sensors, so you "create an object". It tells the program which port the sensor is on, and gives it a name. Every time you read a value, you use this name.

Which port is the sensor on?

Turn on multitasking?

The 1 in the program is the port number: EV3 1~4 = S1~S4. You can also write Port.S1, but then you also need from pybricks.parameters import Port.

3

Use a function

You always write "line16.function_name()": the name, a dot, then the function. Click a function, and the program and output change to its example.

Sensor simulator (looking forward from behind the robot): drag the black line and watch the output on the right change

The simulator is only a sketch: the maths is simplified, so a real sensor may differ by one or two steps. The number in each circle is the light value of that channel (SPIKE: 0 = black, 15 = white). Red ring = that channel counts as on the line.

An example for every function

Full examples


        

!


        

!


        

!

Good to know

  1. The EV3 edition cannot take commands from the hub (E0~E9 cannot be sent on the stock firmware, Pybricks or ev3dev). Hold the board button while powering up = white-line mode (not pressed = black line); press the calibration button while running = 5-second dynamic calibration. Left/right swap and thresholds are not available on the EV3 edition; use the SPIKE or I2C edition for those. Pybricks InfraredSensor.buttons() does not work (mode 2 is line position and raises OSError 5); use the MBC library or LUMPDevice.
  2. Unplugging works differently in the two programs. Pybricks v4 with the MBC library: when the sensor cannot be read, you get the last value it read (or None if it has never been read). The computer prints one warning line, and the program keeps running. Plug it back in and it works again by itself. To check the cable, use sensor_ok(). EV3 MicroPython v2 with Ev3devSensor: you get an OSError and the program stops. Plug the sensor back in and run the program again. Tick "Unplug the sensor" on the right to see both.
  3. On the EV3, changing mode takes about 1.1 s. Try to use one mode in the same loop (the "Mode N" on each card). pos100(), width(), edge_l(), edge_r() are mode 2 (the most used for line following). bin_raw(), bitmap(), on_sensors(), junction(), junction_name() are mode 3. pos16() is mode 0. ir_ch(), ir_calibration() are mode 1. Put device_id() and firmware_version() (mode 5) outside the loop.
  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.
  5. Lost line and crossings: when the line leaves the sensor completely, line position gives −16 or +16 and fine position gives −100 or +100, depending on the side where the line left. So the robot turns back towards that side by itself. To check "I really cannot see a line", use line width = 0. A line width of 10 or more is usually a crossing.
  6. How the EV3 differs from SPIKE: read_all() is SPIKE only. On the EV3 each channel light value is only 1 to 9 (two channels packed per number); the far-left and far-right values are 0 to 100. The EV3 does not get "line segments", so the library works out junction() from the bitmap. The bitmap, ID and firmware version (modes 3 and 5) need sensor firmware v4.0 or later.