Advertisement
microrobotics

MPU-9250 Sample Code

Nov 17th, 2019
3,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include "MPU9250.h"
  2.  
  3. // an MPU9250 object with the MPU-9250 sensor on
  4. // I2C bus 0 with address 0x68
  5.  
  6. MPU9250 IMU(Wire,0x68);
  7. int status;
  8.  
  9. void setup() {
  10.   // serial to display data
  11.   Serial.begin(115200);
  12.   while(!Serial) {}
  13.  
  14.   // start communication with IMU
  15.   status = IMU.begin();
  16.   if (status < 0) {
  17.     Serial.println("IMU initialization unsuccessful");
  18.     Serial.println("Check IMU wiring or try cycling power");
  19.     Serial.print("Status: ");
  20.     Serial.println(status);
  21.     while(1) {}
  22.   }
  23. }
  24.  
  25. void loop() {
  26.   // read the sensor
  27.   IMU.readSensor();
  28.   // display the data
  29.   Serial.print("AccelX: ");
  30.   Serial.print(IMU.getAccelX_mss(),6);
  31.   Serial.print("");
  32.   Serial.print("AccelY: ");  
  33.   Serial.print(IMU.getAccelY_mss(),6);
  34.   Serial.print("");
  35.   Serial.print("AccelZ: ");  
  36.   Serial.println(IMU.getAccelZ_mss(),6);
  37.  
  38.   Serial.print("GyroX: ");
  39.   Serial.print(IMU.getGyroX_rads(),6);
  40.   Serial.print("");
  41.   Serial.print("GyroY: ");  
  42.   Serial.print(IMU.getGyroY_rads(),6);
  43.   Serial.print("");
  44.   Serial.print("GyroZ: ");  
  45.   Serial.println(IMU.getGyroZ_rads(),6);
  46.  
  47.   Serial.print("MagX: ");  
  48.   Serial.print(IMU.getMagX_uT(),6);
  49.   Serial.print("");  
  50.   Serial.print("MagY: ");
  51.   Serial.print(IMU.getMagY_uT(),6);
  52.   Serial.print("");
  53.   Serial.print("MagZ: ");  
  54.   Serial.println(IMU.getMagZ_uT(),6);
  55.  
  56.   Serial.print("Temperature in C: ");
  57.   Serial.println(IMU.getTemperature_C(),6);
  58.   Serial.println();
  59.   delay(200);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement