MBC Ranger 2-Way (TOF2) × official LEGO SPIKE App

Read the Ranger in the official LEGO SPIKE App in three steps

In the official LEGO SPIKE App, the Ranger 2-Way shows up as a Color Sensor. So you do not import a library: you read it with the raw red / green / blue blocks under "More Sensors" and then convert the numbers into the left, right and average distance. Work through the steps on the left and the blocks on the right grow with you. These block pictures were built in the official web app (spike.legoeducation.com) and exported, so they look exactly like what you see in the App.

1

Start: program start + forever

A sensor has to be read again and again. First put the yellow "when program starts" block, attach "forever" under it, and put "wait 0.1 seconds" inside, so it reads every 0.1 seconds.

  1. Open the official LEGO SPIKE App, start a new project and choose "Word Blocks".
  2. From "Events", drag out "when program starts". From "Control", drag "forever" and attach it underneath.
  3. Drag a "wait 1 seconds" block into forever and change the number to 0.1.
2

Meet the three reading blocks and pick the right port

The official App reads the Ranger with three blocks from "More Sensors": raw red, raw green and raw blue. Each block starts with a port drop-down; pick the port the Ranger is plugged into. The number you get has to be converted before it is a distance:

Which port is the Ranger plugged into?
Official blockWhat it gives youConversion
raw redleft distance × 256÷ 256, then round down (the floor block) → left distance 0 ~ 120 cm
raw greenright distance × 256÷ 256, then round down → right distance 0 ~ 120 cm
raw blueaverage distance × 256÷ 256, then round down → average distance 0 ~ 120 cm

These three blocks are in the "More Sensors" extension: press the extension button at the bottom left of the App and add "More Sensors". To round down, use "floor of" from Operators. 120 = nothing in front.

3

Store it in a variable and use it

In "Variables", make a new variable (for example "Left distance" or "Right distance"). Use "set … to …" to store the converted value in it. That gives the value a name; after that, use the variable to show or check the value. Click a function and the blocks and result change to its example.

Sensor simulator (seen from above, front of the robot facing up): drag the left and right obstacle distances and watch the result on the right change

The simulator is only a picture: the distance comes straight from where the obstacle is on the screen. A real laser can be off by 1 to 2 cm and only updates about every 0.1 seconds. 120 = nothing in front (or farther than 120 cm).

Block examples for every function

Every function the official App can read, each with a set of blocks you can copy (port C). Press "Try it" to jump to step 3.

What the official App cannot read: the official App only reads the sensor's "combined data", so it cannot read the Ranger's laser OK, power-on status, fault status, millimetre distance, sensor ID, firmware version and sensor connected. To use them, switch to the MBC SPIKE Blocks website or Pybricks (both have all of them; the hub needs Pybricks firmware).

Good to know

  1. Verified on hardware (2026-11-03, SPIKE Prime Education, SPIKE App 3): the examples pass on the real hub; raw red, green and blue ÷ 256 are the left, right and average distance.
  2. Always convert. Raw red, green and blue all have to be ÷ 256 and then rounded down (floor). If you forget, the numbers are in the thousands.
  3. On Education, color = left distance and reflected light = right distance; tested, they are not clipped to 10 / 100 (nothing ahead = 120), so you can use them too. This page sticks to raw red, green and blue.
  4. Robot Inventor blocks convert differently: raw red, green and blue must be ÷ 64 (not ÷ 256) = left, right and average distance; reflected light = left distance (the opposite of Education), ambient light = left distance (max 100), and color always returns −1, so it cannot be used for distance.
  5. A reading of 120 can mean 4 things: there really is nothing in front, the object is farther than 120 cm, the laser is not ready yet right after power-on (about 1 to 2 seconds), or that laser is broken. Black, shiny and transparent objects are harder to measure; black objects usually cannot be seen beyond about 30 to 50 cm.
  6. Direction: left and right are as seen from behind the sensor, looking the way it faces (left = J5, right = J3). Raw red is the left laser and raw green is the right laser. Left minus right: negative = the left side is closer, positive = the right side is closer.