Ranger 2-Way (TOF2) × 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 2-Way (TOF2) 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 2-Way (TOF2) (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.
Which side is left
Left and right are as seen from behind the sensor, looking the way it faces (left = J5, right = J3). Facing the sensor, they are the other way round, so when you test with your hand, stand behind the sensor and look the same way it does. "Left is closer" and "turn left" in the examples follow this direction.
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).
left distance (cm) leftCm
| Range | Meaning |
|---|---|
0~120 cm |
distance from the left sensor, 0~120 cm (left = sensor 0 seen from behind the robot); 120 = nothing in range / sensor failed |
Add this sensor's blocks on the MBC Blocks website and use the "left 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)[0]
leftCm = (w & 0xFF) # left distance (cm)(lo byte)right distance (cm) rightCm
| Range | Meaning |
|---|---|
0~120 cm |
distance from the right sensor, 0~120 cm (right = sensor 1); 120 = nothing in range / sensor failed |
Add this sensor's blocks on the MBC Blocks website and use the "right 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)[0]
rightCm = ((w >> 8) & 0xFF) # right distance (cm)(hi byte)init bitmap initOk
| Range | Meaning |
|---|---|
0~3 bit |
bit0 = left, bit1 = right; 1 = that sensor initialised at power-on (no retry); 3 = both OK |
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)[1]
initOk = (w & 0xFF) # init bitmap(lo byte)fault bitmap fault
| Range | Meaning |
|---|---|
0~3 bit |
bit0 = left, bit1 = right; 1 = that sensor is 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)[1]
fault = ((w >> 8) & 0xFF) # fault bitmap(hi byte)Not readable here:nearer distance (cm), left distance (mm), right distance (mm), average distance (mm). Use Official LEGO App / Pybricks for these.
Examples
Example 1: See the distance
What this example does: the hub display keeps scrolling two numbers, "left distance right distance" (in cm), for example "40 80".
What you need
- 1 SPIKE hub
- 1 Ranger 2-Way
- A book or a box to use as an "obstacle"
How to connect: plug the Ranger 2-Way into port C.
Download the program
| MBC Blocks website |
|---|
| ex01_show_distance.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 left and right distance: in the official App that is "raw red ÷ 256, rounded down" and "raw green ÷ 256, rounded down", stored in two "variables" (a variable is a box with a name that holds one number). The MBC Blocks website and Pybricks have "left laser distance" and "right laser distance" directly.
- Show it on the display: join "left distance", a space and "right distance" into one piece of text and scroll it on the hub display. The MBC Blocks website and Pybricks also print it on the computer screen.
- Wait 0.2 seconds, then read again.
What you should see when it runs
- Nothing in front: it shows 120 120.
- Put a book 30 cm in front: both numbers go to about 30.
- Cover only the left laser with your hand: the first number drops, the second one stays the same.
Important
Use this example to confirm left and right first. With the sensor facing forward and the component side up, stand behind it looking the same way and cover only the left laser (J5): the first number should drop. If the second number drops instead, your sensor is mounted the opposite way from the examples (for example upside down), and you will need to swap "left" and "right" in the later examples too.
Common questions
- It always shows 120 120? → There really is nothing in front, or the object is too far away, or it is black/transparent. Try a white book 20 cm in front. It also shows 120 for the first 1 to 2 seconds after power-on.
- Only one side is stuck at 120? → That laser may be blocked, dirty or broken. You can check it with the "laser OK" block on the MBC Blocks website or
laser2.is_ok(1)in Pybricks. - The numbers keep jumping? → That's normal. The lasers measure a dozen or more times per second and have an error of 1 to 2 cm.
- The official App shows numbers in the thousands? → You forgot the "÷ 256, rounded down" step.
Example 2: Beep when too close
What this example does: if either side sees something closer than 20 cm, the hub light turns red and the hub beeps; otherwise it stays green.
What you need: same as Example 1.
How to connect: plug the Ranger 2-Way into port C.
Download the program
| MBC Blocks website |
|---|
| ex02_too_close_beep.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- Read the distance: the official App reads the left and right distances; the MBC Blocks website and Pybricks read the "nearer distance" (the smaller of the two) directly.
- If too close then…: the official App writes it as "left < 20 or right < 20", which means the same as "nearer distance < 20" → red light and a beep.
- Else…: green light.
- Wait 0.1 seconds, then check again.
What you should see when it runs
- Nothing in front: green light.
- Move your hand in slowly; once it is within 20 cm: red light and constant beeping. Take your hand away and it goes back to green.
Common questions
- It goes quiet 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. That is just how the laser works; leave a little gap.
- Want it to beep at 30 cm instead? → Change "20" to "30" (in the official App there are two 20s, change both).
Example 3: Which side is closer
What this example does: the hub display uses an arrow to tell you where the object is: left side closer → ⬅, right side closer → ➡, about the same → ⬆, nothing there → ✕.
What you need: same as Example 1.
How to connect: plug the Ranger 2-Way into port C.
Download the program
| MBC Blocks website |
|---|
| ex03_which_closer.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does
- First work out left minus right = left distance minus right distance. When the left side is closer, the left distance is smaller, so the result is negative.
- If both sides are 120 → show ✕ (nothing in front; check this first). The official App writes it as "left > 119 and right > 119".
- Else, if left minus right < -5 → the left side is closer by more than 5 cm → left arrow.
- Else, if left minus right > 5 → the right side is closer by more than 5 cm → right arrow.
- Otherwise (less than 5 cm difference) → up arrow, meaning the object is straight ahead.
What you should see when it runs: move a book slowly from the left to the right (about 30 cm away). The arrow goes ⬅ → ⬆ → ➡; take the book away and it becomes ✕.
Common questions
- Why does it have to differ by more than 5 cm? → The lasers themselves have an error of 1 to 2 cm. Leaving some room keeps the arrow from flickering back and forth.
- The left and right arrows are swapped? → Left and right are as seen from behind the sensor, looking the way it faces (left = J5, right = J3); facing the sensor, they are the other way round. If they are still swapped seen from behind, go back to Example 1 and confirm left and right; the sensor may be mounted the other way round.
- The arrow jumps around when the object is far away? → The two laser beams get wider with distance and overlap far away, so beyond 50 cm it is hard to tell left from right. Play with this example within 50 cm.
Example 4: Back up and turn when something is ahead
What this example does: the robot keeps driving forward. When something is in front (closer than 20 cm), it stops → beeps → backs up 10 cm → turns right 90 degrees, then keeps going.
What you need
- Everything from Example 1, plus
- A two-wheeled robot car (two motors as the wheels)
- An open floor area with a few boxes as obstacles
How to connect
- Plug the Ranger 2-Way into port C, mounted at the front of the car, pointing straight ahead, at least 5 cm off the floor (too low and it will measure the floor)
- Left wheel motor on E, right wheel motor on F
Download the program
| MBC Blocks website |
|---|
| ex04_obstacle_back.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"; on the MBC Blocks website and in Pybricks it is "drive base: left wheel E, right wheel F".
- Set the speed: 30% in the official App; 150 mm per second in the other two.
- Repeat forever: read the distance.
- If nearer distance < 20 → stop → beep → back up 10 cm → turn right on the spot. The official App's "turn right" is "move 0.5 rotations with steering 100", which is roughly 90 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, beeps, backs up a little, turns right, then carries on.
Common questions
- It only stops after bumping into the obstacle? → It is driving too fast, or the obstacle is black/transparent (cannot be measured). Lower the speed, or raise the "20" a bit (for example 25).
- It turns less than 90 degrees / too much? → Official App: change the "0.5 rotations" number; MBC Blocks website and Pybricks: change the "90".
- 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). - It keeps spinning on the spot and never moves forward? → The sensor is mounted too low and is measuring the floor. Raise the sensor a bit and tilt it slightly upward.
Example 5: Drive up to a wall and square up
What this example does: the robot drives forward and stops 20 cm from the wall, then turns slowly on the spot until both lasers read the same distance (within 1 cm), which means the front of the car is facing the wall squarely. When done it beeps and shows a smiley face on the display. Teams often use this trick in competitions to "square up" the robot.
What you need: same as Example 4, plus a wall (or the flat side of a big cardboard box).
How to connect: plug the Ranger 2-Way into port C, mounted at the front pointing forward; left wheel motor on E, right wheel motor on F.
Download the program
| MBC Blocks website |
|---|
| ex05_square_up.abp |
🟦 Blocks on the MBC Blocks website

Step by step: what the program does (this example is not a "repeat forever"; it runs 3 steps in order and then ends)
- Step 1: drive forward, "wait until" the average distance ≤ 20 cm, then stop. In the official App the average distance is "raw blue ÷ 256, rounded down", written as "< 21", which means the same thing.
- Step 2: square up. "Repeat until" left and right differ by 1 cm or less:
- left minus right < 0 (left side closer) → the front is pointing to the right → turn left;
- else → turn right.
- The official App sets "left minus right" to 99 first, so that the "repeat until" checks at least once.
- Step 3: done: stop, beep, show a smiley face.
What you should see when it runs: place the car at a slight angle to the wall (front pointing a little off), then press run. The car drives forward, stops, wiggles left and right to square up, then beeps.
Common questions
- It keeps turning left and right and never stops? → The wall is not flat, or "within 1 cm" is too strict. Change the "1" to "2" (in the official App, change "< 2" to "< 3").
- It squares up the wrong way and gets more crooked? → Left and right are as seen from behind the sensor, looking the way it faces (left = J5, right = J3). Go back to Example 1 and confirm left and right; if they are swapped, swap "turn left" and "turn right".
- It cannot square up when the angle is too big (over 30 degrees)? → At a big angle one laser misses the wall completely (reads 120). Line the car up roughly first, then start.
- The wall is black? → Black walls read shorter and less accurately. Use a light-colored wall or stick white paper on it.
Note
These examples have no version for this software: Example 6 (advanced): Park precisely using millimeters
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/tof2.json(single source of truth) - Setup:
_build/guides/software/spike-mbcblocks.en.md、_build/guides/product/tof2/spike-mbcblocks.en.md - Examples:
sensors/tof2/spike-examples.md