9DoF Razor IMU M0 - X-accel Sensitivity

Hello!

I was wondering if there was any way to get more decimal points besides the 2 that are outputted by the basic setup code for the 9DoF Razor IMU M0.

Thank you!

Hi!

I looked at the code in MPU9250_Basic.ino and think it should be straightforward to see more decimal points. See, the values being printed are stored as floating point numbers in the microcontroller but the printing process assumes only one way to convert those numbers to readable text (by casting them to a String object).

Here’s the original code:

void printIMUData(void)
{  
  // After calling update() the ax, ay, az, gx, gy, gz, mx,
  // my, mz, time, and/or temerature class variables are all
  // updated. Access them by placing the object. in front:

  // Use the calcAccel, calcGyro, and calcMag functions to
  // convert the raw sensor readings (signed 16-bit values)
  // to their respective units.
  float accelX = imu.calcAccel(imu.ax);
  float accelY = imu.calcAccel(imu.ay);
  float accelZ = imu.calcAccel(imu.az);
  float gyroX = imu.calcGyro(imu.gx);
  float gyroY = imu.calcGyro(imu.gy);
  float gyroZ = imu.calcGyro(imu.gz);
  float magX = imu.calcMag(imu.mx);
  float magY = imu.calcMag(imu.my);
  float magZ = imu.calcMag(imu.mz);
  
  SerialPort.println("Accel: " + String(accelX) + ", " +
              String(accelY) + ", " + String(accelZ) + " g");
  SerialPort.println("Gyro: " + String(gyroX) + ", " +
              String(gyroY) + ", " + String(gyroZ) + " dps");
  SerialPort.println("Mag: " + String(magX) + ", " +
              String(magY) + ", " + String(magZ) + " uT");
  SerialPort.println("Time: " + String(imu.time) + " ms");
  SerialPort.println();
}

There are other ways to print floating point values - one is documented on the [Arduino Reference for Serial.print()

Using that knowledge we could change the ‘printIMUData()’ function like so:

void printIMUData(void)
{  
  // After calling update() the ax, ay, az, gx, gy, gz, mx,
  // my, mz, time, and/or temerature class variables are all
  // updated. Access them by placing the object. in front:

  // Use the calcAccel, calcGyro, and calcMag functions to
  // convert the raw sensor readings (signed 16-bit values)
  // to their respective units.
  float accelX = imu.calcAccel(imu.ax);
  float accelY = imu.calcAccel(imu.ay);
  float accelZ = imu.calcAccel(imu.az);
  float gyroX = imu.calcGyro(imu.gx);
  float gyroY = imu.calcGyro(imu.gy);
  float gyroZ = imu.calcGyro(imu.gz);
  float magX = imu.calcMag(imu.mx);
  float magY = imu.calcMag(imu.my);
  float magZ = imu.calcMag(imu.mz);

  const uint8_t digits = 4;
  
  SerialPort.print("Accel: ");
  SerialPort.print(accelX, digits);
  SerialPort.print(", ");
  SerialPort.print(accelY, digits);
  SerialPort.print(", ");
  SerialPort.print(accelZ, digits);
  SerialPort.print(" g");
  SerialPort.println();

  SerialPort.print("Gyro: ");
  SerialPort.print(gyroX, digits);
  SerialPort.print(", ");
  SerialPort.print(gyroY, digits);
  SerialPort.print(", ");
  SerialPort.print(gyroZ, digits);
  SerialPort.print(" dps");
  SerialPort.println();

  SerialPort.print("Mag: ");
  SerialPort.print(magX, digits);
  SerialPort.print(", ");
  SerialPort.print(magY, digits);
  SerialPort.print(", ");
  SerialPort.print(magZ, digits);
  SerialPort.print(" uT");
  SerialPort.println();

  SerialPort.println("Time: " + String(imu.time) + " ms");
  SerialPort.println();
}

That code allows you to easily change the number of digits printed. Keep in mind that the actual precision and accuracy of the sensors onboard is not changed with this process - so printing 100 (or even 10) digits is most likely overkill. You can check the specifications of the sensor in the [MPU9250 Datasheet

Hope that helps - have fun!](https://cdn.sparkfun.com/assets/learn_tutorials/5/5/0/MPU9250REV1.0.pdf)](Serial.print() - Arduino Reference)