Measuring angles with mpu9250

Hi, I am currently working on a project, using mpu9250 and Arduino I have to measure a 20 degrees front tilt and 130 degrees tilt of a person posture when sitting down so I guess I will be measuring the Y-axis. Every time it goes over 20 or 130 degrees, i want the vibration motor to vibrate, but at the moment I don’t even know how I should measure the angles with the accelerometer value and give commands to it… although there are many tutorials online with the mpu6050, there aren’t many tutorials on mpu9250 so it is really hard for me to get information about these things. I would like to know how to give commands to this mpu so that every time it detects a front tilt of 20 degrees and back 130 degrees, the vibration motor vibrates. I don’t have the vibration motor totally set at the moment, so, for now, I might be just using LED as an output. Can anyone help me with this? thanks heaps

This is my current code, and i think it is reading as it should be and the values are displayed on serial monitor.

CODE:

#include <MPU9250.h>

// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;

void setup() {
  // serial to display data
  Serial.begin(115200);
  while(!Serial) {}

  // start communication with IMU 
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }
}

void loop() {
  // read the sensor
  IMU.readSensor();
  // display the data
  Serial.print("AccelX: ");
  Serial.print(IMU.getAccelX_mss(),6);
  Serial.print("  ");
  Serial.print("AccelY: ");  
  Serial.print(IMU.getAccelY_mss(),6);
  Serial.print("  ");
  Serial.print("AccelZ: ");  
  Serial.println(IMU.getAccelZ_mss(),6);
  
  Serial.print("GyroX: ");
  Serial.print(IMU.getGyroX_rads(),6);
  Serial.print("  ");
  Serial.print("GyroY: ");  
  Serial.print(IMU.getGyroY_rads(),6);
  Serial.print("  ");
  Serial.print("GyroZ: ");  
  Serial.println(IMU.getGyroZ_rads(),6);

  Serial.print("MagX: ");  
  Serial.print(IMU.getMagX_uT(),6);
  Serial.print("  ");  
  Serial.print("MagY: ");
  Serial.print(IMU.getMagY_uT(),6);
  Serial.print("  ");
  Serial.print("MagZ: ");  
  Serial.println(IMU.getMagZ_uT(),6);
  
  Serial.print("Temperature in C: ");
  Serial.println(IMU.getTemperature_C(),6);
  Serial.println();
  delay(200);
}

an example screenshot of the serial monitor:

13:19:38.754 -> AccelX: 0.272953 AccelY: 9.759263 AccelZ: 2.557138
13:19:38.754 -> GyroX: 0.000154 GyroY: 0.000113 GyroZ: -0.000438
13:19:38.754 -> MagX: 4.202976 MagY: 87.025505 MagZ: 8.152931
13:19:38.788 -> Temperature in C: 22.443675

Hi Jinkyung,

There’s no easy way around it, this is going to need some solid maths, particularly trigonometry.

While it’s totally possible to do generalised coordinate transformations with matrix multiplication, you’ll make life much easier for yourself if you align the coordinates of your accelerometer to your coordinate system ie. make sure you set up your accelerometer so, for example, when it’s measuring 1g on your Y axis and 0 on the X and Z, your person is standing upright.

DFRobot has a short piece on this here: https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing

Digikey have a fairly in-depth piece about it too:
https://www.digikey.com/en/articles/using-an-accelerometer-for-inclination-sensing

Regards,
Oliver
Support | Core Electronics