简体   繁体   中英

Arduino MPU6050 Sensor Not Working with Code from Arduino's Website

I keep getting the following in the serial monitor over and over: AcX = -1 | AcY = -1 | AcZ = -1 | Tmp = 36.53 | GyX = -1 | GyY = -1 | GyZ = -1

I am using the following code that I got off http://playground.arduino.cc/Main/MPU-6050#sketch :

// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(333);
}

This is the wiring I am using: http://jamesmpoe.com/roboticswiki/images/4/45/MPU6050-Arduino-Uno-Connections.jpg

I do not know what is wrong.

Thank you for your help.

First of all, I'd set the clock to the X gyroscope oscillator instead of the internal oscillator (as it is suggested on the MPU6050 datasheet), hence

Wire.beginTransmission(MPU);
Wire.write(0x6B);  // PWR_MGMT_1 register
Wire.write(1);     // set to one to turn on the Xgyro oscillator
Wire.endTransmission(true);

In my project, however, I wrote a lot more initializations... I suggest you to

  1. Write some functions to write/read bit(s), byte(s) and word(s) on the I2C interface, because you'll need them
  2. Write functions to get and set the important parts of the registers in the MPU6050
  3. Write more complex functions.

Examples:

// I2C interface
bool i2c_writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t* data)
{
    Wire.beginTransmission(devAddr);
    Wire.write((uint8_t) regAddr); // send address

    for (uint8_t i = 0; i < length; i++)
    {
        Wire.write((uint8_t) data[i]);
    }
    return (Wire.endTransmission() == 0);
}

// Simple function
int16_t mpu6050_getAccelerationX() {
    i2c_readBytes(MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, 2, rwBuffer);
    return (((int16_t)rwBuffer[0]) << 8) | rwBuffer[1];
}

// Complex function
void mpu6050_initialize()
{
    mpu6050_reset();
    delay(100);
    mpu6050_setClockSource(MPU6050_CLOCK_PLL_XGYRO);
    mpu6050_setSleepEnabled(false);
    mpu6050_setWakeCycleEnabled(false);
    mpu6050_switchSPIEnabled(false);
    mpu6050_setI2CMasterModeEnabled(false);
    mpu6050_setFIFOEnabled(false);
    delay(100);

    mpu6050_setRate(7); // 1kHz scan freq
    mpu6050_setDLPFMode(MPU6050_DLPF_BW_256);
    mpu6050_setFullScaleGyroRange(MPU6050_GYRO_FS_250);
    mpu6050_setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
    mpu6050_setInterruptMode(MPU6050_INTMODE_ACTIVEHIGH);
    mpu6050_setInterruptDrive(MPU6050_INTDRV_PUSHPULL);
    mpu6050_setInterruptLatch(MPU6050_INTLATCH_WAITCLEAR);
    mpu6050_setInterruptLatchClear(MPU6050_INTCLEAR_ANYREAD);
    mpu6050_setFSyncInterruptEnabled(0);
    mpu6050_setI2CBypassEnabled(0);
    mpu6050_setClockOutputEnabled(0);
    mpu6050_setIntEnabled(MPU6050_INTERRUPT_MASK_DATA_RDY_BIT); // Interrupt when data ready
}

A useful resource is the MPU6050 registers map (you can find it here ).

It was a soldering error.

It works perfectly with the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM