SharpShot Vision (ESP32CAM) × SPIKE × Official LEGO App
Interactive tutorialSPIKE
First time? Try the interactive tutorial in your browser first, then follow the steps below with your hub.
School editionLEGO SPIKE AppLEGO SPIKE App Python
WRO editionLEGO SPIKE App
This page covers Official LEGO App (SPIKE 3, Robot Inventor) only: how to set it up for the SharpShot Vision (ESP32CAM) 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: SharpShot Vision (ESP32CAM) (SPIKE edition). Each platform is a different factory edition; an EV3 edition is not recognised on a SPIKE hub.
- Hub: SPIKE Prime (Education, yellow hub) or Robot Inventor (home edition, teal hub).
- Cable: the standard cable in the box, or an original LEGO cable.
- Software: Education uses the LEGO Education SPIKE App (the examples on this site use SPIKE 3); the home edition uses the LEGO MINDSTORMS Robot Inventor App.
- Hub firmware: the original LEGO firmware. No need to switch to Pybricks.
Install the software
- Education: install the SPIKE App from the LEGO Education download page.
- Home edition: install the LEGO MINDSTORMS Robot Inventor App.
- If the hub has been flashed with Pybricks, switch it back to the original LEGO firmware with the SPIKE App first, or the official App cannot connect.
Note
The Education and home editions read values differently: in the home-edition App some blocks are limited to
0 ~ 126, and "Color" only goes up to 10. Use the readings for the edition you have.
Make the software see the sensor
No extension and no driver needed. Plug the sensor into the hub and the official App treats it as an original LEGO sensor (for most products a SPIKE Color Sensor); just use that sensor's blocks.
- An official block returns one number at a time, so the product data is encoded into the "Color", "Reflected light" and "Raw red / green / blue" slots. See the readings below for what each slot means.
- To open an example: download the
.llsp3(home edition:.lms) → open the App → top-left "File" → "Open" → pick the file.
Wiring
- Plug the sensor into hub port C with the cable (any of A~F works; choose the same port in your program).
- The plug only goes in one way: latch up, push it in gently, do not force it.
- When the hub is on, the light on the sensor comes on.
Set the colour on the sensor first
All SharpShot settings are made on the colour screen and buttons on the back of the sensor; no computer needed:
- The sensor only starts when plugged into a hub that is already on (the screen first shows "LEGO Sync..."); once the camera image appears it is ready.
- Short-press the side button to open the settings menu; left / right to choose, OK to enter. In the menu, hold the side button for 1 s to clear that colour set.
- Colour settings and camera parameters (brightness, contrast, saturation, exposure, white balance, flip, mirror) are saved automatically and used at the next power-on.
Note
- Official Education App blocks: "Color" = hue, "Reflected light" = motion value, "Raw red / green / blue" = blob X / Y / area.
- In the home edition (Robot Inventor) "Color" only goes 0~10 (a tenth of the Education edition); change the numbers in example 2 yourself.
- The numbers on this page are for current shipping firmware (hue 0~100). To check: after power-on look at the
Xnumber at the top of the sensor screen. If it only goes up to about 98, it is the new firmware; if it goes above 100 (up to 159), it is old firmware; contact us for an update first.
WRO edition (HUE12)
The official LEGO App has no blocks for HUE12 (HUE12 is not a LEGO sensor), so the official-App examples are Python programs for the Education SPIKE 3 App. For blocks, use the MBC Blocks website.
First test
- Connect the hub to the App over USB or Bluetooth.
- Open example 1 on this page and press Run.
- Move your hand or the mat in front of the sensor: if the number changes, everything is connected.
FAQ
- Nothing happens when I plug it in? Check in order: both ends of the cable firmly in → another hub port → another cable → another hub.
- Different Apps give different numbers? The Education (yellow hub) and home (teal hub) editions use different formats. Check which edition you have and use its readings.
- Do I need an adapter or a driver? No. The connector and protocol are the same as a LEGO hub port; it is recognised automatically.
- Example 6 won't open in the official App? Examples marked "Pybricks only" use a new mode in firmware from 2026-10 on, which the official App blocks cannot read.
Reading values (School edition)
The official app shows the sensor as a Color Sensor. Read it with the color, reflected light and raw red / green / blue blocks under More Sensors, then decode as shown.
hue hue
| Range | Meaning |
|---|---|
-1~100 |
hue of sample point S1, 0 |
| Hub | Value | Block (More Sensors → Color Sensor) | Decode |
|---|---|---|---|
| SPIKE Prime | hue | 「color」 | the number it returns |
| Robot Inventor | hue | 「color」 | the number it returns |
Firmware note: −1 = 0xFF; 0~10(÷10)
motion motion
| Range | Meaning |
|---|---|
0~100 |
whole-frame motion 0~100 (more change = larger) |
| Hub | Value | Block (More Sensors → Color Sensor) | Decode |
|---|---|---|---|
| SPIKE Prime | motion | 「reflected light」 | the number it returns |
| Robot Inventor | motion | 「reflected light」 | the number it returns |
blob X blobX
| Range | Meaning |
|---|---|
0~100 |
blob centre X 0~100, left = 0; 0 when not found |
| Hub | Value | Block (More Sensors → Color Sensor) | Decode |
|---|---|---|---|
| SPIKE Prime | blob X | 「raw red」 | the number mod 256 |
| Robot Inventor | blob X | 「raw red」 | the number it returns |
Firmware note: byte 3 = 0; ×4 查表編碼(10-bit)
blob Y blobY
| Range | Meaning |
|---|---|
0~75 |
blob centre Y 0~75, bottom = 0, top = 75; 0 when not found |
| Hub | Value | Block (More Sensors → Color Sensor) | Decode |
|---|---|---|---|
| SPIKE Prime | blob Y | 「raw green」 | the number mod 256 |
| Robot Inventor | blob Y | 「raw green」 | the number it returns |
Firmware note: byte 5 = 0; ×4 查表編碼
blob area blobArea
| Range | Meaning |
|---|---|
0~7500 |
W×H (0~7500); 0 when not found |
| Hub | Value | Block (More Sensors → Color Sensor) | Decode |
|---|---|---|---|
| SPIKE Prime | blob area | 「raw blue」 | the number mod 256 |
| Robot Inventor | blob area | 「raw blue」 | the number it returns |
Firmware note: 鎖住:這裡是 map(像素面積, 0~5000, 0~100),不是 W×H;byte 7 = 0; 同上 0~100 再 ×4 查表編碼
Not readable here:saturation, brightness, blob width, blob height, blob found. Use MBC Blocks website / Pybricks for these.
Reading values (WRO edition)
The official app shows the sensor as a Color Sensor. Read it with the color, reflected light and raw red / green / blue blocks under More Sensors, then decode as shown.
Not readable here:spot 1~12 colour code, spot 1~12 vividness. Use Pybricks for these.
Examples (School edition)
Example 1: What color do you see?
What this example does: The hub display keeps showing the "hue" number the SharpShot sees. Put bricks of different colors at the spot you chose in "Setup 1" and the number changes.
What you need
- 1 SPIKE hub
- 1 SharpShot (with "Setup 1: COLOR" done)
- One red, one green, one blue and one white brick
How to connect: SharpShot on port C.
Download the program
| Official LEGO App |
|---|
| ex01_show_hue.llsp3 |
🟨 Official LEGO App blocks

Step by step through the program
- Repeat forever: the blocks inside keep running until you press stop.
- Read the hue: the official App uses the "color" block and puts it into the variable "hue" (a variable = a box with a name that holds one number); the MBC Blocks website uses "SharpShot school edition C hue"; Pybricks uses
cam.hue(). - Show it on the display: the number scrolls across one character at a time. The MBC Blocks website and Pybricks also print it on the computer screen, which is quicker to read.
- Wait 0.5 seconds before reading again.
What you should see
- Red brick: about 0 to 5 or 95 to 100.
- Green brick: about 30; blue brick: about 65 (the lighting is different in every room, so the numbers vary a little).
- White paper, black brick: -1 (no color).
Common problems
- It reads 0 no matter what I put there? → "Setup 1: COLOR" has not been done yet.
- The number keeps jumping around? → Classroom lights flicker, and pale colors jump the most; use a brighter, more saturated brick, or move it closer.
- The number changed after moving to another room? → Different lighting; do "Setup 1" again.
Example 2: Beep when you see red
What this example does: When the SharpShot sees red, the light in the middle of the hub turns red and the hub beeps; for any other color the light turns off.
What you need: Same as example 1.
How to connect: SharpShot on port C.
Download the program
| Official LEGO App |
|---|
| ex02_red_beep.llsp3 |
🟨 Official LEGO App blocks

Step by step through the program
- Read the hue into a variable.
- If hue is between 0 and 5, or hue ≥ 95 (the official App writes it as "> 94", same meaning) → red light, beep. Red sits at both the start and the end of the hue scale, so you have to check both ends.
- Else (any other color, or -1 for no color) → light off.
- Wait 0.1 seconds and check again.
What you should see: Put the red brick at the setup spot → red light + continuous beeping; swap in green or white paper → light off, no sound.
Common problems
- It beeps all the time even with nothing there? → Hue is 0, which means "Setup 1: COLOR" has not been done (hue is 0 before setup, same as red).
- Orange beeps too? → Orange hue is very close to red (about 4 to 10); make the "5" smaller, for example 3.
- My red brick reads 7 and does not beep? → Use example 1 to see what your red reads, then change the "5" to that number plus a little.
- How do I pick the light color in the official App? → Click the color dot on the "set Power Button light to" block.
Example 3: Which side is the blob on?
What this example does: The hub display uses an arrow to tell you where the blob is in the picture: on the left → ⬅, on the right → ➡, in the middle → ⬆, not found → ✕.
What you need: Same as example 1; the sensor must have "Setup 2: TRACK" done first.
How to connect: SharpShot on port C.
Download the program
| Official LEGO App |
|---|
| ex03_blob_arrow.llsp3 |
🟨 Official LEGO App blocks

Step by step through the program
- Read blob X, then read "was it found": the official App checks "is raw blue (area) 0"; the MBC Blocks website checks "blob width = 0"; Pybricks uses
cam.found(). - If not found → show ✕ (X is 0 at this point and cannot be used for the decision, so this check comes first).
- Else, if X < 35 → left arrow.
- Else, if X > 65 → right arrow.
- Otherwise (35 to 65) → up arrow, meaning it is in the middle.
What you should see: Hold the colored block in front of the lens and move it slowly from left to right; the arrow goes ⬅ → ⬆ → ➡. Take the block away and it becomes ✕.
Common problems
- Left and right arrows are swapped? → The camera is mounted the opposite way from the picture. In the camera menu toggle
H-Mirroronce (then checkV-Flip, see the warning above). - An arrow shows even when the blob is gone? → Something of the same color in the background is being tracked: go back to "Setup 2", lower
TOL_HUEand raiseMIN AREA. - Why is the middle 35 to 65 and not just 50? → Leaving a band that "counts as the middle" keeps the arrow from flickering.
Example 4: Turn to follow the blob
What this example does: The robot turns in place to keep the blob in the center of the camera picture. Blob on the left → turn left; on the right → turn right; lined up (or nothing in view) → stop.
What you need
- Everything from example 3, plus
- A two-wheel car (two motors as wheels) with the SharpShot mounted at the front, lens facing forward
- A colored block that you have done "Setup 2: TRACK" on
How to connect
- SharpShot on port C
- Left wheel motor on E, right wheel motor on F
Download the program
| Official LEGO App |
|---|
| ex04_turn_to_blob.llsp3 |
🟨 Official LEGO App blocks

Step by step through the program
- Tell the hub where the wheels are: the official App uses "set movement motors to E+F"; the MBC Blocks website and Pybricks use "drive base: left wheel E, right wheel F".
- Set the speed: 20% in the official App; the other two use "60 degrees per second". Turning too fast blurs the camera picture, so turn slowly.
- If no blob found → stop.
- Else if X < 40 (blob on the left) → turn left in place (official App: steer -100).
- Else if X > 60 (blob on the right) → turn right in place (official App: steer 100).
- Else (40 to 60, lined up) → stop.
What you should see: Hold the block in front of the car and walk slowly to the left; the car turns to follow. Stop, and the car stops facing the block.
Common problems
- Blob on the left but the car turns right and keeps turning away? → Left and right are swapped: flip the sign of both turn numbers (official App: -100 and 100; the other two: -60 and 60), or toggle
H-Mirroras described in example 3. - It keeps wobbling left and right? → Turning too fast. Lower the speed in the official App (for example 15%); in the other two change 60 to 40. You can also widen the "middle" band (change 40/60 to 35/65).
- The car turns in place in a strange way? → 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 switch the "forward" direction of both wheels to the other option; Pybricks: change the left wheel to
Direction.CLOCKWISEand the right wheel toMotor(Port.F, Direction.COUNTERCLOCKWISE).
Example 5: Motion-detection alarm
What this example does: Use the hub as a "security guard" that stays still. When something moves in front of the camera (someone walks past, a hand waves), the hub turns on the red light, beeps, and shows on the display how many times it has happened; when nothing moves, the light is green.
What you need: SPIKE hub, SharpShot (no color setup needed for this example).
How to connect: SharpShot on port C, camera pointed at a doorway or a desk, the whole thing sitting still and steady.
Download the program
| Official LEGO App |
|---|
| ex05_motion_alarm.llsp3 |
🟨 Official LEGO App blocks

Step by step through the program
- Set "count" to 0 at the start (a variable that remembers how many times the alarm has gone off).
- Read the motion value: the official App uses "reflected light"; the MBC Blocks website uses "SharpShot school edition C motion value"; Pybricks uses
cam.motion(). - If motion value > 20 → red light, beep, add 1 to count, show count on the display, then wait 1 second (so one movement is not counted several times).
- Else → green light.
- Wait 0.1 seconds and check again.
What you should see: Wave a hand in front of the camera → red light + beep + display shows 1; wave again → 2… Keep your hands still and the light stays green.
Common problems
- It keeps going off with nobody moving? → "20" is too small. First print
cam.motion()in Pybricks, or watch theMnumber on the sensor screen, to see what it reads when nothing is moving, then set the threshold a bit above that. Flickering classroom lights and trees moving outside the window both push the number up. - Waving does nothing? → The threshold is too high; make it smaller (for example 10), or move your hand closer to the lens.
- Can I put it on a moving car? → No. When the car itself is moving, the whole picture keeps changing and the motion value stays high. The motion value is only meaningful when the robot is standing still.
Note
These examples have no version for this software: Example 6 (advanced): Blob area: stop when close
Examples (WRO edition)
Example 1: Read the hue at point 1
What this example does: The hub display keeps showing the hue seen at point 1; the computer screen also prints the hue and saturation.
What you need
- 1 SPIKE hub
- 1 HUE12 (with point 1 set)
- One red, one green, one blue, one white and one black brick
How to connect: HUE12 on port C.
Download the program
| Official LEGO App (Python) |
|---|
| ex01_show_slot1.llsp3 |
🟨 Official LEGO App Python program
# ===== 讀 HUE12 的小工具(跟官方範例 Hue12_demo_For_Spike_v3.4.3 的讀法一樣,照抄就好、不用改)=====
import device
import utime
from hub import port
class HUE12:
def __init__(self, port_number):
self.port = port_number
self.slots = [(255, 0)] * 12 # 12 格的(色相, 飽和度);255 = 這一格沒設定
def update(self):
"""讀一次最新資料;讀不到回傳 False"""
raw = device.data(self.port) # 感應器送來 8 個整數
if not raw or len(raw) < 8:
return False
b = []
for val in raw[:8]:
if val < 0:
val += 2 ** 32
b += [val & 0xFF, (val >> 8) & 0xFF, (val >> 16) & 0xFF] # 每個整數拆成 3 個數字
for i in range(12):
self.slots[i] = (b[i * 2], b[i * 2 + 1])
return True
def hue(self, slot):
"""第 slot 格(1~12)的色相:0~239 彩色、240~245 白、246~250 黑、255 沒設定"""
return self.slots[slot - 1][0]
def sat(self, slot):
"""第 slot 格的飽和度 0~255(越大顏色越鮮豔)"""
return self.slots[slot - 1][1]
# ===== 小工具到這裡結束,下面才是範例 =====
from hub import light_matrix
cam = HUE12(port.C) # HUE12 插在 C 孔
while True:
cam.update() # 讀一次最新資料
hue = cam.hue(1) # 第 1 格的色相
print(hue, cam.sat(1)) # 電腦畫面印出 色相、飽和度
light_matrix.write(str(hue)) # 主機螢幕顯示色相
utime.sleep_ms(500) # 等 0.5 秒Step by step through the program
- Read the data: the official App and Pybricks call
cam.update()first (reads the latest values of all 12 points once), then take point 1; the MBC Blocks website uses the "SharpShot HUE12 C point 1 hue" block directly. - Show it: the hub display scrolls the hue one character at a time, and the computer screen prints it too.
- Wait 0.5 seconds before reading again.
- The big block at the top of the official App program is the "HUE12 reader helper" (the official App cannot import other files, so every program has to carry it). Copy it as is; nothing to change.
What you should see: Red brick 0 to 9 or 230 to 239; green about 80; white paper 240 to 245; black 246 to 250; 255 if the point is not set.
Common problems
- Always 255? → Point 1 is not set, or the sensor is still in the menu (go back to
RUN MODE). - Pybricks always shows 255 and the number after
Connected to ID:is not 95? → The hub's Pybricks firmware is too old; update to v4.0.0b5 or newer.
Example 2: Is point 1 red?
What this example does: Point 1 sees red → the light in the middle of the hub turns red and the hub beeps; any other color → light off.
What you need: Same as example 1. How to connect: HUE12 on port C.
Download the program
| Official LEGO App (Python) |
|---|
| ex02_is_red.llsp3 |
🟨 Official LEGO App Python program
# ===== 讀 HUE12 的小工具(跟官方範例 Hue12_demo_For_Spike_v3.4.3 的讀法一樣,照抄就好、不用改)=====
import device
import utime
from hub import port
class HUE12:
def __init__(self, port_number):
self.port = port_number
self.slots = [(255, 0)] * 12 # 12 格的(色相, 飽和度);255 = 這一格沒設定
def update(self):
"""讀一次最新資料;讀不到回傳 False"""
raw = device.data(self.port) # 感應器送來 8 個整數
if not raw or len(raw) < 8:
return False
b = []
for val in raw[:8]:
if val < 0:
val += 2 ** 32
b += [val & 0xFF, (val >> 8) & 0xFF, (val >> 16) & 0xFF] # 每個整數拆成 3 個數字
for i in range(12):
self.slots[i] = (b[i * 2], b[i * 2 + 1])
return True
def hue(self, slot):
"""第 slot 格(1~12)的色相:0~239 彩色、240~245 白、246~250 黑、255 沒設定"""
return self.slots[slot - 1][0]
def sat(self, slot):
"""第 slot 格的飽和度 0~255(越大顏色越鮮豔)"""
return self.slots[slot - 1][1]
# ===== 小工具到這裡結束,下面才是範例 =====
import color
from hub import light, sound
cam = HUE12(port.C)
while True:
cam.update()
hue = cam.hue(1)
if hue < 10 or (hue >= 230 and hue < 240): # 紅色在色相的兩端:0~9 和 230~239
light.color(light.POWER, color.RED) # 中間按鈕燈變紅
sound.beep(880, 100, 100) # 嗶一聲(880 Hz、0.1 秒、音量 100)
else:
light.color(light.POWER, color.BLACK) # 黑色 = 關燈
utime.sleep_ms(200)Step by step through the program
- Read the hue at point 1.
- If hue < 10, or hue is between 230 and 239 → red: red light, beep.
- Else → light off.
- Wait 0.2 seconds and check again.
What you should see: Put the red brick at point 1 → red light + beep; swap in another color → light off.
Common problems
- Orange beeps too? → Orange is about 10 to 30; make the "10" smaller (for example 6).
- Is there a simpler way in Pybricks? → Yes:
if name == "Red":; the library does the check for you (same range as above).
Example 3: How many of the 12 points see a color?
What this example does: Checks the 12 points one by one, counts how many see a color (not white, black or unset), and shows the count on the hub display.
What you need: Same as example 1, with several points set (for example points 1 to 4 in a row), plus a few colored bricks.
How to connect: HUE12 on port C.
Download the program
| Official LEGO App (Python) |
|---|
| ex03_count_colors.llsp3 |
🟨 Official LEGO App Python program
# ===== 讀 HUE12 的小工具(跟官方範例 Hue12_demo_For_Spike_v3.4.3 的讀法一樣,照抄就好、不用改)=====
import device
import utime
from hub import port
class HUE12:
def __init__(self, port_number):
self.port = port_number
self.slots = [(255, 0)] * 12 # 12 格的(色相, 飽和度);255 = 這一格沒設定
def update(self):
"""讀一次最新資料;讀不到回傳 False"""
raw = device.data(self.port) # 感應器送來 8 個整數
if not raw or len(raw) < 8:
return False
b = []
for val in raw[:8]:
if val < 0:
val += 2 ** 32
b += [val & 0xFF, (val >> 8) & 0xFF, (val >> 16) & 0xFF] # 每個整數拆成 3 個數字
for i in range(12):
self.slots[i] = (b[i * 2], b[i * 2 + 1])
return True
def hue(self, slot):
"""第 slot 格(1~12)的色相:0~239 彩色、240~245 白、246~250 黑、255 沒設定"""
return self.slots[slot - 1][0]
def sat(self, slot):
"""第 slot 格的飽和度 0~255(越大顏色越鮮豔)"""
return self.slots[slot - 1][1]
# ===== 小工具到這裡結束,下面才是範例 =====
from hub import light_matrix
cam = HUE12(port.C)
while True:
cam.update()
count = 0 # 先歸零
for slot in range(1, 13): # 第 1 格到第 12 格,一格一格檢查
if cam.hue(slot) < 240: # 小於 240 = 彩色(240 以上是白、黑、沒設定)
count = count + 1
print(count)
light_matrix.write(str(count)) # 主機螢幕顯示有幾格是彩色
utime.sleep_ms(500)Step by step through the program
- Set "color count" to 0 first.
- Repeat 12 times, checking one point each time: the official App and Pybricks use
for slot in range(1, 13)(slot goes 1, 2, … 12 in turn); the MBC Blocks website uses a variable "point number" that starts at 1 and adds 1 after each pass. - If this point's hue < 240 (a color) → add 1 to color count.
- After all 12 points → show color count on the display.
What you should see: 2 colored bricks on 4 points → shows 2; add one more → 3. White paper and black bricks do not count.
Common problems
- The count is higher than the number of bricks I placed? → Some points are seeing a colored background (for example a colored line on the field); clear those points (hold the side button for 1 second in the menu).
- Why 240 as the cutoff? → 240 and above is white (240 to 245), black (246 to 250) and not set (255), none of which is a color.
Example 4: Turn by color
What this example does: Point 1 sees red → turn right in place; sees green → turn left in place; anything else → stop.
What you need
- Everything from example 1, plus a two-wheel car (two motors as wheels) with the HUE12 mounted at the front, lens facing forward
- A red object and a green object
How to connect: HUE12 on port C; left wheel motor on E, right wheel motor on F.
Download the program
| Official LEGO App (Python) |
|---|
| ex04_turn_by_color.llsp3 |
🟨 Official LEGO App Python program
# ===== 讀 HUE12 的小工具(跟官方範例 Hue12_demo_For_Spike_v3.4.3 的讀法一樣,照抄就好、不用改)=====
import device
import utime
from hub import port
class HUE12:
def __init__(self, port_number):
self.port = port_number
self.slots = [(255, 0)] * 12 # 12 格的(色相, 飽和度);255 = 這一格沒設定
def update(self):
"""讀一次最新資料;讀不到回傳 False"""
raw = device.data(self.port) # 感應器送來 8 個整數
if not raw or len(raw) < 8:
return False
b = []
for val in raw[:8]:
if val < 0:
val += 2 ** 32
b += [val & 0xFF, (val >> 8) & 0xFF, (val >> 16) & 0xFF] # 每個整數拆成 3 個數字
for i in range(12):
self.slots[i] = (b[i * 2], b[i * 2 + 1])
return True
def hue(self, slot):
"""第 slot 格(1~12)的色相:0~239 彩色、240~245 白、246~250 黑、255 沒設定"""
return self.slots[slot - 1][0]
def sat(self, slot):
"""第 slot 格的飽和度 0~255(越大顏色越鮮豔)"""
return self.slots[slot - 1][1]
# ===== 小工具到這裡結束,下面才是範例 =====
import motor_pair
cam = HUE12(port.C)
motor_pair.pair(motor_pair.PAIR_1, port.E, port.F) # 左輪 E、右輪 F 配成一組
while True:
cam.update()
hue = cam.hue(1)
if hue < 10 or (hue >= 230 and hue < 240): # 紅色 → 原地右轉
motor_pair.move(motor_pair.PAIR_1, 100, velocity=200)
elif hue >= 60 and hue <= 110: # 綠色 → 原地左轉
motor_pair.move(motor_pair.PAIR_1, -100, velocity=200)
else: # 其他 → 停
motor_pair.stop(motor_pair.PAIR_1)
utime.sleep_ms(50)Step by step through the program
- Tell the hub where the wheels are: the official App uses
motor_pair.pair(…, port.E, port.F); the MBC Blocks website and Pybricks use "drive base: left wheel E, right wheel F". - Read the hue at point 1.
- If red → turn right in place (official App: steering 100, wheels at 200 degrees per second; the other two: the car body turns at 60 degrees per second).
- Else if green (hue 60 to 110) → turn left in place.
- Else → stop.
What you should see: Put the red object in front of the car → the car turns right until it no longer sees red, then stops; green object → turns left.
Common problems
- No reaction to green? → Use example 1 to see what your green reads, and change "60 to 110" to a range that includes it.
- It turns the wrong way? → The motors are mounted the opposite way from the standard LEGO car. Official App: swap 100 and -100; MBC Blocks website: in the "drive base" block switch the "forward" direction of both wheels to the other option; Pybricks: change the left wheel to
Direction.CLOCKWISEand the right wheel toMotor(Port.F, Direction.COUNTERCLOCKWISE).
Example 5: Look at the object's color and decide which side to go around (a common WRO pattern)
What this example does: The car drives forward slowly. A red object ahead → go around it on the right; a green object → go around it on the left; then keep going forward. Many WRO tasks use "look at the color to decide which side to take" (for example the red/green traffic-sign posts in the Future Engineers category); this is the most basic way to write it.
What you need: Same as example 4, with a few red and green objects on the field (at least 40 cm apart).
How to connect: HUE12 on port C (point 1 set straight ahead of the car, about 15 cm away, at floor height); left wheel motor on E, right wheel motor on F.
Download the program
| Official LEGO App (Python) |
|---|
| ex05_wro_bypass.llsp3 |
🟨 Official LEGO App Python program
# ===== 讀 HUE12 的小工具(跟官方範例 Hue12_demo_For_Spike_v3.4.3 的讀法一樣,照抄就好、不用改)=====
import device
import utime
from hub import port
class HUE12:
def __init__(self, port_number):
self.port = port_number
self.slots = [(255, 0)] * 12 # 12 格的(色相, 飽和度);255 = 這一格沒設定
def update(self):
"""讀一次最新資料;讀不到回傳 False"""
raw = device.data(self.port) # 感應器送來 8 個整數
if not raw or len(raw) < 8:
return False
b = []
for val in raw[:8]:
if val < 0:
val += 2 ** 32
b += [val & 0xFF, (val >> 8) & 0xFF, (val >> 16) & 0xFF] # 每個整數拆成 3 個數字
for i in range(12):
self.slots[i] = (b[i * 2], b[i * 2 + 1])
return True
def hue(self, slot):
"""第 slot 格(1~12)的色相:0~239 彩色、240~245 白、246~250 黑、255 沒設定"""
return self.slots[slot - 1][0]
def sat(self, slot):
"""第 slot 格的飽和度 0~255(越大顏色越鮮豔)"""
return self.slots[slot - 1][1]
# ===== 小工具到這裡結束,下面才是範例 =====
import motor_pair
from hub import light_matrix
cam = HUE12(port.C)
motor_pair.pair(motor_pair.PAIR_1, port.E, port.F)
def go(steering, ms):
"""用 steering(-100 左轉 ~ 0 直走 ~ 100 右轉)走 ms 毫秒,然後停"""
motor_pair.move(motor_pair.PAIR_1, steering, velocity=200)
utime.sleep_ms(ms)
motor_pair.stop(motor_pair.PAIR_1)
while True:
cam.update()
hue = cam.hue(1)
if hue < 10 or (hue >= 230 and hue < 240): # 紅色物塊 → 從右邊繞過
light_matrix.write("R")
go(100, 450) # 右轉約 45 度
go(0, 1500) # 直走約 15 公分
go(-100, 450) # 左轉回來
elif hue >= 60 and hue <= 110: # 綠色物塊 → 從左邊繞過
light_matrix.write("G")
go(-100, 450)
go(0, 1500)
go(100, 450)
else: # 沒看到物塊 → 慢慢往前
motor_pair.move(motor_pair.PAIR_1, 0, velocity=150)
utime.sleep_ms(20)Step by step through the program
- Read the hue at point 1.
- If red → show R on the display → turn right 45 degrees → drive straight 15 cm → turn left 45 degrees (the car faces its original direction again).
- Else if green → show G on the display → do the same with left and right swapped.
- Else (no object in view) → drive straight slowly.
- The MBC Blocks website and Pybricks use "turn N degrees" and "drive N mm" and work it out for you; the official App version here uses "drive for N milliseconds" (
go(100, 450)= turn right for 0.45 seconds), so you have to tune the angle by trial.
What you should see: The car drives forward, swerves right around a red object, swerves left around a green object, and keeps going once it is past.
Common problems
- It turns back before it is past the object and hits it? → Make the straight distance longer (150 → 200; official App 1500 → 2000).
- The official App turns the wrong angle? → Change the 450 in
go(100, 450)andgo(-100, 450): a bigger number turns farther. Battery level also makes a difference, so for competitions we recommend Pybricks or the MBC Blocks website. - It keeps going around the same object? → After going around, the camera sees the same object again. Set point 1 lower (closer to the front of the car), or make the turn angle bigger.
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
Sources
- Host and software list:
_build/guides.json - Values and commands:
_build/protocol/cam_school.json、_build/protocol/cam_wro.json(single source of truth) - Setup:
_build/guides/software/spike-official.en.md、_build/guides/product/esp32cam/spike-official.en.md - Examples:
sensors/esp32cam/spike-examples(-wro).md