MBC SharpShot Vision × Pybricks (SPIKE)

Read SharpShot Vision with MicroPython in three steps

Programs always go in this order: first import the library and pick the camera mode, then create an object and name the camera, then call its functions with that name. Work down the steps on the left and watch the program grow on the right; drag the blobs, papers, blocks or line in the simulated camera view to see what the program prints.

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

The first thing in the program tells the hub "I am using the SharpShot Vision library". Without it the hub does not know UniCam and stops with NameError.

  1. Download the library (save the file, keep its name): MBC_UniCam.py.
  2. In Pybricks Code press "Import a file" in the file list and import it into the same project (not a sub-folder).
  3. Plug the camera into any port A–F; the hub powers it. Green hub name on the camera screen = connected.
  4. 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).
2

Create the object: name the camera

A robot can have several sensors, so you "create an object": tell the program which port the camera is on and which mode it is in, and give it a name. You use that name for every reading.

Which port is the camera on?

Use multitasking?

The number after mode= is the mode number from step 1; switch the camera menu to the same mode (SPIKE cannot read the camera's mode, so a mismatch gives nonsense numbers).

3

Use a function

Always write "cam.function()": name + dot + function. Functions are grouped by camera mode; dashed ones belong to another mode (clicking one switches the mode for you). Click a function and the program 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 an example you can drop into the loop; the badge shows which modes it works in. "Try it" jumps to step 3 (and switches the mode if needed).

Full examples

Press "Copy" for the whole program. More examples (multi-color, spot color, color order, stop at junctions) are on the SPIKE examples page.

Turn toward a blob (mode 13, left wheel E, right wheel F)

        

!Teach "target 1" on the camera first and set the camera menu to mode 13. Turns the wrong way? Make KP negative.

Line following (mode 41 black line)

        

!Point the camera down and forward. Left = negative, so "line on the left → turn left" is turn = KP * e. No line: spin toward the side it was last seen.

Good to know

  1. Unplugging does not stop the program. Readings keep the last value; if nothing was ever read they are None, and one warning line is printed. It recovers when plugged back in. Use sensor_ok() to know. Tick "Unplug the sensor" to see it.
  2. The mode number must match the camera menu. SPIKE cannot read the camera's mode, so the library decodes using mode=. A function the mode does not have raises ValueError (the message says which mode to use).
  3. Check that something is seen first. (0, 0) or area 0 = not seen. Red's hue code is 0, so in color order read total() first to know how many entries are real.
  4. Directions: X 0–159 left→right, Y 0–119 top→bottom (top = small); the middle is X = 80. Mode 11 gives 16×12 grid-cell centres (±5 px). Line offset: line on the left = negative, right = positive (looking forward from behind the robot).
  5. Re-reading within 10 ms costs nothing. Most values come from mode 9; in modes 11 and 12, count() and colors_found() read mode 6, which needs about 30 ms more, so do not call them every loop. device_id() reads mode 8: call it outside the loop.
  6. Colors are taught on the camera. "Target 1" is color 1 in the program; the program cannot change target colors. Sample-spot positions are also set on the camera.