MBC SharpShot Vision × Pybricks blocks

Read SharpShot Vision with Pybricks blocks in three steps

Blocks follow the same order as MicroPython: under "set up" import the library and pick the camera mode, then create the object and name the camera, then under "program" call its functions with that name. Work down the steps on the left and watch the blocks grow on the right; the block pictures are exported from Pybricks Code itself, exactly as you see them there.

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

Import the library, pick the camera mode

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

The first thing in a block program is a red "from … import …" block under the yellow "set up" block: it tells Pybricks to use the SharpShot Vision library.

  1. Download the library (save the file and keep its name): MBC_UniCam.py. The file name is MBC_UniCam.py.
  2. In Pybricks Code press "Import a file" in the file list and import it into the same project; keep the file name.
  3. Press "+" to add a file and choose blocks.
  4. Drag the red "from … import …" block under "set up" and pick MBC_UniCam and UniCam.
  5. 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); step 2 puts it in the block.

Rather not import a library? Pybricks' own color sensor blocks (reflection, ambient) read part of it (1–2 colors or spots per mode); see the "Pybricks blocks" column of the value reference.

2

Create the object: name the camera

Drag a red "… is a UniCam" block under the import block: on the left is the name you give the camera; the three slots are port number, mode number and multitasking. Every reading then uses this name.

Which port is the camera on?

Name it (any English name works in Pybricks; two examples here):

Use multitasking? (the green true/false in the last slot)

The port slot takes a number: SPIKE 1–6 = A–F, so 3 is port C. The mode slot must match the camera menu (SPIKE cannot read the camera's mode, so a mismatch gives nonsense numbers).

3

Use a function

Put a red function block under "program": pick the name cam first, call in the middle, then the function. Functions are grouped by camera mode; dashed ones belong to another mode (clicking one switches it for you). Click a function and the blocks and output switch to its example.

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, multitask false, port C); the badge shows which modes it works in. "Try it" jumps to step 3 (and switches the mode if needed).

Full examples

Whole programs (turn toward a blob, line following) are in the MicroPython tutorial and on the SPIKE examples page; in blocks, build the same program piece by piece following the MicroPython view on the right.

Good to know

  1. Import the library file first. Without MBC_UniCam.py in the project, the red blocks do not offer UniCam.
  2. await or call must match multitasking: true needs await, false needs call. Mixing them makes the program fail on the hub.
  3. The mode number must match the camera menu. A function the mode does not have raises ValueError (the message says which mode to use).
  4. Unplugging does not stop the program. Readings keep the last value (or None if never read) and one warning line is printed; use sensor_ok to check.
  5. Directions: X 0–159 left→right, Y 0–119 top→bottom (top = small); line offset: left = negative, right = positive (looking forward from behind the robot). Colors are taught on the camera; the program cannot change them.
  6. Blocks are MicroPython underneath. The code pane in Pybricks Code is what the blocks generate, the same as the MicroPython view here (the multitask loop starts with await wait(0), added by Pybricks). Examples that need a for loop, like "find the red triangle", are in the MicroPython tutorial.