MBC IMU Gyro × Arduino

Read the IMU gyro with Arduino in three steps

An Arduino program goes in this order. First wire it up and paste the helper. Then, in setup(), start talking and wait until the sensor is ready. Last, in loop(), read values with mbcRead(). Follow the steps on the left, and the program on the right grows with you. Turn, raise and shake the robot in the simulator to see what the Serial Monitor shows.

1

Wire it up and paste the helper

Arduino talks to the sensor over I2C (4 wires). You do not need to install a library. Write 2 lines at the very top and paste a "helper" at the very bottom. Then you can read values with mbcRead() and send commands with mbcWrite().

Which board do you use? (Only the SDA / SCL pins change. The program is the same.)

  1. Unplug the USB cable before wiring. Use the Universal I2C edition (the SPIKE and EV3 editions have different firmware and cannot connect to Arduino).
  2. Sensor VCC → 5V, GND → GND
  3. Sensor SDA → A4, SCL → A5
  4. Mount the sensor flat on the robot with the board's printed X axis pointing to the front. Keep the robot still for about 0.5 s at power-on; it learns which side is up and zeroes itself.
  5. Open the Arduino IDE: File → New, and delete what is already there.
  6. Write lines 1 and 2 at the very top. Paste the "helper" at the very bottom (marked ① in the program on the right; "Copy program" copies it too).
2

Start talking and wait until the sensor is ready

setup() runs only once, when the board starts. It opens the serial port, starts I2C and waits until the sensor says "I am ready". ADDR is the sensor's "house number" (its I2C address). You write it every time you read a value.

The sensor's I2C address:

If the sensor is not found:

3

Use a function

To read a value, write "mbcRead(ADDR, id)". To send a command, write "mbcWrite(ADDR, command)". Click a function, and the program and the Serial Monitor change 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 = command E0. When the sensor is unplugged, every value the Arduino reads is -32768. Firmware version 102 is an example value.

An example for every function

28 functions (values, commands, plus the shared ids every MBC sensor has). Each one has a short example. Press "Try it" to jump to step 3. The program and the Serial Monitor on the right change to that example.

Full examples

Press "Copy" to get the whole program (the helper is already at the bottom). Paste it into the Arduino IDE and upload.

Four headings + type z to zero

        

!A shorter version of website example 3. After uploading, open the Serial Monitor (115200), lay the gyro flat and turn it slowly; type z in the box at the top and press Enter to zero all four headings.

Which side is up? LED on when flat

        

!A shorter version of website example 6: read 0x08–0x0A at once (trust, disturbance, face up), print a line only when the face up changes, and light the built-in LED when it is flat (+Z up).

Good to know

  1. Unplugging does not stop the program. When the sensor cannot be read, mbcRead() gives -32768 (MBC_NONE) and mbcReadMany() gives false; plug it back in and it works again. -32768 is also the smallest heading value, so check for MBC_NONE before using a heading (a robot should stop when it cannot read).
  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 upside down. "gyro Z" is the raw gyro value: negative turning right when flat, the opposite of the heading.
  3. Which heading: start with the 3D heading (right for any mounting angle); for long runs with no drift use the fused heading; near motors or an iron field the magnetometer is disturbed, so use the 3D heading. The magnetic heading is relative, not north.
  4. Read neighbours together: ids 0x01–0x0A and 0x10–0x18 are each in a row; read them with one mbcReadMany() call so they come from the same moment. Set the Serial Monitor to 115200 baud.