Advertisement
microrobotics

MPU-6500

Mar 29th, 2023
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2.  
  3. const int MPU_addr=0x68;  // I2C address of MPU-6500
  4. int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
  5.  
  6. void setup(){
  7.   Wire.begin();
  8.   Wire.beginTransmission(MPU_addr);
  9.   Wire.write(0x6B);  // PWR_MGMT_1 register
  10.   Wire.write(0);     // set to zero (wakes up the MPU-6500)
  11.   Wire.endTransmission(true);
  12.   Serial.begin(9600);
  13. }
  14.  
  15. void loop(){
  16.   Wire.beginTransmission(MPU_addr);
  17.   Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  18.   Wire.endTransmission(false);
  19.   Wire.requestFrom(MPU_addr,14,true);  // read 14 bytes from MPU-6500
  20.   AcX=Wire.read()<<8|Wire.read();     // concatenate high and low byte for each axis
  21.   AcY=Wire.read()<<8|Wire.read();
  22.   AcZ=Wire.read()<<8|Wire.read();
  23.   Tmp=Wire.read()<<8|Wire.read();
  24.   GyX=Wire.read()<<8|Wire.read();
  25.   GyY=Wire.read()<<8|Wire.read();
  26.   GyZ=Wire.read()<<8|Wire.read();
  27.  
  28.   Serial.print("AcX = "); Serial.print(AcX);
  29.   Serial.print(" | AcY = "); Serial.print(AcY);
  30.   Serial.print(" | AcZ = "); Serial.print(AcZ);
  31.   Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  // convert to Celsius
  32.   Serial.print(" | GyX = "); Serial.print(GyX);
  33.   Serial.print(" | GyY = "); Serial.print(GyY);
  34.   Serial.print(" | GyZ = "); Serial.println(GyZ);
  35.   delay(1000);
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement