MBC IMU Gyro × EV3 Pybricks

Read the IMU gyro on the EV3 with MicroPython in three steps

MicroPython on the EV3 always goes in this order: first import, then create the object and name the gyro, and only then use that name to read values. Pick Pybricks v4 or EV3 MicroPython v2 above, 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 program prints.

Your software:
1

Import the modules

On the EV3 the gyro acts as a stock gyro, so no library is needed: Pybricks' built-in LUMPDevice reads its "modes" directly (each mode sends back a few numbers).

  1. Open code.pybricks.com in Chrome and put the Pybricks firmware on the EV3 as the site explains (the EV3 version is in beta).
  2. Turn on the EV3 first, then plug the gyro into a sensor port (keep the robot still at that moment).
  3. Create a new program and write these lines at the top (marked ① on the right).
2

Create the object: which port

Create an object with LUMPDevice: tell the program which port the gyro is on and give it a name. You read values through that name.

Which port is the gyro on?

The port must be written as Port.S1 (not just a number), which is why step 1 imports Port.

3

Read values

Write "name.read(mode)[which]": each mode sends a few numbers, and [0], [1]… pick one (counting from 0). Use one mode per loop: the EV3 needs about 0.5 s every time the mode changes. Click a reading and the program and output 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 EV3 port is read-only, so Pybricks cannot tell the sensor to zero; the "Zero" button here is like unplugging and replugging the sensor (the heading is 0 at power-on). To zero inside a program, use the "Remember the start" card. Firmware version 400 is an example value.

An example for every reading

Full examples


        

!


        

!


        

!

Good to know

  1. Unplugging stops the program: this page reads the sensor modes directly with no MBC library to help, so a missing sensor gives OSError: [Errno 19] ENODEV and the program stops (in both Pybricks v4 and EV3 MicroPython v2). Plug it back in and run again. Plug the sensor in before you press run.
  2. One mode per loop: the EV3 needs about 0.5 s every time the mode changes. Use mode 0 (3D heading) for turning and mode 6 (pitch) for slopes, but do not read both in the same loop.
  3. The stock gyro way works too: on the EV3 the sensor acts as a stock gyro, and website EV3 beginner examples 1 to 5 use GyroSensor(Port.S1) with angle() and reset_angle(0) (Pybricks angle() reads the gyro 1D heading). For pitch, turn rate and the other new modes, use LUMPDevice / Ev3devSensor as on this page.
  4. Direction and zero: turning right makes the heading bigger; one turn = 360 and it keeps adding up. Keep the robot still at power-on (the heading is 0 then). If the heading is off, unplug the sensor and plug it back in. Modes 2 to 6 need sensor firmware v4.0 or later.
  5. Values the EV3 cannot read: magnetometer trust, magnetic disturbance, face up, raw 9-axis values and the calibration commands are only for Arduino, mBlock and micro:bit; on the EV3, calibrate with the buttons on the sensor.