Skip to content

IMU Sensor MPU9250

IMU The MPU9250 is a multi-chip module that integrates two chips. One chip is the MPU6500, which consists of a 3-axis gyroscope and a 3-axis accelerometer. The other is the AK8963 is a 3-axis magnetometer.

initMPU9250()

Syntax:

1
void initMPU9250();

Description:

This function initialize MPU6500 chip.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup() {
  M5.begin();
  Wire.begin();
  IMU.initMPU9250(); // this line must be after Wire.begin()
}

void loop() {
}

initAK8963()

Syntax:

1
void initAK8963(float * destination);

Description:

This function initialize AK8963 chip.

Argument Description Type
destination for AK8963 Calibration Value float *

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup() {
  M5.begin();
  Wire.begin();

  IMU.initAK8963(IMU.magCalibration);
  M5.Lcd.println("AK8963 initialized for active data mode....");
  if (Serial)
  {
    M5.Lcd.println("Calibration values: ");
    M5.Lcd.print("X-Axis sensitivity adjustment value ");
    M5.Lcd.println(IMU.magCalibration[0], 2);
    M5.Lcd.print("Y-Axis sensitivity adjustment value ");
    M5.Lcd.println(IMU.magCalibration[1], 2);
    M5.Lcd.print("Z-Axis sensitivity adjustment value ");
    M5.Lcd.println(IMU.magCalibration[2], 2);
  }
}

void loop() {
}

calibrateMPU9250()

Syntax:

1
void calibrateMPU9250(float * gyroBias, float * accelBias);

Description:

This function calcurate offset values of gyro and accelerometer.

Argument Description Type
gyroBias gyro offset float *
accelBias accel offset float *

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup() {
  M5.begin();
  Wire.begin();

  IMU.initMPU9250();
  IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.setTextSize(1);
  M5.Lcd.setCursor(0, 0); M5.Lcd.print("MPU9250 bias");
  M5.Lcd.setCursor(0, 16); M5.Lcd.print(" x   y   z  ");

  M5.Lcd.setCursor(0,  32); M5.Lcd.print((int)(1000 * IMU.accelBias[0]));
  M5.Lcd.setCursor(32, 32); M5.Lcd.print((int)(1000 * IMU.accelBias[1]));
  M5.Lcd.setCursor(64, 32); M5.Lcd.print((int)(1000 * IMU.accelBias[2]));
  M5.Lcd.setCursor(96, 32); M5.Lcd.print("mg");

  M5.Lcd.setCursor(0,  48); M5.Lcd.print(IMU.gyroBias[0], 1);
  M5.Lcd.setCursor(32, 48); M5.Lcd.print(IMU.gyroBias[1], 1);
  M5.Lcd.setCursor(64, 48); M5.Lcd.print(IMU.gyroBias[2], 1);
  M5.Lcd.setCursor(96, 48); M5.Lcd.print("o/s");
}

void loop() {
}

readByte()

Syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
uint8_t readByte(uint8_t address, uint8_t subAddress)
```;

**Description:**

This function reads a byte data from specified register of MPU9250.

| Argument | Description | Type |
| --- | --- | -- |
| address | (MPU9250/AK8963) I2C address | uint8_t |
| subAddress | register address | uint8_t |

**Example:**

```arduino
#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup() {
  M5.begin();
  Wire.begin();

  uint8_t id = IMU.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);
  M5.Lcd.print("MPU9250 I AM 0x"); M5.Lcd.print(id, HEX);
}

void loop() {
}

readGyroData()

Syntax:

1
void readGyroData(int16_t * destination);

Description:

This function reads the value of 3-axis gyro sensor.

Argument Description Type
destination read gyro values int16_t *

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup() {
  M5.begin();
  Wire.begin();
  IMU.initMPU9250();
  IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
}

void loop() {
  // If intPin goes high, all data registers have new data
  // On interrupt, check if data ready interrupt
  if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
  {
    M5.Lcd.clear();
    M5.Lcd.setCursor(0, 0);
    IMU.readGyroData(IMU.gyroCount);  // Read the x/y/z adc values
    IMU.getGres(); // get Gyro scales saved to "gRes"
    IMU.gx = (float)IMU.gyroCount[0] * IMU.gRes;
    IMU.gy = (float)IMU.gyroCount[1] * IMU.gRes;
    IMU.gz = (float)IMU.gyroCount[2] * IMU.gRes;
    M5.Lcd.print("X-gyro rate: "); M5.Lcd.print(IMU.gx, 3);
    M5.Lcd.println(" degrees/sec ");
    M5.Lcd.print("Y-gyro rate: "); M5.Lcd.print(IMU.gy, 3);
    M5.Lcd.println(" degrees/sec ");
    M5.Lcd.print("Z-gyro rate: "); M5.Lcd.print(IMU.gz, 3);
    M5.Lcd.println(" degrees/sec");
  }
  delay(500);
}

readAccelData()

Syntax:

1
void readAccelData(int16_t * destination);

Description:

This function reads the value of 3-axis accelerate sensor.

Argument Description Type
destination read accelerate values int16_t *

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <M5Stack.h>
#include "utility/MPU9250.h"

MPU9250 IMU; // new a MPU9250 object

void setup() {
  M5.begin();
  Wire.begin();
  IMU.initMPU9250();
  IMU.calibrateMPU9250(IMU.gyroBias, IMU.accelBias);
}

void loop() {
  // If intPin goes high, all data registers have new data
  // On interrupt, check if data ready interrupt
  if (IMU.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01)
  {
    M5.Lcd.clear();
    M5.Lcd.setCursor(0, 0);
    IMU.readAccelData(IMU.accelCount);
    IMU.getAres(); // get accelerometer scales saved to "aRes"
    IMU.ax = (float)IMU.accelCount[0] * IMU.aRes; // - accelBias[0];
    IMU.ay = (float)IMU.accelCount[1] * IMU.aRes; // - accelBias[1];
    IMU.az = (float)IMU.accelCount[2] * IMU.aRes; // - accelBias[2];
    M5.Lcd.print("X-acceleration: "); M5.Lcd.print(1000 * IMU.ax);
    M5.Lcd.println(" mg ");
    M5.Lcd.print("Y-acceleration: "); M5.Lcd.print(1000 * IMU.ay);
    M5.Lcd.println(" mg ");
    M5.Lcd.print("Z-acceleration: "); M5.Lcd.print(1000 * IMU.az);
    M5.Lcd.println(" mg ");
  }
  delay(500);
}