MBC IMU Gyro × micro:bit (MakeCode)

Read the IMU gyro with a micro:bit in three steps

In MakeCode for micro:bit, first add the MBC extension, then wait for the gyro in "on start", and last put a heading or tilt block into "forever". Follow the steps on the left, and the blocks on the right grow with you. The block pictures come from MakeCode's own renderer, so they look the same as in the editor. Switch to JavaScript at the top right, just like the switch in MakeCode.

MakeCode is free and needs no login. Open it in Chrome or Edge. Click "Download" to save a .hex file and drag it onto the micro:bit.

1

Add the MBC extension and wire it up

The micro:bit does not know this gyro yet (its own on-board compass is a different thing). After you add the MBC extension, an "MBC Sensors" category appears, and the gyro blocks are in its "IMU" group. Pick your micro:bit version first; the hardware check below changes with it.

Which micro:bit do you have? (V2 has "V2" printed on the back)
  1. Open MakeCode and click "New Project".
  2. At the bottom of the toolbox, click "Advanced" → "Extensions".
  3. Paste https://github.com/contactmbcrobot/pxt-mbc-sensors into the search box, press Enter and click mbc-sensors.
  4. The blocks are in English. (Traditional Chinese is also included in the extension.)
2

On start: wait for the gyro

At power-on the gyro first works out how it is mounted and zeroes the heading, so keep the robot still. The program first waits for it: tick = connected, cross = not connected. You find wiring problems at once.

I2C address of the sensor
  1. From "Logic", drag "if … then … else" into "on start".
  2. From "MBC Sensors" → "more", drag "wait for device at … ready, timeout … ms" into the condition. Pick 106 (0x6A) and type 3000.
  3. From "Basic", drag two "show icon" blocks: one tick, one cross.
  4. Put "pause 100 ms" into "forever".

3

Use the gyro blocks

In "MBC Sensors → IMU" you find headings, turn rate, pitch, face up, raw 9-axis values, zero heading and a "turned by" event; magnetic trust, disturbance, calibration and use address are under "more"; the "Device" group has shared blocks that every MBC sensor has. Pick one, and the blocks and the output switch to its example.

Sensor simulator: the left picture is seen from above (heading), the right one from the side (pitch). Drag the sliders and press the buttons, and watch the output change

The simulator is only a picture: values come from the robot's direction and angle on screen, not the sensor's own algorithm; real readings wobble by about 1°. The "Zero" button = the "IMU zero heading" block. Firmware version 1.2 is an example value.

An example for every block

Every IMU block and every Device block has an example you can copy (address 0x6A). Click "Try it" to jump to step 3, and the blocks and output on the right switch to that example.

Good to know

  1. Unplugging does not stop the program: when the gyro cannot be read, reading blocks return 0 (true/false blocks return false), and nothing is printed. Careful: a heading of 0 looks like "facing straight ahead". Before turning by heading, check "device at … is ready", or the robot may keep spinning.
  2. Direction: turning right makes the heading bigger, left smaller; one turn = 360 and it keeps adding up (two turns = 720, and it can go negative). If right makes it smaller, the sensor is mounted upside down. "gyro Z axis" is the raw gyro value: negative turning right when flat, the opposite of the heading; "turn rate" goes the same way as the heading.
  3. Which heading: start with gyro 3D (right for any mounting angle); for long runs with no drift use fused; near motors or an iron field the magnetometer is disturbed, so use gyro 3D. The magnetic heading is relative (0 when zeroed), not north; the micro:bit's own compass has nothing to do with this gyro.
  4. Pitch rate is not "how fast the pitch changes": it is the gyro value about the board's X axis (counter-clockwise seen from the front of X is positive).
  5. JavaScript extras: mbc.readValue(106, 0x01) reads any I2C value directly (−32768 = not connected). The value ids are in the I2C reference on the product website.