Read the Ranger 8-Way in the LEGO SPIKE App in three steps
The LEGO SPIKE App sees the Ranger 8-Way as a Color Sensor. So you do not import a library: you read it with the Color Sensor blocks and then split the numbers into the distances of sensors 1–8. Follow the steps on the left and the blocks on the right grow with you. These block pictures were built and exported straight from the official web app (spike.legoeducation.com), so they look just like your app.
1
Start: when program starts + forever
The sensor has to be read again and again. Start with the yellow "when program starts", attach "forever" under it, and put "wait 0.1 seconds" inside, so it reads every 0.1 s.
Open the LEGO SPIKE App, start a new project and choose "Word Blocks".
From "Events", drag out "when program starts"; from "Control", drag "forever" and attach it underneath.
Drag a "wait 1 seconds" block into forever and change the number to 0.1.
2
Meet the five reading blocks and pick the right port
The official app reads the Ranger 8-Way with five Color Sensor blocks. Each block starts with a port drop-down; pick the port the Ranger is plugged into. The 8 distances (cm) are spread over these five blocks, and the last three blocks hold two sensors each, so you have to split them:
Which port is the Ranger plugged into?
Official block
What it reads
How to split it
color (in Sensors)
sensor 1 (straight left)
use it as it is
reflected light (in Sensors)
sensor 2 (front-left 60°)
use it as it is
raw red (in the "More Sensors" extension)
sensor 3 + sensor 4 × 256
the remainder after dividing by 256 = sensor 3; ÷ 256 rounded down = sensor 4
raw green
sensor 5 + sensor 6 × 256
remainder = sensor 5; rounded down = sensor 6
raw blue
sensor 7 + sensor 8 × 256
remainder = sensor 7; rounded down = sensor 8
To use raw red / green / blue, first click the extension button at the bottom left of the app and add "More Sensors". For the "remainder" use "… mod …" in Operators; for "rounded down" wrap "… / 256" in "floor of …".
3
Store it in a variable and use it
In "Variables", make a new variable (for example "sensor 4") and store the split value in it with "set … to …". This gives the value a name; after that, use the variable to show it or to make decisions. Click a function and the blocks and result change to its example.
Simulated sensor (seen from above, robot facing up): drag the obstacle and watch the result on the right
The simulation is only a sketch: direction and distance come from a simple position-based estimate, so a real sensor may differ by a step or two. Red sector = that sensor sees something; grey sector = it reads 120 (nothing that way). Sensors 4 and 5 both face straight ahead.
A block example for every function
Every function the official app can read, each with blocks you can copy (port C). Click "Try it" to jump to step 3.
What the official app cannot read: the official app only reads the sensor's "combined data" (the 8 distances in cm). So for the Ranger's nearest direction / distance and most open direction you compare the sensors one by one with blocks, as in the examples above. Each sensor OK / faulty, distances in mm, sensor ID, firmware version and sensor connected cannot be read at all. To use these directly, switch to the MBC SPIKE Blocks website or Pybricks (the hub needs Pybricks firmware).
Good to know
Use the "Color Sensor" set of sensor blocks in the official app. To the official app, the Ranger 8-Way is just a Color Sensor; add the "More Sensors" extension first for raw red / green / blue. This page is for the Education edition (SPIKE 3) only; the home edition (Robot Inventor) reads a different format.
Always split. Raw red / green / blue each hold two sensors: the remainder is the first one, the rounded-down part is the second one. If you forget to split, the numbers go into the thousands.
The official app readings have not been checked one by one on a real hub yet. Especially sensor 1 (color block) and sensor 2 (reflected light block): the app may only accept numbers 0–10 and 0–100. If the numbers look strange wherever you put your hand, switch to the MBC Blocks website or Pybricks.
120 = nothing that way (or more than 120 cm away, just switched on and not ready for 2–4 s, or that sensor is faulty). Black, shiny or see-through objects are harder to measure, so keep distance checks within 60 cm.
Direction: stand behind the robot and look forward: left is negative, right is positive. Sensor 1 points straight left, sensors 4 and 5 point straight ahead, sensor 8 points straight right. To work out a direction with blocks: direction = sensor number × 2 − 9 (sensor 1 −7, sensor 4 −1, sensor 5 +1, sensor 8 +7).