Advertisement
microrobotics

Arduino MPU-9250

Mar 29th, 2023
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. const int MPU_addr=0x68;  // I2C address of the MPU-9250
  3.  
  4. // Accelerometer and gyroscope values
  5. int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
  6.  
  7. void setup(){
  8.   Wire.begin();
  9.   Wire.beginTransmission(MPU_addr);
  10.   Wire.write(0x6B); // PWR_MGMT_1 register
  11.   Wire.write(0);    // set to zero (wakes up the MPU-9250)
  12.   Wire.endTransmission(true);
  13.   Serial.begin(9600);
  14. }
  15.  
  16. void loop(){
  17.   Wire.beginTransmission(MPU_addr);
  18.   Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
  19.   Wire.endTransmission(false);
  20.   Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
  21.  
  22.   // read the values from the registers
  23.   AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  24.   AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  25.   AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  26.   GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  27.   GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  28.   GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  29.  
  30.   // print out the values
  31.   Serial.print("Accelerometer: ");
  32.   Serial.print(AcX); Serial.print(" ");
  33.   Serial.print(AcY); Serial.print(" ");
  34.   Serial.print(AcZ); Serial.print(" ");
  35.   Serial.print("Gyroscope: ");
  36.   Serial.print(GyX); Serial.print(" ");
  37.   Serial.print(GyY); Serial.print(" ");
  38.   Serial.println(GyZ);
  39.  
  40.   delay(500); // wait for half a second
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement