Ranger 8-Way (TOF8) × SPIKE × MBC Blocks website
Interactive tutorialSPIKE
First time? Try the interactive tutorial in your browser first, then follow the steps below with your hub.
This page covers MBC SPIKE Blocks website only: how to set it up for the Ranger 8-Way (TOF8) on SPIKE Prime / Robot Inventor, 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) (SPIKE edition).
- Hub: SPIKE Prime or Robot Inventor.
- Computer: open the website in Chrome. Nothing to install.
- Hub firmware: must be switched to Pybricks firmware first (step 2 below, done from the website).
Install the software
- Open the MBC SPIKE Blocks website (blocks with a Chinese interface, nothing to install).
- Switch the firmware: top menu "SPIKE Hub → Install/Update Pybricks firmware", follow the screen, about 3 minutes. The website flashes Pybricks v4.1.0.
- To go back to the official LEGO App later, switch back to the original LEGO firmware with the SPIKE App.
Make the software see the sensor
No library needed: the MBC sensor blocks are built into the website, one block per value.
- To open an example: top menu "File → Open" (or press Ctrl+O) → pick the downloaded
.abp. - The Blocks website reads more data than the official App (no need to split numbers yourself), but example 6, marked "Pybricks only", uses a new mode that the Blocks website cannot read either.
Wiring
- Plug the sensor into hub port C with the cable (any of A~F works; choose the same port in the blocks).
- Latch up, push the plug in gently.
- When the hub is on, the light on the sensor comes on.
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
- Connect the website to the hub (the hub must be running Pybricks firmware).
- Open the
.abpof example 1 on this page and press Run. - If the number changes with what is in front of the sensor, everything is connected.
FAQ
- The website can't connect to the hub? The hub still has the original LEGO firmware: do step 2 of "Install the software" to switch to Pybricks firmware.
- The official App can't connect any more? The hub now runs Pybricks firmware: switch back to the original LEGO firmware with the SPIKE App.
- Nothing happens when I plug it in? Check in order: cable firmly in → another port → another cable → another hub.
Reading values
The hub needs Pybricks firmware. The MBC Blocks website reads mode 9 of the sensor (all values in one read).
nearest distance (cm) nearCm
| Range | Meaning |
|---|---|
0~120 cm |
smallest of the 8 distances, 0~120 cm (a failed sensor counts as 120); 120 = nothing around |
Add this sensor's blocks on the MBC Blocks website and use the "nearest distance (cm)" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
w = dev.read(9)[4]
nearCm = ((w >> 8) & 0xFF) # nearest distance (cm)(hi byte)nearest direction nearDir
| 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) |
Add this sensor's blocks on the MBC Blocks website and use the "nearest direction" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
w = dev.read(9)[4]
nearDir = (w & 0xFF) - 8 # nearest direction(lo byte)farthest distance (cm) farCm
| Range | Meaning |
|---|---|
0~120 cm |
largest of the 8 distances, 0~120 cm (a failed sensor always reads 120 and looks 'farthest') |
Add this sensor's blocks on the MBC Blocks website and use the "farthest distance (cm)" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
w = dev.read(9)[5]
farCm = ((w >> 8) & 0xFF) # farthest distance (cm)(hi byte)farthest direction farDir
| Range | Meaning |
|---|---|
-7~+7 |
direction of the most open side: −7 to +7, 0 = straight ahead, negative = left; 0 when all 8 read 120 |
Add this sensor's blocks on the MBC Blocks website and use the "farthest direction" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
w = dev.read(9)[5]
farDir = (w & 0xFF) - 8 # farthest direction(lo byte)init bitmap initOk
| Range | Meaning |
|---|---|
0~255 bit |
bit0 = sensor 1 (left) … bit7 = sensor 8; 1 = initialised at power-on (8 sensors take |
Add this sensor's blocks on the MBC Blocks website and use the "init bitmap" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
w = dev.read(9)[6]
initOk = (w & 0xFF) # init bitmap(lo byte)fault bitmap fault
| Range | Meaning |
|---|---|
0~255 bit |
bit0 = sensor 1 … bit7 = sensor 8; 1 = currently failing (I2C error or no update for 200 ms); its distance reads 120 / 1200 |
Add this sensor's blocks on the MBC Blocks website and use the "fault bitmap" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
w = dev.read(9)[6]
fault = ((w >> 8) & 0xFF) # fault bitmap(hi byte)sensor 1~8 distance (cm) distCm
| Range | Meaning |
|---|---|
0~120 cm |
distance of each sensor 1 |
Add this sensor's blocks on the MBC Blocks website and use the "sensor 1~8 distance (cm)" block; on the hub it runs this Pybricks code (mode 9):
from pybricks.iodevices import PUPDevice
from pybricks.parameters import Port
dev = PUPDevice(Port.A) # sensor on port A
words = dev.read(9)[0:4]
distCm = []
for w in words:
distCm.append(w & 0xFF) # odd channel
distCm.append((w >> 8) & 0xFF) # even channelNot readable here:front distance (cm), nearest distance (mm), front distance (mm), sensor 1~8 distance (mm). Use Pybricks for these.
Examples
Example 1: Which direction is the nearest thing
What this example does: the hub display shows which direction the nearest thing is in (-7 to 7). Hand on the left shows a negative number, straight ahead shows 0, on the right shows a positive number; nothing around shows ✕.
What you need
- 1 SPIKE hub
- 1 Ranger 8-Way
- Your hand (or a book)
How to connect: plug the Ranger 8-Way into port C.
Download the program
| MBC Blocks website |
|---|
| ex01_nearest_direction.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- Repeat forever: the blocks inside keep running until you press stop.
- Read the nearest distance and direction: the MBC Blocks website and Pybricks have "nearest target distance" and "nearest target direction" directly.
In the official App you build it yourself, in 3 parts:
- Read the 8 channels: following the table above, split the 5 blocks into 8 variables, "ch1" to "ch8".
- Compare channel by channel: start by assuming the "nearest distance" is 120 and the "nearest channel" is 0; then go from channel 1 to channel 8, and if this channel is closer than the "nearest distance", remember it. After the comparison, "nearest channel" is the closest channel (it stays 0 if nothing is there).
- Convert to a direction: direction = channel × 2 − 9 (channel 1 → -7, channel 4 → -1, channel 5 → 1, channel 8 → 7).
- If nothing is there (MBC Blocks website and Pybricks: nearest distance ≥ 120; official App: nearest channel = 0) → show ✕.
- Else → show the direction on the display. The MBC Blocks website and Pybricks also print the direction and distance on the computer screen.
What you should see when it runs
- Nothing around: ✕.
- Hand 30 cm to the far left: -7; front-left: -5 or -3.
- Hand straight ahead: 0 (the official App version shows -1 or 1, because it picks only one channel, and both channels 4 and 5 point straight ahead).
- Hand on the right: a positive number.
Common questions
- Left and right are swapped? → Your sensor is mounted the opposite way from the table (for example upside down), so you will need to swap the left and right turns in the later examples too.
- It always shows ✕? → Wait 2 to 4 seconds after power-on; or your hand is too far away (over 120 cm), or the object is black/transparent.
- The MBC Blocks website shows even numbers (for example -6, -2)? → That's normal. When your hand is right between two channels and both see it, the direction is averaged.
- It shows the same number wherever my hand is? → One channel may be broken or blocked and keeps reading a very small number. Check with the "channel N OK" block on the MBC Blocks website or
laser8.is_ok(1)tois_ok(8)in Pybricks.
Example 2: Change the light when something gets close
What this example does: when something gets close in any of the 8 directions, the hub light changes: yellow within 50 cm, red with beeping within 20 cm, green when nothing is near. Like a parking sensor.
What you need: same as Example 1.
How to connect: plug the Ranger 8-Way into port C.
Download the program
| MBC Blocks website |
|---|
| ex02_near_light.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- Read the nearest distance: the official App uses the "read 8 channels + compare channel by channel" blocks from Example 1; the other two read "nearest target distance" directly.
- If nearest distance < 20 → red light and a beep.
- Else, if nearest distance < 50 → yellow light.
- Otherwise → green light.
- Wait 0.1 seconds, then check again.
What you should see when it runs: move your hand in slowly from far away, and the light goes green → yellow → red (beeping). It changes the same way when your hand comes in from the left or the right.
Common questions
- Why check < 20 first, then < 50? → 15 cm is also "less than 50". If you checked < 50 first, the red light would never come on. Write the conditions starting from the strictest one.
- It turns green when my hand touches the sensor? → When something is extremely close (within a few cm), the laser may not be able to measure it and reads 120. Leave a little gap.
- It stays yellow when sitting on the desk? → The side channels are seeing other things on the desk, or the sensor is mounted too low and sees the desktop.
Example 3: Turn to face the nearest thing
What this example does: the robot turns on the spot so its front faces the nearest thing (within 60 cm). Hand on the left → it turns left; on the right → it turns right; once it is facing you, it stops.
What you need
- Everything from Example 1, plus
- A two-wheeled robot car (two motors as the wheels)
How to connect
- Plug the Ranger 8-Way into port C, mounted at the front of the car, with channels 4 and 5 pointing straight ahead, at least 5 cm off the floor
- Left wheel motor on E, right wheel motor on F
Download the program
| MBC Blocks website |
|---|
| ex03_turn_to_nearest.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- Tell the hub where the wheels are: in the official App it is "set movement motors to E+F" at 20% speed; on the MBC Blocks website and in Pybricks it is "drive base: left wheel E, right wheel F".
- Read the nearest distance and direction (in the official App, the "nearest channel").
- If nearest distance > 60 → too far or nothing there → stop and wait.
- Else, if the thing is on the left (direction < -1; official App: channels 1 to 3) → turn left on the spot.
- Else, if the thing is on the right (direction > 1; official App: channels 6 to 8) → turn right on the spot.
- Otherwise (straight ahead) → stop.
What you should see when it runs: put your hand on the robot's left and it turns to face your hand; walk slowly around it and it keeps turning to follow you.
Common questions
- It overshoots and wobbles from side to side of my hand? → It is turning too fast. MBC Blocks website and Pybricks: make the "90" smaller (for example 60); official App: lower the 20% speed (for example 15%).
- It turns the wrong way and gets farther off? → Go back to Example 1 and confirm left and right; or the motors are mounted the other way round (see the common questions in Example 4).
- It never stops turning? → A nearby wall or table leg is closer than your hand, so it goes after the wall. Play in an open area.
Example 4: Avoid obstacles by heading for the most open side
What this example does: the robot keeps driving forward. When something is within 30 cm straight ahead, it stops, checks which side is more open, turns 45 degrees that way, then keeps going.
What you need: same as Example 3, plus an open floor area with a few boxes as obstacles.
How to connect: plug the Ranger 8-Way into port C, mounted at the front; left wheel motor on E, right wheel motor on F.
Download the program
| MBC Blocks website |
|---|
| ex04_avoid_open_side.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- Set up the wheels and speed: 30% in the official App; 150 mm per second in the other two.
- Repeat forever: check whether channel 4 or channel 5, the ones pointing straight ahead, reads less than 30 cm.
- If yes → stop, then find the most open direction:
- The MBC Blocks website and Pybricks read "most open direction" directly: negative (the left is more open) → turn left 45 degrees, otherwise turn right 45 degrees.
- The official App uses the same "compare channel by channel" as Example 1, but looks for the farthest channel instead: channels 1 to 4 → turn left, channels 5 to 8 → turn right. The official App's 45-degree turn is "move 0.25 rotations with steering -100 (or 100)", which is roughly 45 degrees on the standard LEGO car.
- Else → drive forward.
What you should see when it runs: the car drives forward, stops just before hitting a box, turns toward the more open side, then carries on. In a corner it keeps turning until it finds a way out.
Common questions
- It only stops after bumping into the obstacle? → Too fast, or the obstacle is black/transparent. Lower the speed, or raise the "30".
- It clips things on the side? → This example only looks at the two front channels. For extra safety, add channels 3 and 6 to the "or" so they are checked as well.
- It turns toward the opposite side from the open one? → Go back to Example 1 and confirm left and right; if they are swapped, swap the left and right turns.
- The car drives backward / the direction is reversed? → The motors are mounted the opposite way from the standard LEGO car. Official App: mount the motors the way the LEGO "standard driving base" does; MBC Blocks website: in the "drive base" block, flip the "forward" direction of both the left and right wheel; Pybricks: change the left wheel to
Direction.CLOCKWISEand the right wheel toMotor(Port.F, Direction.COUNTERCLOCKWISE). - What happens if one channel is broken? → A broken channel always reads 120, so it gets treated as "the most open", and the robot may turn toward an obstacle. Check each channel using the common questions in Example 1.
Example 5: Follow your hand
What this example does: the robot follows your hand and keeps a distance of 15 to 25 cm: when your hand is a bit far (25 to 60 cm) it drives forward while steering toward your hand; at just the right distance it stops; too close (under 15 cm) it backs up; hand taken away (over 60 cm) it stops.
What you need: same as Example 3.
How to connect: plug the Ranger 8-Way into port C, mounted at the front; left wheel motor on E, right wheel motor on F.
Download the program
| MBC Blocks website |
|---|
| ex05_follow_hand.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- Read the distance and direction of your hand (the nearest thing) (the official App uses the blocks from Example 1).
- Split it into 4 cases by distance, checked from top to bottom:
- > 60 cm → can't see the hand → stop.
- < 15 cm → too close → back up (MBC Blocks website and Pybricks: 80 mm per second).
- > 25 cm → a bit far → drive forward, and steer as much as the hand is off center: the MBC Blocks website and Pybricks turn at "direction × 15" degrees per second; the official App's steering is "direction × 10" (direction is still channel × 2 − 9).
- Everything else (15 to 25 cm) → just right → stop.
What you should see when it runs: put your hand 40 cm in front of the robot and it drives forward; move your hand to the left and it turns left while driving; hold your hand still and it stops about 20 cm away; push your hand toward it and it backs up.
Common questions
- It keeps jittering back and forth? → The "just right" range is too narrow. Change 15 to 12 and 25 to 30.
- It steers too sharply / too slowly? → Change the "× 15" number (the official App uses "× 10"): bigger turns faster, smaller turns slower.
- It follows the wall instead of my hand? → Something nearby is closer than your hand. Play in an open area where your hand is the nearest thing.
- The official App version doesn't back up? → Make sure the direction on the "start moving" block is the "backward" arrow.
Note
These examples have no version for this software: Example 6 (advanced): How many millimeters away is the nearest thing
More questions
- The hub does not detect the sensor? Unplug and replug, wait 2 s, try another port. The official app shows it as a colour sensor — that is expected.
- Can I use the official app and Pybricks on the same hub? One firmware at a time: Pybricks and the MBC Blocks website need Pybricks firmware, the official app needs LEGO firmware; you can switch back any time with the SPIKE app.
Still stuck? Contact us with your software and a screenshot of the program.
Downloads
- MBC SPIKE Blocks website (online, nothing to install)
- Download centre (libraries, firmware, catalogue for every product)
Sources
- Host and software list:
_build/guides.json - Values and commands:
_build/protocol/tof8.json(single source of truth) - Setup:
_build/guides/software/spike-mbcblocks.en.md、_build/guides/product/tof8/spike-mbcblocks.en.md - Examples:
sensors/tof8/spike-examples.md