Ranger 8-Way (TOF8) × EV3 × Pybricks

Interactive tutorialEV3

First time? Try the interactive tutorial in your browser first, then follow the steps below with your hub.

This page covers Pybricks v4 / EV3 MicroPython v2 only: how to set it up for the Ranger 8-Way (TOF8) on MINDSTORMS EV3, how to read every value, the commands it supports and examples. Use the switcher above to change product, host or software.

Setup

What you need

  • Sensor: Ranger 8-Way (TOF8) (EV3 edition, RJ12 plug).
  • Brick: LEGO MINDSTORMS EV3.
  • Software (pick one):
    • Pybricks v4: open code.pybricks.com in Chrome; the EV3 needs the Pybricks firmware first (the stable release now includes EV3 firmware, v4.1.1 as of 2026-10).
    • EV3 MicroPython v2.0: the official LEGO "EV3 MicroPython" microSD boot card + the "LEGO MINDSTORMS EV3 MicroPython" extension for VS Code. Pick this if you already use it.

Install the software

Pybricks v4

  1. Open code.pybricks.com in Chrome.
  2. Follow the website to switch the EV3 to Pybricks firmware. The stable code.pybricks.com now includes EV3 firmware (v4.1.1 as of 2026-10). The examples on this site were first bench-tested on v4.0.0b10; v4.1.1 uses the same code.
  3. To open an example: drag the .py file into the editor and press Run.

EV3 MicroPython v2.0

  1. Boot the EV3 from the official LEGO EV3 MicroPython microSD card.
  2. Install the "LEGO MINDSTORMS EV3 MicroPython" extension in VS Code.
  3. To open an example: paste the program into main.py and press F5 to download and run.

Make the software see the sensor

The beginner examples need no library; they read the sensor data directly:

  • Pybricks v4 uses LUMPDevice(Port.S1); sensor.read(mode) returns that mode's values.
  • EV3 MicroPython v2.0: always read with Ev3devSensor. For products that show up as an infrared sensor, do not use InfraredSensor: every mode change waits about 1.1 s, and buttons() / beacon() / keypad() raise errors or change the values.
  • Both can read modes that EV3-G and Classroom cannot, so examples marked "Pybricks / EV3 MicroPython / clev3r only" run here too.

Important

Do not switch modes often inside the main loop. The EV3 hardware adds a communication wait every time a sensor mode changes. Pick one mode for the task and keep it for the whole run.

Wiring

  1. Plug the sensor into EV3 port 1 with the cable (any of 14 works; use the same port as Port.S1Port.S4 in the program).
  2. Turn the EV3 on first, then plug in the sensor.
  3. Latch up, push the plug in gently.

Wait 2~4 seconds after power-on

The Ranger 8-Way needs no calibration; plug it in and go. After power-on, wait 2~4 seconds for the 8 lasers to get ready; meanwhile everything reads 120 (cm). Start your program with a short wait before reading distances.

Channel 1 2 3 4 5 6 7 8
Facing left left-front 60° left-front 30° front front right-front 30° right-front 60° right

The table assumes the sensor faces forward with the components up. Put your hand on the robot's left: example 1 should show a negative direction. If it shows a positive one, the sensor is mounted the other way, and "turn left" / "turn right" in the examples must be swapped too.

First test

  1. Open example 1 on this page and run it (F5 for the MicroPython version).
  2. If the number on the EV3 screen changes with what is in front of the sensor, everything is connected.

FAQ

  • Cannot find Pybricks firmware for the EV3? Use the stable code.pybricks.com (EV3 v4.1.1 built in since 2026-10); the old ev3dev-based Pybricks (microSD card) is a different system and does not apply here.
  • The number never changes? Unplug the sensor and plug it back in (the EV3 must be on first).
  • The program reacts unevenly? The main loop reads different modes in turn and waits at every switch. Use one mode only.
  • Error when importing a library? The library file must be imported into the same Pybricks project, and its file name must match the import name exactly.

Reading values

Code assumes port 1 and reads raw modes with LUMPDevice; EV3 MicroPython v2 works the same way.

nearest distance (cm) nearCm

nearest distance (cm)
Range Meaning
0~120 cm (EV3 mode 2 gives a scaled value: cm×100/120 (0~100)) smallest of the 8 distances, 0~120 cm (a failed sensor counts as 120); 120 = nothing around
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

nearCm = sensor.read(2)[1]   # nearest distance (cm):cm×100/120 (0~100)

mode 2 · int8 · IR-REMOTE [最近方向−8, 最近', 最空曠方向−8, 最空曠'](INO:154-159) (locked mode: firmware and hosts will not change it)

nearest direction nearDir

nearest direction
Range Meaning
-8~+8 direction of the nearest object: −7 (left) to +7 (right), 0 = straight ahead, negative = left; ±8 = nothing around (keeps the last side)
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

nearDir = sensor.read(2)[0]   # nearest direction:-8~+8
nearDir = sensor.read(5)[1]   # nearest direction:-8~+8

mode 2 · int8 · int8 二補數(方位−8); mode 5 · int16 · #67 新:IR-CAL 2×int16 = [最近 mm, 最近方向 −8~+8](原本 = 前 4 顆兩兩合併) (locked mode: firmware and hosts will not change it)

farthest distance (cm) farCm

farthest distance (cm)
Range Meaning
0~120 cm (EV3 mode 2 gives a scaled value: cm×100/120 (0~100)) largest of the 8 distances, 0~120 cm (a failed sensor always reads 120 and looks 'farthest')
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

farCm = sensor.read(2)[3]   # farthest distance (cm):cm×100/120 (0~100)

mode 2 · int8 (locked mode: firmware and hosts will not change it)

farthest direction farDir

farthest direction
Range Meaning
-7~+7 direction of the most open side: −7 to +7, 0 = straight ahead, negative = left; 0 when all 8 read 120
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

farDir = sensor.read(2)[2]   # farthest direction:-7~+7

mode 2 · int8 · int8 二補數(方位−8) (locked mode: firmware and hosts will not change it)

front distance (cm) frontCm

front distance (cm)
Range Meaning
0~120 cm (EV3 mode 0 gives a scaled value: cm×100/120 (0~100)) average of sensors 4 and 5 (both facing forward), 0~120 cm; EV3 mode 3 and SPIKE mode 6 carry the same thing in mm; 0x22 'C' is min(4, 5), not the average (see 10)
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

frontCm = sensor.read(0)[0]   # front distance (cm):cm×100/120 (0~100)

mode 0 · int8 · IR-PROX = (第 4'+第 5')>>1(鎖住,INO:148-150) (locked mode: firmware and hosts will not change it)

nearest distance (mm) nearMm

nearest distance (mm)
Range Meaning
0~1200 mm smallest of the 8 mm readings, 0~1200 (failed sensor counts as 1200); 1200 = nothing around
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

nearMm = sensor.read(5)[0]   # nearest distance (mm):0~1200 mm

mode 5 · int16 · #67 新:[最近 mm, 最近方向]

front distance (mm) frontMm

front distance (mm)
Range Meaning
0~1200 mm (EV3 mode 3 gives a scaled value: mm (0~1200)) average of sensors 4 and 5 (the two facing forward), 0~1200 mm; 1200 = nothing ahead. cm version is 05
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

frontMm = sensor.read(3)[0]   # front distance (mm):mm (0~1200)

mode 3 · int16 · #67 新:IR-REM-A 1×int16 = 正前方(第 4、5 顆平均)mm(原本 = 第 1 顆 0~100)

sensor 1~8 distance (cm) distCm

sensor 1~8 distance (cm)
Range Meaning
0~120 cm (EV3 mode 1, mode 4 give a scaled value: cm×100/120 (0~100)) distance of each sensor 18, 0120 cm (1 = far left, 4 and 5 = front, 8 = far right, 30° apart); 120 = nothing that way / sensor failed; ids 10~17
from pybricks.iodevices import LUMPDevice
from pybricks.parameters import Port

sensor = LUMPDevice(Port.S1)   # sensor on port 1

distCm = sensor.read(1)[0:8]   # sensor 1~8 distance (cm)
distCm = sensor.read(4)[0:4]   # sensor 1~8 distance (cm)

mode 1 · int8 · IR-SEEK 8×int8 = 第 1~8 顆(EV3 視為頻道 1~4 的〈方位,距離〉); mode 4 · int8 · IR-S-ALT = 前 4 顆 (locked mode: firmware and hosts will not change it)

Not readable here:init bitmap, fault bitmap, sensor 1~8 distance (mm). Use Arduino / mBlock / micro:bit for these.

Examples

Example 1: Measure the distance straight ahead

What this example does: Mode 0 of the eight-direction laser gives the average distance straight ahead (the middle two lasers): the simplest value to use.

What you need:

  • TOF8 eight-direction laser (EV3 edition)
  • EV3 brick

How to connect: TOF8 eight-direction laser in port 1.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0

Pybricks v4 code
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 01:量量看正前方有多遠
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:TOF8 八方位雷射插 1 號孔
# 這個範例會做什麼:八方位雷射的模式 0 會給你正前方(中間兩顆)的平均距離,最簡單好用。
# ============================================================

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)       # TOF8 八方位雷射插 1 號孔


def read_front():
    # 正前方(第 4、5 顆平均)距離:0 ~ 100(1 格=1.2 公分,100=前面沒東西)
    return sensor.read(0)[0]


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行顯示正前方距離(0~100)
    ev3.screen.draw_text(0, 0, "FRONT " + str(read_front()) + "      ", Color.BLACK, Color.WHITE)
    # 第 2 行換算成公分:× 1.2
    ev3.screen.draw_text(0, 16, "CM " + str(read_front() * 1.2) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒
    wait(100)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 01:量量看正前方有多遠
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:TOF8 八方位雷射插 1 號孔
# 這個範例會做什麼:八方位雷射的模式 0 會給你正前方(中間兩顆)的平均距離,最簡單好用。
# ============================================================

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)     # TOF8 八方位雷射插 1 號孔


def read_front():
    # 正前方(第 4、5 顆平均)距離:0 ~ 100(1 格=1.2 公分,100=前面沒東西)
    return sensor.read('IR-PROX')[0]


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行顯示正前方距離(0~100)
    ev3.screen.draw_text(0, 0, "FRONT " + str(read_front()) + "      ", Color.BLACK, Color.WHITE)
    # 第 2 行換算成公分:× 1.2
    ev3.screen.draw_text(0, 16, "CM " + str(read_front() * 1.2) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒
    wait(100)

Step by step:

  1. Clear the screen
  2. Repeat forever
    • Row 1 shows the straight-ahead distance (0~100)
    • Row 2 converts to centimetres: × 1.2
    • Wait 0.1 s

What you should see: Move your hand in front and the number follows; with nothing ahead it reads 100.

FAQ:

  • The number does not match a ruler? On EV3, 1 unit = 1.2 cm (for example 25 = 30 cm); 100 means nothing within 120 cm ahead.
  • The number stays at 0 or never changes? Turn the EV3 on before plugging in the sensor, or unplug and plug it back in; make sure there is no protective film over the laser.

Example 2: Stop 30 cm before the wall

What this example does: The robot drives forward and stops when something is within 30 cm (value 25) straight ahead.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • TOF8 eight-direction laser (EV3 edition)
  • A few boxes as obstacles

How to connect: TOF8 eight-direction laser in port 1, mounted at the front with lasers 4 and 5 facing straight ahead, about 5 cm above the ground.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0

Pybricks v4 code
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 02:前方 30 公分停車
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:TOF8 八方位雷射插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:車子往前開,正前方 30 公分(數字 25)以內有東西就停下來。
# ============================================================

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)       # TOF8 八方位雷射插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_front():
    # 正前方(第 4、5 顆平均)距離:0 ~ 100(1 格=1.2 公分,100=前面沒東西)
    return sensor.read(0)[0]


# ---------- 程式從這裡開始 ----------
# 兩輪往前開
left_motor.dc(30)
right_motor.dc(30)
# 等到正前方距離小於 25
while not (read_front() < 25):
    wait(10)
# 停車
left_motor.brake()
right_motor.brake()
# 嗶一聲
ev3.speaker.beep()
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 02:前方 30 公分停車
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:TOF8 八方位雷射插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:車子往前開,正前方 30 公分(數字 25)以內有東西就停下來。
# ============================================================

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)     # TOF8 八方位雷射插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_front():
    # 正前方(第 4、5 顆平均)距離:0 ~ 100(1 格=1.2 公分,100=前面沒東西)
    return sensor.read('IR-PROX')[0]


# ---------- 程式從這裡開始 ----------
# 兩輪往前開
left_motor.dc(30)
right_motor.dc(30)
# 等到正前方距離小於 25
while not (read_front() < 25):
    wait(10)
# 停車
left_motor.brake()
right_motor.brake()
# 嗶一聲
ev3.speaker.beep()

Step by step:

  1. Both wheels drive forward
  2. Wait until the straight-ahead distance is less than 25
  3. Stop
  4. Beep

What you should see: The robot stops about 30 cm from the obstacle and beeps.

FAQ:

  • The number does not match a ruler? On EV3, 1 unit = 1.2 cm (for example 25 = 30 cm); 100 means nothing within 120 cm ahead.

Example 3: See all 8 directions at once

What this example does: Mode 1 sends the distance of each of the 8 lasers. This example shows all 8 numbers on the screen; wave your hand around and you will know which laser points where.

What you need:

  • TOF8 eight-direction laser (EV3 edition)
  • EV3 brick

How to connect: TOF8 eight-direction laser in port 1.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0

Pybricks v4 code
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 03:一次看 8 個方向的距離
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:TOF8 八方位雷射插 1 號孔
# 這個範例會做什麼:模式 1 會一次送出 8 顆雷射各自的距離。這個範例把 8 個數字都顯示在螢幕上,拿手在四周晃一晃,就知道每顆雷射對應哪個方向。
# ============================================================

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)       # TOF8 八方位雷射插 1 號孔


def read_d1():
    # 第 1 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[0]


def read_d2():
    # 第 2 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[1]


def read_d3():
    # 第 3 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[2]


def read_d4():
    # 第 4 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[3]


def read_d5():
    # 第 5 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[4]


def read_d6():
    # 第 6 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[5]


def read_d7():
    # 第 7 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[6]


def read_d8():
    # 第 8 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read(1)[7]


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行顯示第 1 顆雷射的距離
    ev3.screen.draw_text(0, 0, "D1 " + str(read_d1()) + "      ", Color.BLACK, Color.WHITE)
    # 第 2 行顯示第 2 顆雷射的距離
    ev3.screen.draw_text(0, 16, "D2 " + str(read_d2()) + "      ", Color.BLACK, Color.WHITE)
    # 第 3 行顯示第 3 顆雷射的距離
    ev3.screen.draw_text(0, 32, "D3 " + str(read_d3()) + "      ", Color.BLACK, Color.WHITE)
    # 第 4 行顯示第 4 顆雷射的距離
    ev3.screen.draw_text(0, 48, "D4 " + str(read_d4()) + "      ", Color.BLACK, Color.WHITE)
    # 第 5 行顯示第 5 顆雷射的距離
    ev3.screen.draw_text(0, 64, "D5 " + str(read_d5()) + "      ", Color.BLACK, Color.WHITE)
    # 第 6 行顯示第 6 顆雷射的距離
    ev3.screen.draw_text(0, 80, "D6 " + str(read_d6()) + "      ", Color.BLACK, Color.WHITE)
    # 第 7 行顯示第 7 顆雷射的距離
    ev3.screen.draw_text(0, 96, "D7 " + str(read_d7()) + "      ", Color.BLACK, Color.WHITE)
    # 第 8 行顯示第 8 顆雷射的距離
    ev3.screen.draw_text(0, 112, "D8 " + str(read_d8()) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒
    wait(100)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 03:一次看 8 個方向的距離
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:TOF8 八方位雷射插 1 號孔
# 這個範例會做什麼:模式 1 會一次送出 8 顆雷射各自的距離。這個範例把 8 個數字都顯示在螢幕上,拿手在四周晃一晃,就知道每顆雷射對應哪個方向。
# ============================================================

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)     # TOF8 八方位雷射插 1 號孔


def read_d1():
    # 第 1 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[0]


def read_d2():
    # 第 2 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[1]


def read_d3():
    # 第 3 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[2]


def read_d4():
    # 第 4 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[3]


def read_d5():
    # 第 5 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[4]


def read_d6():
    # 第 6 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[5]


def read_d7():
    # 第 7 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[6]


def read_d8():
    # 第 8 顆雷射的距離:0 ~ 100(1 格=1.2 公分)
    return sensor.read('IR-SEEK')[7]


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行顯示第 1 顆雷射的距離
    ev3.screen.draw_text(0, 0, "D1 " + str(read_d1()) + "      ", Color.BLACK, Color.WHITE)
    # 第 2 行顯示第 2 顆雷射的距離
    ev3.screen.draw_text(0, 16, "D2 " + str(read_d2()) + "      ", Color.BLACK, Color.WHITE)
    # 第 3 行顯示第 3 顆雷射的距離
    ev3.screen.draw_text(0, 32, "D3 " + str(read_d3()) + "      ", Color.BLACK, Color.WHITE)
    # 第 4 行顯示第 4 顆雷射的距離
    ev3.screen.draw_text(0, 48, "D4 " + str(read_d4()) + "      ", Color.BLACK, Color.WHITE)
    # 第 5 行顯示第 5 顆雷射的距離
    ev3.screen.draw_text(0, 64, "D5 " + str(read_d5()) + "      ", Color.BLACK, Color.WHITE)
    # 第 6 行顯示第 6 顆雷射的距離
    ev3.screen.draw_text(0, 80, "D6 " + str(read_d6()) + "      ", Color.BLACK, Color.WHITE)
    # 第 7 行顯示第 7 顆雷射的距離
    ev3.screen.draw_text(0, 96, "D7 " + str(read_d7()) + "      ", Color.BLACK, Color.WHITE)
    # 第 8 行顯示第 8 顆雷射的距離
    ev3.screen.draw_text(0, 112, "D8 " + str(read_d8()) + "      ", Color.BLACK, Color.WHITE)
    # 等 0.1 秒
    wait(100)

Step by step:

  1. Clear the screen
  2. Repeat forever
    • Row 1 shows the distance of laser 1
    • Row 2 shows the distance of laser 2
    • Row 3 shows the distance of laser 3
    • Row 4 shows the distance of laser 4
    • Row 5 shows the distance of laser 5
    • Row 6 shows the distance of laser 6
    • Row 7 shows the distance of laser 7
    • Row 8 shows the distance of laser 8
    • Wait 0.1 s

What you should see: 8 rows of numbers, one per laser; bring your hand near a laser and that row gets smaller.

FAQ:

  • The number does not match a ruler? On EV3, 1 unit = 1.2 cm (for example 25 = 30 cm); 100 means nothing within 120 cm ahead.
  • Is laser 1 on the left or the right? Try it with your hand and write it down; you will need it when programming.

Example 4: Sumo chaser: turn towards the nearest object and charge

What this example does: Mode 2 tells you directly "where the nearest object is and how far", so you do not have to compare 8 numbers yourself. The robot turns towards the nearest object and charges; when it sees nothing it spins to search.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • TOF8 eight-direction laser (EV3 edition)
  • A can as the opponent

How to connect: TOF8 eight-direction laser in port 1, mounted at the front with lasers 4 and 5 facing straight ahead, about 5 cm above the ground.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0

Pybricks v4 code
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 04:相撲追擊:轉向最近的東西衝過去
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:TOF8 八方位雷射插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:模式 2 會直接告訴你「最近的東西在哪個方向、有多遠」,不用自己比 8 個數字。車子轉向最近的東西衝過去,看不到東西時原地轉圈找。
# ============================================================

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)       # TOF8 八方位雷射插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_nearDist():
    # 最近的東西有多遠:0 ~ 100
    return sensor.read(2)[1]


def read_nearDir():
    # 最近的東西在哪個方向:-7 ~ +7(負=左、正=右、0 附近=正前方;±8=沒有東西)
    return sensor.read(2)[0]


# ---------- 程式從這裡開始 ----------
# 一直重複
while True:
    # 如果最近的東西在 60 以內(約 72 公分)
    if read_nearDist() < 60:
        # 如果方向小於 -1(偏左)
        if read_nearDir() < (-1):
            # 東西在左邊:原地往左轉
            left_motor.dc(-20)
            right_motor.dc(20)
        # 否則,如果方向大於 1(偏右)
        elif read_nearDir() > 1:
            # 東西在右邊:原地往右轉
            left_motor.dc(20)
            right_motor.dc(-20)
        else:
            # 東西在正前方:全力衝過去
            left_motor.dc(60)
            right_motor.dc(60)
    else:
        # 附近沒東西:原地轉圈找對手
        left_motor.dc(25)
        right_motor.dc(-25)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 04:相撲追擊:轉向最近的東西衝過去
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:TOF8 八方位雷射插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:模式 2 會直接告訴你「最近的東西在哪個方向、有多遠」,不用自己比 8 個數字。車子轉向最近的東西衝過去,看不到東西時原地轉圈找。
# ============================================================

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)     # TOF8 八方位雷射插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_nearDist():
    # 最近的東西有多遠:0 ~ 100
    return sensor.read('IR-REMOTE')[1]


def read_nearDir():
    # 最近的東西在哪個方向:-7 ~ +7(負=左、正=右、0 附近=正前方;±8=沒有東西)
    return sensor.read('IR-REMOTE')[0]


# ---------- 程式從這裡開始 ----------
# 一直重複
while True:
    # 如果最近的東西在 60 以內(約 72 公分)
    if read_nearDist() < 60:
        # 如果方向小於 -1(偏左)
        if read_nearDir() < (-1):
            # 東西在左邊:原地往左轉
            left_motor.dc(-20)
            right_motor.dc(20)
        # 否則,如果方向大於 1(偏右)
        elif read_nearDir() > 1:
            # 東西在右邊:原地往右轉
            left_motor.dc(20)
            right_motor.dc(-20)
        else:
            # 東西在正前方:全力衝過去
            left_motor.dc(60)
            right_motor.dc(60)
    else:
        # 附近沒東西:原地轉圈找對手
        left_motor.dc(25)
        right_motor.dc(-25)

Step by step:

  1. Repeat forever
    • If the nearest object is within 60 (about 72 cm)
      • If the direction is less than -1 (to the left)
        • Object on the left: spin left on the spot
        • Otherwise:
          • Otherwise, if the direction is greater than 1 (to the right)
            • Object on the right: spin right on the spot
            • Otherwise:
              • Object straight ahead: charge at full power
      • Otherwise:
        • Nothing nearby: spin on the spot to look for the opponent

What you should see: Put the can anywhere around the robot: it turns towards the can and pushes it.

FAQ:

  • The direction reads 8 or -8? ±8 means nothing within 120 cm in that direction, not "far left" or "far right".
  • It overshoots and swings back and forth? Lower the turning power 20, or change -1 and 1 to -2 and 2.

Example 5: Drive towards the most open direction

What this example does: Mode 2 also tells you "the most open direction". With proportional control the robot keeps heading for open space and finds its way out of a box maze.

What you need:

  • EV3 brick + Education base robot (left wheel on B, right wheel on C)
  • TOF8 eight-direction laser (EV3 edition)
  • A few boxes as obstacles

How to connect: TOF8 eight-direction laser in port 1, mounted at the front with lasers 4 and 5 facing straight ahead, about 5 cm above the ground.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0

Pybricks v4 code
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 05:往最空曠的方向走
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:TOF8 八方位雷射插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:模式 2 也會告訴你「最空曠的方向」。車子用比例控制一直往最空的地方開,在箱子陣裡會自己找出口。
# ============================================================

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)       # TOF8 八方位雷射插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_farDir():
    # 最空曠的方向:-7 ~ +7(負=左、正=右)
    return sensor.read(2)[2]


# ---------- 程式從這裡開始 ----------
# 一直重複
while True:
    # 轉彎力量 turn = 最空方向 × 5(負數往左、正數往右)
    turn = read_farDir() * 5
    # 左輪=25+turn、右輪=25-turn
    left_motor.dc(25 + turn)
    right_motor.dc(25 - turn)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 05:往最空曠的方向走
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:TOF8 八方位雷射插 1 號孔;左輪馬達接 B、右輪馬達接 C
# 這個範例會做什麼:模式 2 也會告訴你「最空曠的方向」。車子用比例控制一直往最空的地方開,在箱子陣裡會自己找出口。
# ============================================================

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)     # TOF8 八方位雷射插 1 號孔
left_motor = Motor(Port.B)        # 左輪
right_motor = Motor(Port.C)       # 右輪


def read_farDir():
    # 最空曠的方向:-7 ~ +7(負=左、正=右)
    return sensor.read('IR-REMOTE')[2]


# ---------- 程式從這裡開始 ----------
# 一直重複
while True:
    # 轉彎力量 turn = 最空方向 × 5(負數往左、正數往右)
    turn = read_farDir() * 5
    # 左輪=25+turn、右輪=25-turn
    left_motor.dc(25 + turn)
    right_motor.dc(25 - turn)

Step by step:

  1. Repeat forever
    • Steering power turn = most open direction × 5 (negative = left, positive = right)
    • Left wheel = 25 + turn, right wheel = 25 − turn

What you should see: The robot avoids the boxes and drives towards open space.

FAQ:

  • The direction reads 8 or -8? ±8 means nothing within 120 cm in that direction, not "far left" or "far right".
  • It keeps driving into walls? The sign of the direction may be opposite to your robot: swap "+turn" and "−turn" in the program.

Example 6: Where is the nearest object, and how many millimetres away

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

What this example does: Mode 5 gives two numbers at once: how many millimetres away the nearest object is, and in which direction (-7~+7). That is 12 times finer than the 1.2 cm units of mode 2. This example shows both numbers and beeps when something comes within 15 cm.

What you need:

  • TOF8 eight-direction laser (EV3 edition, firmware v4.0 or later)
  • EV3 brick

How to connect: TOF8 eight-direction laser in port 1.

Download the program (pick your software):

⬇ Pybricks v4 | ⬇ EV3 MicroPython v2.0

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

Pybricks v4 code
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 06:最近的東西在哪、幾公釐
# 軟體:Pybricks v4(用 Chrome 打開 https://code.pybricks.com ,EV3 版目前是 Beta)
# 接法:TOF8 八方位雷射插 1 號孔
# 這個範例會做什麼:模式 5 一次給兩個數字:最近的東西有幾公釐、在哪個方向(-7~+7)。比模式 2 的 1.2 公分一格準 12 倍。這個範例把兩個數字顯示在螢幕上,東西靠到 15 公分內就嗶一聲。
# ============================================================

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)       # TOF8 八方位雷射插 1 號孔


def read_nearMm():
    # 最近的東西有多遠(公釐):0 ~ 1200 mm
    return sensor.read(5)[0]


def read_nearDir5():
    # 最近的東西在哪個方向:-7 ~ +7(負=左、正=右、0 附近=正前方;±8=沒有東西)
    return sensor.read(5)[1]


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行顯示最近的東西有幾公釐
    ev3.screen.draw_text(0, 0, "NEARMM " + str(read_nearMm()) + "      ", Color.BLACK, Color.WHITE)
    # 第 2 行顯示它在哪個方向(負=左、正=右、0=正前方)
    ev3.screen.draw_text(0, 16, "DIR " + str(read_nearDir5()) + "      ", Color.BLACK, Color.WHITE)
    # 如果最近的東西不到 150 公釐(15 公分)
    if read_nearMm() < 150:
        # 不到 15 公分:嗶一聲
        ev3.speaker.beep()
    # 等 0.1 秒
    wait(100)
EV3 MicroPython v2.0 code
#!/usr/bin/env pybricks-micropython
# ============================================================
# TOF8 八方位雷射測距感應器 EV3 新手範例 06:最近的東西在哪、幾公釐
# 軟體:樂高官方 EV3 MicroPython v2.0(microSD 開機卡+VS Code「LEGO MINDSTORMS EV3 MicroPython」擴充套件)
# 接法:TOF8 八方位雷射插 1 號孔
# 這個範例會做什麼:模式 5 一次給兩個數字:最近的東西有幾公釐、在哪個方向(-7~+7)。比模式 2 的 1.2 公分一格準 12 倍。這個範例把兩個數字顯示在螢幕上,東西靠到 15 公分內就嗶一聲。
# ============================================================

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)     # TOF8 八方位雷射插 1 號孔


def read_nearMm():
    # 最近的東西有多遠(公釐):0 ~ 1200 mm
    return sensor.read('IR-CAL')[0]


def read_nearDir5():
    # 最近的東西在哪個方向:-7 ~ +7(負=左、正=右、0 附近=正前方;±8=沒有東西)
    return sensor.read('IR-CAL')[1]


# ---------- 程式從這裡開始 ----------
# 清除螢幕
ev3.screen.clear()
# 一直重複
while True:
    # 第 1 行顯示最近的東西有幾公釐
    ev3.screen.draw_text(0, 0, "NEARMM " + str(read_nearMm()) + "      ", Color.BLACK, Color.WHITE)
    # 第 2 行顯示它在哪個方向(負=左、正=右、0=正前方)
    ev3.screen.draw_text(0, 16, "DIR " + str(read_nearDir5()) + "      ", Color.BLACK, Color.WHITE)
    # 如果最近的東西不到 150 公釐(15 公分)
    if read_nearMm() < 150:
        # 不到 15 公分:嗶一聲
        ev3.speaker.beep()
    # 等 0.1 秒
    wait(100)

Step by step:

  1. Clear the screen
  2. Repeat forever
    • Row 1 shows the nearest distance in mm
    • Row 2 shows its direction (negative = left, positive = right, 0 = straight ahead)
    • If the nearest object is closer than 150 mm (15 cm)
      • Closer than 15 cm: beep
    • Wait 0.1 s

What you should see: Move your hand around the sensor: row 1 follows in millimetres and row 2 tells you whether your hand is on the left (negative) or right (positive); within 15 cm the EV3 keeps beeping.

FAQ:

  • The direction reads 8 or -8? ±8 means nothing within 120 cm in that direction, not "far left" or "far right".
  • 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.

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

More questions

  • The EV3 shows an infrared sensor? Expected: the sensor emulates a LEGO sensor so the stock blocks can read it.
  • A short freeze when switching modes? The EV3 needs about 1 s per mode change. Stick to one mode during a run.

Still stuck? Contact us with your software and a screenshot of the program.

Downloads

Sources

  • Host and software list:_build/guides.json
  • Values and commands:_build/protocol/tof8.json (single source of truth)
  • Setup:_build/guides/software/ev3-pybricks.en.md、_build/guides/product/tof8/ev3-pybricks.en.md
  • Examples:sensors/tof8/ev3-examples.md
Email for a quoteBuying Guide