MBC SharpShot Vision × Pybricks built-in blocks

Read SharpShot Vision with Pybricks built-in blocks, no library

On a SPIKE hub SharpShot Vision shows up as a LEGO color sensor, so Pybricks' own color sensor blocks can read it: two numbers, reflection() and ambient(). Each camera mode packs different things into them; split them with math blocks and use them. Work down the steps on the left and watch the blocks grow on the right; the block pictures are exported from Pybricks Code itself.

How this differs from Pybricks blocks (with the library)
  • This page: nothing to download or import, just open Pybricks; but each camera mode gives only 2 numbers (positions on a 40 × 30 grid), and you split them yourself with // and %.
  • With the library: import MBC_UniCam.py first, but you get many values at once (e.g. 14 colors, pixel positions, areas, object shapes), one block per function.

Verified on SPIKE hardwareRun on real hubs on 2026-11-01 with shipping firmware v1.0-261101 (Pybricks library and blocks, Pybricks built-in blocks, the official app Education and home edition, MBC Blocks website).

1

set up: create the color sensor, pick the port and mode

Check first: Pybricks block coding needs a license code or a Patreon membership. Without one, write the MicroPython directly; it is exactly the same (switch the right panel to "MicroPython").

No library: under the yellow "set up" put Pybricks' own color sensor block (ColorSensor), pick the port the camera is on, and name it cam.

Which port is the camera on?
  1. In Pybricks Code press "+" to add a file and choose blocks.
  2. From "Sensors" drag the color sensor setup block (sensor icon) under "set up", rename it cam (any English name works) and pick the port above.
  3. Pick the camera mode: choose the camera mode and sub-option in the camera's MODE menu and click the same mode number below (mode number = camera mode × 10 + sub-option). The program has no mode setting, but how you split the numbers depends on it.
2

program: read reflection() and ambient()

Under "program" put "repeat while true", print cam reflected light (reflection) and cam ambient light (ambient) inside, then "wait 100 ms". Reading reflection switches the sensor to SPIKE mode 5 and ambient to mode 7 automatically; the camera puts one set of numbers in each. The table shows what the two numbers hold in the mode you picked in step 1 (it changes with the mode):

The simulator reads now:

reflection() is a whole number; above 3199 it turns negative (how Pybricks computes it), and adding 6399 fixes it. ambient() is a decimal A.B (one decimal: Pybricks 4.1 keeps only one, and 3.x is the same here): the part before the point is the first value, the first decimal the second. Full layouts per mode are in the "Pybricks blocks" column of the value reference.

3

Split with math blocks and use it

Store the reading in a variable with "set to" from Variables: r = reflection (add 6399 if negative), a = ambient. Split reflection into two values with // (whole part of ÷) and % (remainder) from Math; for ambient use round down for the whole part and round(a × 10) % 10 for the first decimal. Print or test them. Functions are grouped by camera mode; dashed ones belong to another mode (clicking one switches it for you).

Simulated camera view: drag things in the picture and watch the output change

The view is a sketch: values come straight from where things are on screen (X 0–159 left→right, Y 0–119 top→bottom, area % = width × height ÷ whole picture); a hue code is the color the paper or block really has (red 0, yellow 40, green 80, blue 160, purple 200, white table 244, black tape 252). It is not the camera's own algorithm, and real numbers vary with the light. Color tracking assumes 4 colors were taught on the camera: color 1 red, 2 green, 3 blue, 4 yellow. In object detection you pick each shape yourself (the camera does not judge it here); objects 1, 2… are sorted by area, largest first; X, Y is the shape's centroid (white dot), the dashed box is its bounding box, and the angle is the rotation you set.

An example for every function

Every function has a set of blocks to copy (name cam, port C); the badge shows which modes it works in. "Try it" jumps to step 3 (and switches the mode if needed).

Full examples

For more values (14 colors, pixel positions, areas, object shapes, mid line offset…) use Pybricks blocks with the library or MicroPython; whole programs (turn toward a blob, line following) are on the SPIKE examples page.

Good to know

  1. Only 2 numbers per mode. reflection() and ambient() hold 1–2 values each; everything else (e.g. colors 3 and up, pixel positions, areas) is out of reach and listed under the step 2 table. For those use Pybricks blocks with the library.
  2. reflection() can turn negative: when X is over 31 (of 40 cells) the value passes 3199 and Pybricks shows it as negative. Always "if r < 0, add 6399" before splitting (the Kito example does the same).
  3. ambient() has one decimal: from Pybricks 4.1 ambient() keeps only the first decimal, so every mode packs ambient as "whole number . first decimal". × 10 on the decimal is inexact (4.3 × 10 may be 42.9999), so round it before % 10.
  4. The mode must match the camera menu. Pybricks cannot read the camera's mode and the program has no place for it; with the wrong mode the split numbers mean nothing.
  5. Do not use the "color" block or color names. The camera sends numbers: store them in variables and compare numbers. Unplugging the sensor makes the built-in blocks raise an error and stop the program.
  6. Directions: X left→right, Y top→bottom (top = small), 4 pixels per cell; line offset: left = negative, right = positive (looking forward from behind the robot). Colors are taught on the camera.