Dear Arduino Community,
I am struggling with the connection and setting of acceleration offsets for my MPU6050 with GY-521 breakout board. I am using the MPU6050 Arduino Library by Electronic Cats Version 0.0.2.
The following code and output accurately illustrate the problem:
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 mpu(0x68);
int16_t ax, az, gy;
void setup() {
Serial.begin(9600);
// Initialize MPU6050, test connection, set & get accel/gyro offsets
mpu.initialize();
Serial.println("Testing Connection...");
Serial.print(mpu.testConnection() ? "Connection Success" : "Connection Failed");
Serial.print("\n");
Serial.println("Device ID...");
Serial.print(mpu.getDeviceID());
Serial.print("\n");
mpu.setXAccelOffset(100);
mpu.setYAccelOffset(100);
mpu.setZAccelOffset(100);
mpu.setXGyroOffset(100);
mpu.setYGyroOffset(100);
mpu.setZGyroOffset(100);
Serial.print("ax_Off: "); Serial.print(mpu.getXAccelOffset()); Serial.print("\t");
Serial.print("ay_Off: "); Serial.print(mpu.getYAccelOffset()); Serial.print("\t");
Serial.print("az_Off: "); Serial.print(mpu.getZAccelOffset()); Serial.print("\t");
Serial.print("gx_Off: "); Serial.print(mpu.getXGyroOffset()); Serial.print("\t");
Serial.print("gy_Off: "); Serial.print(mpu.getYGyroOffset()); Serial.print("\t");
Serial.print("gz_Off: "); Serial.print(mpu.getZGyroOffset()); Serial.print("\t");
Serial.print("\n");
}
void loop() {
// Get Raw sensor data and Serial Print
ax = mpu.getAccelerationX();
az = mpu.getAccelerationZ();
gy = mpu.getRotationY();
Serial.print("ax: "); Serial.print(ax); Serial.print("\t");
Serial.print("az: "); Serial.print(az); Serial.print("\t");
Serial.print("gy: "); Serial.println(gy);
delay(1000);
}
The output of the code:
Testing Connection...
Connection Failed
Device ID...
57
ax_Off: 0 ay_Off: 0 az_Off: 0 gx_Off: 100 gy_Off: 100 gz_Off: 100
ax: -992 az: 16228 gy: 636
ax: -968 az: 16156 gy: 630
ax: -1036 az: 16120 gy: 664
ax: -1040 az: 16184 gy: 622
ax: -968 az: 16076 gy: 653
ax: -964 az: 16080 gy: 635
ax: -968 az: 16172 gy: 646
ax: -1092 az: 16080 gy: 623
ax: -1044 az: 16036 gy: 630
ax: -1000 az: 16160 gy: 670
ax: -1064 az: 16088 gy: 632
ax: -1016 az: 16152 gy: 639
ax: -1036 az: 16108 gy: 622
Connection issue: The first issue is that mpu.testConnection() returns a boolean false. When searching for my device ID I am returned with 57 (or hexadecimal 0x39). After comparing this with the code in the MPU6050.ccp file, the testConnection() method is defined as follows:
bool MPU6050::testConnection() {
return getDeviceID() == 0x34;
}
It is clear why this is returning me a boolean false and why I am getting a "Connection failed" response. Does that matter though? The MPU6050 still seems to be returning acceleration and gyro values that alter when physically handing the sensor.
Accleration Offset issue: The second issue is that I cannot set/get my acceleration offsets, while setting/getting gyro offsets works. Again, I checked the code within the MPU6050.cpp file and found the functions to be defined as follows:
int16_t MPU6050::getXAccelOffset() {
uint8_t SaveAddress = ((getDeviceID() < 0x38 )? MPU6050_RA_XA_OFFS_H:0x77); // MPU6050,MPU9150 Vs MPU6500,MPU9250
I2Cdev::readBytes(devAddr, SaveAddress, 2, buffer);
return (((int16_t)buffer[0]) << 8) | buffer[1];
}
void MPU6050::setXAccelOffset(int16_t offset) {
uint8_t SaveAddress = ((getDeviceID() < 0x38 )? MPU6050_RA_XA_OFFS_H:0x77); // MPU6050,MPU9150 Vs MPU6500,MPU9250
I2Cdev::writeWord(devAddr, SaveAddress, offset);
int16_t MPU6050::getYGyroOffset() {
I2Cdev::readBytes(devAddr, MPU6050_RA_YG_OFFS_USRH, 2, buffer);
return (((int16_t)buffer[0]) << 8) | buffer[1];
}
void MPU6050::setYGyroOffset(int16_t offset) {
I2Cdev::writeWord(devAddr, MPU6050_RA_YG_OFFS_USRH, offset);
}
I am assuming that the getDeviceID() < 0x38 portion of the acceleration offset code is causing the problem (because my getDivceID() provides me with a hexadecimal 0x39).
I am a totally new to Arduino and programming and am not sure how to proceed. Could I somehow set a new ID for my device (0x34)? Would that in fact solve these problem, or would I somehow be creating issues on other places within the code?
Your input, help and explanations would be very much appreciated!
Thanks,
Pedro