This page has moved into the new guide: Pathfinder 16-Way › EV3

EV3 beginner examples: Pathfinder 16-way

Back to the Pathfinder 16-way product page

This page has 6 examples for people who have never written a program: every example comes with a ready-made program file you can download and run, plus a plain-language step-by-step explanation. Start with example 1 and work through in order.

Before you start

  1. Use the EV3 edition of the sensor (RJ12 plug), plugged into port 1 of the EV3.
  2. Turn the EV3 on first, then plug in the sensor. If the number never changes, unplug the sensor and plug it back in.
  3. Examples that drive use the LEGO Education base robot: left wheel motor on B, right wheel motor on C.
  4. The sensor shows up on the EV3 as an "Infrared Sensor". That is normal: read its values with the infrared sensor blocks.

Every example comes in 5 software versions; pick the one you use: (examples marked "Pybricks / EV3 MicroPython / clev3r only" come in those 3 versions only, because the EV3-G and Classroom blocks cannot read that mode)

Software Who it suits How to open the example file
EV3-G (official software) School computer labs, WRO teams (the most common in Taiwan) Download the .ev3 file, choose "File → Open Project" in the EV3 software, then "Download and Run" at the bottom right.
EV3 Classroom First time with EV3, want the same blocks as SPIKE Download the .lmsp file, choose "Open" on the EV3 Classroom home screen, connect the EV3 and press "Download and Run" at the bottom right.
Pybricks v4 Want to learn typed code, works in the browser Open code.pybricks.com in Chrome; the EV3 needs the Pybricks firmware first (currently Beta). Drag the .py file into the editor and press Run.
EV3 MicroPython v2.0 Already using the official LEGO EV3 MicroPython Use the official LEGO "EV3 MicroPython" microSD boot card + the VS Code extension; paste the program into main.py and press F5 to download and run.
clev3r Already using clev3r (EV3 Basic) Open the .bp file in clev3r, press "Compile and download", then run it from the MBC_ folder in the EV3 file browser.

Before you start: calibrate (once)

The Pathfinder has to "learn" the black and white of your mat before its numbers are right. Calibration is stored in the sensor and survives power-off.

  1. Plug in the sensor and turn the EV3 on (the sensor lights come on).
  2. Press and release the calibration button on the sensor (do not hold it down; holding it keeps restarting calibration).
  3. When the green calibration light comes on, within 5 seconds sweep the sensor back and forth between the black line and the white mat a few times, so all 16 channels see both black and white.
  4. Green light off = calibration done.

How do I know it worked? Before running example 4, use example 1 to watch the line position: 0 with the line in the centre, negative when moved left, positive when moved right.

The 4 values used on this page

Name Range Where it comes from
Line position -16 ~ +16, 0 = line in the centre Mode 0
Line width (channels on the black line) 0 ~ 16, 0 = no line Mode 2, value 2
Line position (fine) -100 ~ +100, 0 = line in the centre Mode 2, value 1
Line bitmap 0 ~ 65535: bit0 = channel 1 (far left) … bit15 = channel 16, 1 = that channel is on the line Mode 3, value 1 (Pybricks / EV3 MicroPython / clev3r only)

Tip

When the line disappears, the "line position" keeps the last value it saw; it does not go back to 0. So to check "is there a line", always look at whether the line width is 0.

Example list:

  1. See where the line is
  2. Three-way line following: left, right, straight
  3. Proportional line following: the further off, the harder it turns
  4. Stop at a crossing
  5. Count crossings: stop at the 3rd one
  6. Line bitmap: draw an LED ruler on the screen (Pybricks / EV3 MicroPython / clev3r only)

Example 1: See where the line is

What this example does: Put the sensor over the black line and the EV3 screen shows a number telling you where the line is. This is step one of every line-following program: learn to "read the number".

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • Pathfinder 16-way (EV3 edition, RJ12 plug)
  • A white mat with a black line (about 2 cm wide)

How to connect: Pathfinder 16-way in port 1, mounted under the front of the robot about 1 cm above the ground, facing straight down.

Download the program (pick your software):

⬇ EV3-G (official software) | ⬇ EV3 Classroom | ⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0 | ⬇ clev3r

EV3-G (official software) blocks

Pathfinder 16-way See where the line is(EV3-G (official software))

(Too small? Click the picture to open it full size.)

EV3 Classroom blocks

Pathfinder 16-way See where the line is(EV3 Classroom)

(Too small? Click the picture to open it full size.)

Pybricks v4 code
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 01:看看線在哪裡
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:循行者 16 路插 1 號孔
# 這個範例會做什麼:把感應器放在黑線上,EV3 螢幕會顯示一個數字告訴你線在哪裡。這是所有循線程式的第一步:先學會「讀數字」。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port, Color
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = LUMPDevice(Port.S1)       # 循行者 16 路插 1 號孔


def read_pos():
    # 線的位置:-16 ~ +16,0=線在正中間
    return sensor.read(0)[0]


# ---------- 程式從這裡開始 ----------
# 先把螢幕清乾淨
ev3.screen.clear()
# 下面的動作一直重複
while True:
    # 讀取線的位置,顯示在螢幕第 1 行
    ev3.screen.draw_text(0, 0, "POS " + str(read_pos()) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒再讀下一次,數字才不會閃太快
    wait(100)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 01:看看線在哪裡
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:循行者 16 路插 1 號孔
# 這個範例會做什麼:把感應器放在黑線上,EV3 螢幕會顯示一個數字告訴你線在哪裡。這是所有循線程式的第一步:先學會「讀數字」。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.iodevices import Ev3devSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = Ev3devSensor(Port.S1)     # 循行者 16 路插 1 號孔


def read_pos():
    # 線的位置:-16 ~ +16,0=線在正中間
    return sensor.read('IR-PROX')[0]


# ---------- 程式從這裡開始 ----------
# 先把螢幕清乾淨
ev3.screen.clear()
# 下面的動作一直重複
while True:
    # 讀取線的位置,顯示在螢幕第 1 行
    ev3.screen.draw_text(0, 0, "POS " + str(read_pos()) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒再讀下一次,數字才不會閃太快
    wait(100)
clev3r code
' ============================================================
' 循行者 16 路循線感應器 EV3 新手範例 01:看看線在哪裡
' 軟體:clev3r
' 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
' 這個範例會做什麼:把感應器放在黑線上,EV3 螢幕會顯示一個數字告訴你線在哪裡。這是所有循線程式的第一步:先學會「讀數字」。
' 讀值:pos=線的位置(-16 ~ +16,0=線在正中間)
' ============================================================
folder "prjs" "MBC_line16_ex01"

' 循行者 16 路插 1 號孔,使用模式 0
Sensor.SetMode(1, 0)
LCD.Clear()

' 先把螢幕清乾淨
LCD.Clear()
' 下面的動作一直重複
While "True"
  ' 讀取線的位置,顯示在螢幕第 1 行
  LCD.Text(1, 0, 0, 1, "POS " + Sensor.ReadRawValue(1, 0) + "      ")
  ' 等 0.1 秒再讀下一次,數字才不會閃太快
  Program.Delay(100)
EndWhile

Step by step:

  1. Clear the screen first
  2. Repeat the steps below forever
    • Read the line position and show it on screen row 1
    • Wait 0.1 s before the next read so the number does not flicker

What you should see: With the black line in the middle of the sensor the screen shows 0; move the line left and the number goes negative, move it right and it goes positive, up to ±16 at the edges (left/right as seen standing behind the robot, facing forward).

FAQ:

  • The line is gone but the number stays at the last value? That is normal: when the line leaves the sensor, the position stays where it was last seen. To detect "line lost", use the line width from example 4 (width = 0).
  • The number never changes? Check that the sensor light is on and the cable is seated. Turn the EV3 on before plugging in the sensor, or unplug and plug it back in.
  • The robot steers the wrong way and gets worse? Swap the "+" and "−" in the program (swap the left/right wheel corrections), or check that the left wheel is on B and the right wheel on C.

Example 2: Three-way line following: left, right, straight

What this example does: Use "if … then … else" to make the robot follow the black line: turn left when the line is to the left, turn right when it is to the right, go straight when it is in the middle.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • Pathfinder 16-way (EV3 edition, RJ12 plug)
  • A white mat with a black line (about 2 cm wide)

How to connect: Pathfinder 16-way in port 1, mounted under the front of the robot about 1 cm above the ground, facing straight down.

Download the program (pick your software):

⬇ EV3-G (official software) | ⬇ EV3 Classroom | ⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0 | ⬇ clev3r

EV3-G (official software) blocks

Pathfinder 16-way Three-way line following: left, right, straight(EV3-G (official software))

(Too small? Click the picture to open it full size.)

EV3 Classroom blocks

Pathfinder 16-way Three-way line following: left, right, straight(EV3 Classroom)

(Too small? Click the picture to open it full size.)

Pybricks v4 code
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 02:三段式循線:偏左、偏右、直走
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:用「如果…就…否則…」讓車子沿著黑線走:線偏左就往左轉,線偏右就往右轉,在中間就直走。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = LUMPDevice(Port.S1)       # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_pos():
    # 線的位置:-16 ~ +16,0=線在正中間
    return sensor.read(0)[0]


# ---------- 程式從這裡開始 ----------
# 一直重複檢查線的位置
while True:
    # 如果線的位置小於 -4(偏左)
    if read_pos() < (-4):
        # 線偏左:左輪慢、右輪快,車子往左修
        left_motor.dc(10)
        right_motor.dc(40)
    # 否則,如果線的位置大於 4(偏右)
    elif read_pos() > 4:
        # 線偏右:左輪快、右輪慢,車子往右修
        left_motor.dc(40)
        right_motor.dc(10)
    else:
        # 線在中間:兩輪一樣快,直直走
        left_motor.dc(30)
        right_motor.dc(30)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 02:三段式循線:偏左、偏右、直走
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:用「如果…就…否則…」讓車子沿著黑線走:線偏左就往左轉,線偏右就往右轉,在中間就直走。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import Ev3devSensor
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = Ev3devSensor(Port.S1)     # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_pos():
    # 線的位置:-16 ~ +16,0=線在正中間
    return sensor.read('IR-PROX')[0]


# ---------- 程式從這裡開始 ----------
# 一直重複檢查線的位置
while True:
    # 如果線的位置小於 -4(偏左)
    if read_pos() < (-4):
        # 線偏左:左輪慢、右輪快,車子往左修
        left_motor.dc(10)
        right_motor.dc(40)
    # 否則,如果線的位置大於 4(偏右)
    elif read_pos() > 4:
        # 線偏右:左輪快、右輪慢,車子往右修
        left_motor.dc(40)
        right_motor.dc(10)
    else:
        # 線在中間:兩輪一樣快,直直走
        left_motor.dc(30)
        right_motor.dc(30)
clev3r code
' ============================================================
' 循行者 16 路循線感應器 EV3 新手範例 02:三段式循線:偏左、偏右、直走
' 軟體:clev3r
' 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
' 這個範例會做什麼:用「如果…就…否則…」讓車子沿著黑線走:線偏左就往左轉,線偏右就往右轉,在中間就直走。
' 讀值:pos=線的位置(-16 ~ +16,0=線在正中間)
' ============================================================
folder "prjs" "MBC_line16_ex02"

' 循行者 16 路插 1 號孔,使用模式 0
Sensor.SetMode(1, 0)
LCD.Clear()

' 一直重複檢查線的位置
While "True"
  ' 如果線的位置小於 -4(偏左)
  If Sensor.ReadRawValue(1, 0) < (-4) Then
    ' 線偏左:左輪慢、右輪快,車子往左修
    Motor.StartPower("B", 10)
    Motor.StartPower("C", 40)
  ' 否則,如果線的位置大於 4(偏右)
  ElseIf Sensor.ReadRawValue(1, 0) > 4 Then
    ' 線偏右:左輪快、右輪慢,車子往右修
    Motor.StartPower("B", 40)
    Motor.StartPower("C", 10)
  Else
    ' 線在中間:兩輪一樣快,直直走
    Motor.StartPower("B", 30)
    Motor.StartPower("C", 30)
  EndIf
EndWhile

Step by step:

  1. Keep checking the line position
    • If the line position is less than -4 (line is to the left)
      • Line to the left: left wheel slow, right wheel fast, the robot steers left
      • Otherwise:
        • Otherwise, if the line position is greater than 4 (line is to the right)
          • Line to the right: left wheel fast, right wheel slow, the robot steers right
          • Otherwise:
            • Line in the centre: both wheels the same speed, go straight

What you should see: The robot wobbles left and right along the black line. On sharp bends it may run off; try lowering 40 and changing 10 to 0.

FAQ:

  • The number never changes? Check that the sensor light is on and the cable is seated. Turn the EV3 on before plugging in the sensor, or unplug and plug it back in.
  • The robot steers the wrong way and gets worse? Swap the "+" and "−" in the program (swap the left/right wheel corrections), or check that the left wheel is on B and the right wheel on C.

Example 3: Proportional line following: the further off, the harder it turns

What this example does: Three-way line following keeps wobbling. This example uses "line position × a number" as the steering power: a little off means a little turn, a lot off means a big turn, so the robot drives more smoothly.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • Pathfinder 16-way (EV3 edition, RJ12 plug)
  • A white mat with a black line (about 2 cm wide)

How to connect: Pathfinder 16-way in port 1, mounted under the front of the robot about 1 cm above the ground, facing straight down.

Download the program (pick your software):

⬇ EV3-G (official software) | ⬇ EV3 Classroom | ⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0 | ⬇ clev3r

EV3-G (official software) blocks

Pathfinder 16-way Proportional line following: the further off, the harder it turns(EV3-G (official software))

(Too small? Click the picture to open it full size.)

EV3 Classroom blocks

Pathfinder 16-way Proportional line following: the further off, the harder it turns(EV3 Classroom)

(Too small? Click the picture to open it full size.)

Pybricks v4 code
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 03:比例循線:偏越多、轉越多
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:三段式循線會一直搖擺。這個範例把「線的位置 × 一個數字」當成轉彎的力量:偏一點就轉一點、偏很多就轉很多,車子會走得更順。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = LUMPDevice(Port.S1)       # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_pos():
    # 線的位置:-16 ~ +16,0=線在正中間
    return sensor.read(0)[0]


# ---------- 程式從這裡開始 ----------
# 一直重複
while True:
    # 轉彎力量 turn = 線的位置 × 2
    turn = read_pos() * 2
    # 左輪=30+turn、右輪=30-turn:線偏哪邊,車子就往哪邊修
    left_motor.dc(30 + turn)
    right_motor.dc(30 - turn)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 03:比例循線:偏越多、轉越多
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:三段式循線會一直搖擺。這個範例把「線的位置 × 一個數字」當成轉彎的力量:偏一點就轉一點、偏很多就轉很多,車子會走得更順。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import Ev3devSensor
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = Ev3devSensor(Port.S1)     # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_pos():
    # 線的位置:-16 ~ +16,0=線在正中間
    return sensor.read('IR-PROX')[0]


# ---------- 程式從這裡開始 ----------
# 一直重複
while True:
    # 轉彎力量 turn = 線的位置 × 2
    turn = read_pos() * 2
    # 左輪=30+turn、右輪=30-turn:線偏哪邊,車子就往哪邊修
    left_motor.dc(30 + turn)
    right_motor.dc(30 - turn)
clev3r code
' ============================================================
' 循行者 16 路循線感應器 EV3 新手範例 03:比例循線:偏越多、轉越多
' 軟體:clev3r
' 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
' 這個範例會做什麼:三段式循線會一直搖擺。這個範例把「線的位置 × 一個數字」當成轉彎的力量:偏一點就轉一點、偏很多就轉很多,車子會走得更順。
' 讀值:pos=線的位置(-16 ~ +16,0=線在正中間)
' ============================================================
folder "prjs" "MBC_line16_ex03"

' 循行者 16 路插 1 號孔,使用模式 0
Sensor.SetMode(1, 0)
LCD.Clear()

' 一直重複
While "True"
  ' 轉彎力量 turn = 線的位置 × 2
  turn = Sensor.ReadRawValue(1, 0) * 2
  ' 左輪=30+turn、右輪=30-turn:線偏哪邊,車子就往哪邊修
  Motor.StartPower("B", 30 + turn)
  Motor.StartPower("C", 30 - turn)
EndWhile

Step by step:

  1. Repeat forever
    • Steering power turn = line position × 2
    • Left wheel = 30 + turn, right wheel = 30 − turn: the robot steers towards the line

What you should see: The robot follows the black line more smoothly than in example 2. The number 2 is the "proportional gain": too big and it swings left and right, too small and it cannot make the bends. Tune it yourself.

FAQ:

  • It keeps swinging even on a straight line? Lower 2 a little (for example 1).
  • The number never changes? Check that the sensor light is on and the cable is seated. Turn the EV3 on before plugging in the sensor, or unplug and plug it back in.
  • The robot steers the wrong way and gets worse? Swap the "+" and "−" in the program (swap the left/right wheel corrections), or check that the left wheel is on B and the right wheel on C.

Example 4: Stop at a crossing

What this example does: The sensor also tells you "how many channels see the black line" (line width). Normally that is 2~3; at a crossing it suddenly jumps. This example follows the line while watching the width, and stops with a beep when it is 10 or more.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • Pathfinder 16-way (EV3 edition, RJ12 plug)
  • A black-line mat with crossings

How to connect: Pathfinder 16-way in port 1, mounted under the front of the robot about 1 cm above the ground, facing straight down.

Download the program (pick your software):

⬇ EV3-G (official software) | ⬇ EV3 Classroom | ⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0 | ⬇ clev3r

EV3-G (official software) blocks

Pathfinder 16-way Stop at a crossing(EV3-G (official software))

(Too small? Click the picture to open it full size.)

EV3 Classroom blocks

Pathfinder 16-way Stop at a crossing(EV3 Classroom)

(Too small? Click the picture to open it full size.)

Pybricks v4 code
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 04:碰到十字路口就停車
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:感應器還會告訴你「壓到黑線的格數」(線寬)。平常只有 2~3 格,到十字路口會一下子變很多。這個範例邊循線邊看線寬,大於等於 10 就停車並嗶一聲。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = LUMPDevice(Port.S1)       # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_width():
    # 線的寬度(壓到黑線的格數):0 ~ 16,0=看不到線
    return sensor.read(2)[1]


def read_pos100():
    # 線的位置(細):-100 ~ +100,0=線在正中間
    return sensor.read(2)[0]


# ---------- 程式從這裡開始 ----------
# 一直循線,直到線寬大於等於 10(到路口了)
while not (read_width() >= 10):
    # 轉彎力量 turn = 線的位置(-100~100)× 0.3
    turn = read_pos100() * 0.3
    # 一樣用比例循線往前走,速度放慢到 25
    left_motor.dc(25 + turn)
    right_motor.dc(25 - turn)
# 停車
left_motor.brake()
right_motor.brake()
# 嗶一聲告訴你到了
ev3.speaker.beep()
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 04:碰到十字路口就停車
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:感應器還會告訴你「壓到黑線的格數」(線寬)。平常只有 2~3 格,到十字路口會一下子變很多。這個範例邊循線邊看線寬,大於等於 10 就停車並嗶一聲。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import Ev3devSensor
from pybricks.parameters import Port
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = Ev3devSensor(Port.S1)     # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_width():
    # 線的寬度(壓到黑線的格數):0 ~ 16,0=看不到線
    return sensor.read('IR-REMOTE')[1]


def read_pos100():
    # 線的位置(細):-100 ~ +100,0=線在正中間
    return sensor.read('IR-REMOTE')[0]


# ---------- 程式從這裡開始 ----------
# 一直循線,直到線寬大於等於 10(到路口了)
while not (read_width() >= 10):
    # 轉彎力量 turn = 線的位置(-100~100)× 0.3
    turn = read_pos100() * 0.3
    # 一樣用比例循線往前走,速度放慢到 25
    left_motor.dc(25 + turn)
    right_motor.dc(25 - turn)
# 停車
left_motor.brake()
right_motor.brake()
# 嗶一聲告訴你到了
ev3.speaker.beep()
clev3r code
' ============================================================
' 循行者 16 路循線感應器 EV3 新手範例 04:碰到十字路口就停車
' 軟體:clev3r
' 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
' 這個範例會做什麼:感應器還會告訴你「壓到黑線的格數」(線寬)。平常只有 2~3 格,到十字路口會一下子變很多。這個範例邊循線邊看線寬,大於等於 10 就停車並嗶一聲。
' 讀值:width=線的寬度(壓到黑線的格數)(0 ~ 16,0=看不到線);pos100=線的位置(細)(-100 ~ +100,0=線在正中間)
' ============================================================
folder "prjs" "MBC_line16_ex04"

' 循行者 16 路插 1 號孔,使用模式 2
Sensor.SetMode(1, 2)
LCD.Clear()

' 一直循線,直到線寬大於等於 10(到路口了)
While Sensor.ReadRawValue(1, 1) < 10
  ' 轉彎力量 turn = 線的位置(-100~100)× 0.3
  turn = Sensor.ReadRawValue(1, 0) * 0.3
  ' 一樣用比例循線往前走,速度放慢到 25
  Motor.StartPower("B", 25 + turn)
  Motor.StartPower("C", 25 - turn)
EndWhile
' 停車
Motor.Stop("BC", "True")
' 嗶一聲告訴你到了
Speaker.Tone(50, 1000, 100)
Speaker.Wait()

Step by step:

  1. Keep following the line until the line width is 10 or more (reached a crossing)
    • Steering power turn = line position (-100~100) × 0.3
    • Same proportional line following, slowed down to 25
  2. Stop
  3. Beep to tell you it arrived

What you should see: The robot follows the line and stops with a beep when it reaches a crossing.

FAQ:

  • It stops before the crossing? Bends can also widen the line; raise 10 a little.
  • It does not stop at the crossing? Show the line width the way example 1 does, read the real value at the crossing, then change 10 in the program.
  • The number never changes? Check that the sensor light is on and the cable is seated. Turn the EV3 on before plugging in the sensor, or unplug and plug it back in.
  • The robot steers the wrong way and gets worse? Swap the "+" and "−" in the program (swap the left/right wheel corrections), or check that the left wheel is on B and the right wheel on C.

Example 5: Count crossings: stop at the 3rd one

What this example does: Competitions often say "drive to the Nth crossing". This example counts crossings with a variable count and uses another variable onCross to remember "am I on a crossing right now", so one crossing is not counted many times.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • Pathfinder 16-way (EV3 edition, RJ12 plug)
  • A black-line mat with 3 or more crossings

How to connect: Pathfinder 16-way in port 1, mounted under the front of the robot about 1 cm above the ground, facing straight down.

Download the program (pick your software):

⬇ EV3-G (official software) | ⬇ EV3 Classroom | ⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0 | ⬇ clev3r

EV3-G (official software) blocks

Pathfinder 16-way Count crossings: stop at the 3rd one(EV3-G (official software))

(Too small? Click the picture to open it full size.)

EV3 Classroom blocks

Pathfinder 16-way Count crossings: stop at the 3rd one(EV3 Classroom)

(Too small? Click the picture to open it full size.)

Pybricks v4 code
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 05:數路口:第 3 個路口停車
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:比賽常常要「走到第幾個路口」。這個範例用一個變數 count 數路口,用另一個變數 onCross 記住「現在是不是正壓在路口上」,避免同一個路口被數很多次。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port, Color
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = LUMPDevice(Port.S1)       # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_pos100():
    # 線的位置(細):-100 ~ +100,0=線在正中間
    return sensor.read(2)[0]


def read_width():
    # 線的寬度(壓到黑線的格數):0 ~ 16,0=看不到線
    return sensor.read(2)[1]


# ---------- 程式從這裡開始 ----------
# 路口數 count 從 0 開始
count = 0
# onCross=0 代表「現在不在路口上」
onCross = 0
# 一直循線,直到數滿 3 個路口
while not (count >= 3):
    # 比例循線:turn = 線的位置 × 0.3
    turn = read_pos100() * 0.3
    # 左輪=25+turn、右輪=25-turn
    left_motor.dc(25 + turn)
    right_motor.dc(25 - turn)
    # 如果線寬大於等於 10(壓在路口上)
    if read_width() >= 10:
        # 而且剛剛還不在路口上
        if onCross == 0:
            # 剛壓到新路口:count 加 1
            count = count + 1
            # 記住「正在路口上」,同一個路口不要再數
            onCross = 1
            # 嗶一聲
            ev3.speaker.beep()
    else:
        # 離開路口了:onCross 改回 0,準備數下一個
        onCross = 0
    # 螢幕顯示已經數到第幾個路口
    ev3.screen.draw_text(0, 0, "COUNT " + str(count) + "      ", Color.BLACK, Color.WHITE)
# 停車
left_motor.brake()
right_motor.brake()
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 05:數路口:第 3 個路口停車
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:比賽常常要「走到第幾個路口」。這個範例用一個變數 count 數路口,用另一個變數 onCross 記住「現在是不是正壓在路口上」,避免同一個路口被數很多次。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor
from pybricks.iodevices import Ev3devSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = Ev3devSensor(Port.S1)     # 循行者 16 路插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_pos100():
    # 線的位置(細):-100 ~ +100,0=線在正中間
    return sensor.read('IR-REMOTE')[0]


def read_width():
    # 線的寬度(壓到黑線的格數):0 ~ 16,0=看不到線
    return sensor.read('IR-REMOTE')[1]


# ---------- 程式從這裡開始 ----------
# 路口數 count 從 0 開始
count = 0
# onCross=0 代表「現在不在路口上」
onCross = 0
# 一直循線,直到數滿 3 個路口
while not (count >= 3):
    # 比例循線:turn = 線的位置 × 0.3
    turn = read_pos100() * 0.3
    # 左輪=25+turn、右輪=25-turn
    left_motor.dc(25 + turn)
    right_motor.dc(25 - turn)
    # 如果線寬大於等於 10(壓在路口上)
    if read_width() >= 10:
        # 而且剛剛還不在路口上
        if onCross == 0:
            # 剛壓到新路口:count 加 1
            count = count + 1
            # 記住「正在路口上」,同一個路口不要再數
            onCross = 1
            # 嗶一聲
            ev3.speaker.beep()
    else:
        # 離開路口了:onCross 改回 0,準備數下一個
        onCross = 0
    # 螢幕顯示已經數到第幾個路口
    ev3.screen.draw_text(0, 0, "COUNT " + str(count) + "      ", Color.BLACK, Color.WHITE)
# 停車
left_motor.brake()
right_motor.brake()
clev3r code
' ============================================================
' 循行者 16 路循線感應器 EV3 新手範例 05:數路口:第 3 個路口停車
' 軟體:clev3r
' 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
' 這個範例會做什麼:比賽常常要「走到第幾個路口」。這個範例用一個變數 count 數路口,用另一個變數 onCross 記住「現在是不是正壓在路口上」,避免同一個路口被數很多次。
' 讀值:pos100=線的位置(細)(-100 ~ +100,0=線在正中間);width=線的寬度(壓到黑線的格數)(0 ~ 16,0=看不到線)
' ============================================================
folder "prjs" "MBC_line16_ex05"

' 循行者 16 路插 1 號孔,使用模式 2
Sensor.SetMode(1, 2)
LCD.Clear()

' 路口數 count 從 0 開始
count = 0
' onCross=0 代表「現在不在路口上」
onCross = 0
' 一直循線,直到數滿 3 個路口
While count < 3
  ' 比例循線:turn = 線的位置 × 0.3
  turn = Sensor.ReadRawValue(1, 0) * 0.3
  ' 左輪=25+turn、右輪=25-turn
  Motor.StartPower("B", 25 + turn)
  Motor.StartPower("C", 25 - turn)
  ' 如果線寬大於等於 10(壓在路口上)
  If Sensor.ReadRawValue(1, 1) >= 10 Then
    ' 而且剛剛還不在路口上
    If onCross = 0 Then
      ' 剛壓到新路口:count 加 1
      count = count + 1
      ' 記住「正在路口上」,同一個路口不要再數
      onCross = 1
      ' 嗶一聲
      Speaker.Tone(50, 1000, 100)
      Speaker.Wait()
    EndIf
  Else
    ' 離開路口了:onCross 改回 0,準備數下一個
    onCross = 0
  EndIf
  ' 螢幕顯示已經數到第幾個路口
  LCD.Text(1, 0, 0, 1, "COUNT " + count + "      ")
EndWhile
' 停車
Motor.Stop("BC", "True")

Step by step:

  1. Crossing counter count starts at 0
  2. onCross = 0 means "not on a crossing right now"
  3. Keep following the line until 3 crossings have been counted
    • Proportional line following: turn = line position × 0.3
    • Left wheel = 25 + turn, right wheel = 25 − turn
    • If the line width is 10 or more (on a crossing)
      • and we were not on a crossing a moment ago
        • Just reached a new crossing: add 1 to count
        • Remember "on a crossing" so the same crossing is not counted again
        • Beep
      • Otherwise:
        • Left the crossing: set onCross back to 0, ready for the next one
    • Show how many crossings have been counted
  4. Stop

What you should see: Every crossing beeps once and adds 1 on the screen; the robot stops at the 3rd crossing. Change the 3 to stop at a different crossing.

FAQ:

  • One crossing is counted twice? Happens when the black line at the crossing is damaged or shiny. Slow the speed 25 down or raise the width threshold.
  • The number never changes? Check that the sensor light is on and the cable is seated. Turn the EV3 on before plugging in the sensor, or unplug and plug it back in.
  • The robot steers the wrong way and gets worse? Swap the "+" and "−" in the program (swap the left/right wheel corrections), or check that the left wheel is on B and the right wheel on C.

Example 6: Line bitmap: draw an LED ruler on the screen

Pybricks v4 / EV3 MicroPython v2.0 / clev3r only (the EV3-G and Classroom blocks cannot read this mode)

What this example does: Mode 3 sends the "line bitmap": 16 bits, one per channel, 1 when that channel is on the line. This example draws it as a ruler (# = on the line, . = not), just like the LEDs on the sensor, so you can see at a glance which channels see the line and whether there is a fork.

What you need:

  • Pathfinder 16-way (EV3 edition, firmware v4.0 or later)
  • EV3 brick
  • A white mat with a black line

How to connect: Pathfinder 16-way in port 1, mounted under the front of the robot about 1 cm above the ground, facing straight down.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0 | ⬇ clev3r

(The code is the example; no screenshot needed.)

Pybricks v4 code
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 06:壓線位元圖:在螢幕上畫一把 LED 尺
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:循行者 16 路插 1 號孔
# 這個範例會做什麼:模式 3 送出「壓線位元圖」:16 個位元,每一路壓到線就是 1。這個範例把它畫成一把尺(# =壓到線、. =沒壓到),跟感應器上的 LED 一樣,一眼就看出線在哪幾路、有沒有分岔。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port, Color
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = LUMPDevice(Port.S1)       # 循行者 16 路插 1 號孔


def read_bitmap():
    # 壓線位元圖:0 ~ 65535:bit0=第 1 路(最左)… bit15=第 16 路,1=那一路壓到線
    return sensor.read(3)[0] & 0xFFFF


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行畫尺:16 格,最左是第 1 路,# =壓到線、. =沒壓到
    bits_value = read_bitmap()
    bits_text = ""
    for b in range(16):
        if (bits_value // (2 ** b)) % 2 == 1:   # 第 b 個位元是 1
            bits_text = bits_text + "#"
        else:
            bits_text = bits_text + "."
    ev3.screen.draw_text(0, 0, "L " + bits_text + "  ", Color.BLACK, Color.WHITE)
    # 第 2 行顯示位元圖的數字(第 1 路=1、第 2 路=2、第 3 路=4…相加)
    ev3.screen.draw_text(0, 16, "BITS " + str(read_bitmap()) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒
    wait(100)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# 循行者 16 路循線感應器 EV3 新手範例 06:壓線位元圖:在螢幕上畫一把 LED 尺
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:循行者 16 路插 1 號孔
# 這個範例會做什麼:模式 3 送出「壓線位元圖」:16 個位元,每一路壓到線就是 1。這個範例把它畫成一把尺(# =壓到線、. =沒壓到),跟感應器上的 LED 一樣,一眼就看出線在哪幾路、有沒有分岔。
# ============================================================

from pybricks.hubs import EV3Brick
from pybricks.iodevices import Ev3devSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait

ev3 = EV3Brick()
sensor = Ev3devSensor(Port.S1)     # 循行者 16 路插 1 號孔


def read_bitmap():
    # 壓線位元圖:0 ~ 65535:bit0=第 1 路(最左)… bit15=第 16 路,1=那一路壓到線
    return sensor.read('IR-REM-A')[0] & 0xFFFF


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行畫尺:16 格,最左是第 1 路,# =壓到線、. =沒壓到
    bits_value = read_bitmap()
    bits_text = ""
    for b in range(16):
        if (bits_value // (2 ** b)) % 2 == 1:   # 第 b 個位元是 1
            bits_text = bits_text + "#"
        else:
            bits_text = bits_text + "."
    ev3.screen.draw_text(0, 0, "L " + bits_text + "  ", Color.BLACK, Color.WHITE)
    # 第 2 行顯示位元圖的數字(第 1 路=1、第 2 路=2、第 3 路=4…相加)
    ev3.screen.draw_text(0, 16, "BITS " + str(read_bitmap()) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒
    wait(100)
clev3r code
' ============================================================
' 循行者 16 路循線感應器 EV3 新手範例 06:壓線位元圖:在螢幕上畫一把 LED 尺
' 軟體:clev3r
' 接法:循行者 16 路插 1 號孔;左輪馬達接 B、右輪馬達接 C
' 這個範例會做什麼:模式 3 送出「壓線位元圖」:16 個位元,每一路壓到線就是 1。這個範例把它畫成一把尺(# =壓到線、. =沒壓到),跟感應器上的 LED 一樣,一眼就看出線在哪幾路、有沒有分岔。
' 讀值:bitmap=壓線位元圖(0 ~ 65535:bit0=第 1 路(最左)… bit15=第 16 路,1=那一路壓到線)
' ============================================================
folder "prjs" "MBC_line16_ex06"

' 循行者 16 路插 1 號孔,使用模式 3
Sensor.SetMode(1, 3)
LCD.Clear()

' 清除螢幕
LCD.Clear()
' 一直重複
While "True"
  ' 第 1 行畫尺:16 格,最左是第 1 路,# =壓到線、. =沒壓到
  bitsValue = Math.Remainder(Sensor.ReadRawValue(1, 0) + 65536, 65536)
  bitsText = ""
  For b = 0 To 15
    If Math.Remainder(Math.Floor(bitsValue / Math.Power(2, b)), 2) = 1 Then
      bitsText = Text.Append(bitsText, "#")
    Else
      bitsText = Text.Append(bitsText, ".")
    EndIf
  EndFor
  LCD.Text(1, 0, 0, 1, "L " + bitsText + "  ")
  ' 第 2 行顯示位元圖的數字(第 1 路=1、第 2 路=2、第 3 路=4…相加)
  LCD.Text(1, 0, 16, 1, "BITS " + Math.Remainder(Sensor.ReadRawValue(1, 0) + 65536, 65536) + "      ")
  ' 等 0.1 秒
  Program.Delay(100)
EndWhile

Step by step:

  1. Clear the screen
  2. Repeat forever
    • Row 1 draws the ruler: 16 cells, channel 1 on the far left, # = on the line, . = not
    • Row 2 shows the bitmap as a number (channel 1 = 1, channel 2 = 2, channel 3 = 4 … added up)
    • Wait 0.1 s

What you should see: Put the sensor over the black line: 2~3 # marks follow the line along the ruler; at a crossing the whole row turns into #; with no line it is all dots. The number is the sum of 1, 2, 4, 8 … for the channels on the line, for example channels 1 and 2 = 3.

FAQ:

  • The ruler is mirrored compared with the sensor LEDs? The ruler is seen from behind the robot (far left = channel 1); the LEDs may look mirrored when viewed from above.
  • The number stays at 0 or never changes? This mode needs sensor firmware v4.0 (2026-10) or later; update older firmware first.
  • Why is there no EV3-G / Classroom version? This example uses a new mode (firmware v4, 2026-10). The EV3-G and Classroom infrared blocks cannot read it; only Pybricks, EV3 MicroPython and clev3r can.
  • The number never changes? Check that the sensor light is on and the cable is seated. Turn the EV3 on before plugging in the sensor, or unplug and plug it back in.

Still stuck? Leave a message on Contact us with the software you use and a screenshot of your program, and we will help.

Email for a quoteBuying Guide