MBC SharpShot Vision × micro:bit (MakeCode JavaScript)

Read SharpShot Vision with MakeCode in three steps

The micro:bit reads the camera with its built-in I2C commands, no extension needed (the MBC extension has no SharpShot Vision blocks yet, so this page uses JavaScript). In order: wire it, paste the helper and pick the camera mode; then in on start wait for the camera and switch the mode; then read values in forever. Work down the steps on the left and watch the program grow on the right; drag things in the simulated camera view to see what it prints.

v1 not yet hardware-verifiedThe value layout is final and passed computer tests; real-hardware numbers (fps, line thresholds, I2C pins) may change after hardware testing.

1

Wire it, paste the helper, pick the mode

There are no camera blocks, so first paste the "helper" (a few I2C functions) at the bottom of the program; then you can call ucReadBlob() and friends.

  1. Wiring: camera SDA → P20, SCL → P19, GND → GND; the camera needs an external 5 V supply (not the micro:bit 3V pin), with its GND joined to the micro:bit GND.
  2. I2C pins are provisional until hardware testing: on the camera, SDA = IO3 and SCL = IO1 for now; the camera needs the "I2C" firmware (it shares IO1/IO3 with the LEGO serial firmware, so it is one or the other).
  3. Open a new MakeCode project, switch to JavaScript, and paste the program marked ① on the right (the helper is at the bottom; "Show helper" shows it all, and "Copy program" always copies everything). The helper is also in MBC_UniCam_v1_Host_Libraries.zip (microbit folder).
  4. Pick the camera mode: click a mode number below (mode number = camera mode × 10 + sub-option). Over I2C the program can switch the mode: the step-2 code does it at power-on.
2

First round of forever: wait for the camera, switch the mode

The camera (ESP32) takes a few seconds to boot. The first rounds of "forever" wait until it answers (the "ready" bit of F3), then the E0 command switches it to the mode from step 1. (The helper sits at the bottom of the program, so it is used inside forever, like the website examples.)

Switch the mode from the program at power-on?

3

Read values in forever

Functions are grouped by camera mode; dashed ones belong to another mode (clicking one switches it, and MODE in the program follows). 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 basic.forever; the badge shows which modes it works in. "Try it" jumps to step 3.

Full examples

More examples (blob tracking, spot color, color order, line-follower car) are on the micro:bit examples page.

Where are colors 1–4 (mode 13)

        

!Teach targets 1–4 on the camera first. "Copy" gives the whole program (helper included).

Line values (mode 41 black line)

        

!Left = negative. With no line it prints the side the line was lost, so turn that way.

Good to know

  1. When the camera cannot be read every value is −32768 (UC_NONE). Unplugging does not stop the program; the examples check == UC_NONE first. If the camera does not answer at power-on, the program keeps waiting in "on start" (small diamond on the LEDs). Tick "Unplug the sensor".
  2. The I2C version gives 16 colors / 16 spots in every mode, as raw pixels (mode 11 too), so there is no grid to decode. Fields a mode does not give are −1 (e.g. mode 13 has no width/height).
  3. Check that something is seen first. X = 0 = not seen. Red's hue code is 0, so in color order use the total to know how many entries are real.
  4. Directions: X 0–159 left→right, Y 0–119 top→bottom (top = small), middle X = 80. Line offset: left = negative, right = positive (looking forward from behind the robot).
  5. Commands are saved in the camera: E0 (mode) and E1 (spot position) stay after power-off. Target colors can only be taught on the camera; v1 has no command to write them.
  6. The MakeCode extension (pxt-mbc-sensors) has no SharpShot Vision blocks yet, so this page uses JavaScript. Switching back to Blocks shows grey JavaScript blocks; they still work.