How to read values off of MPU 9150

Hello, this is my first dealing with an IMU and I2C as well, the problem I am having is that I can’t manage to get the accelerometer or gyro values from the MPU 9150. https://www.sparkfun.com/products/11486

I tried a couple of codes, one was a pack of libraries https://github.com/zarthcode/MPU9150Lib and I really didn’t know what I would do with the results because it printed the “output of the data fusion” and I really dont know what that is exactly.

////////////////////////////////////////////////////////////////////////////
//
//  This file is part of MPU9150Lib
//
//  Copyright (c) 2013 Pansenti, LLC
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy of 
//  this software and associated documentation files (the "Software"), to deal in 
//  the Software without restriction, including without limitation the rights to use, 
//  copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
//  Software, and to permit persons to whom the Software is furnished to do so, 
//  subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all 
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
//  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
//  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
//  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <Wire.h>
#include "I2Cdev.h"
#include "MPU9150Lib.h"
#include "CalLib.h"
#include <dmpKey.h>
#include <dmpmap.h>
#include <inv_mpu.h>
#include <inv_mpu_dmp_motion_driver.h>
#include <EEPROM.h>

MPU9150Lib MPU;                                              // the MPU object

//  MPU_UPDATE_RATE defines the rate (in Hz) at which the MPU updates the sensor data and DMP output

#define MPU_UPDATE_RATE  (10)

//  SERIAL_PORT_SPEED defines the speed to use for the debug serial port

#define  SERIAL_PORT_SPEED  115200

void setup()
{
    Serial.begin(SERIAL_PORT_SPEED);
    Serial.println("Arduino9150 starting");
    Wire.begin();
    MPU.init(MPU_UPDATE_RATE);                             // start the MPU
}

void loop()
{  
  if (MPU.read()) {                                        // get the latest data
//  MPU.printQuaternion(MPU.m_rawQuaternion);              // print the raw quaternion from the dmp
//  MPU.printVector(MPU.m_rawMag);                         // print the raw mag data
//  MPU.printVector(MPU.m_rawAccel);                       // print the raw accel data
//  MPU.printAngles(MPU.m_dmpEulerPose);                   // the Euler angles from the dmp quaternion
//  MPU.printVector(MPU.m_calAccel);                       // print the calibrated accel data
//  MPU.printVector(MPU.m_calMag);                         // print the calibrated mag data
    MPU.printAngles(MPU.m_fusedEulerPose);                 // print the output of the data fusion
    Serial.println();
  }
}

There are so many libraries here within each other so I’m pretty much lost.

The second one is from http://playground.arduino.cc/Main/MPU-9150 , I liked this one cause I would like to deal directly with the registers using the Wire library only and no other library.

But the strange thing that this code only outputs “36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1” over and over, nothing is changing.

I would really appreciate any help, I have been struggling with this for a couple of days now. Thank you!

It looks like your i2c communication is not actually working.

Do you get a correct response from the i2c_scanner sketch ?

I removed all the connections except for Vcc, GND, SDA and SCL and it does work for the serial monitor now but the output for Arduino Playground - MPU-9150 is still the same

Now, say I just wanted to get the X axis accelerometer output. The device's address is 0x68, register address are 0x1B for the high byte and 0x1C for the low byte. Using the Wire library, what should the commands for reading be like?

I have the same problem with you---seeing "36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1" over and over.
However, I found the other two ways to solve it .
The codes in Arduino Playground - MPU-6050 and GitHub - sparkfun/MPU-9150_Breakout: Example code and PCB design files for the MPU-9105, 9DOF. can work out correctly.
Maybe the problem is in the code of 9150 itself or for other configuration reasons, hope someone can check it later.

Hi!
Was this issue ever solved?
I have the same problem but interestingly I get correct measurements from the magnetometer and the dummy data from any of the mpu6050 compatible registers.
I tried every sample code.
I guess its hardware problem :frowning:

geris2:
Hi!
Was this issue ever solved?
I have the same problem but interestingly I get correct measurements from the magnetometer and the dummy data from any of the mpu6050 compatible registers.
I tried every sample code.
I guess its hardware problem :frowning:

Hi geris,

if you are having the same I2C issue it may be that your code has the incorrect AD0 address. If pin 9 of your MPU9150 is pulled high, then the address will be 0x69. If pin 9 is pulled low, then the address will be 0x68.

HoCo:
Hi geris,

if you are having the same I2C issue it may be that your code has the incorrect AD0 address. If pin 9 of your MPU9150 is pulled high, then the address will be 0x69. If pin 9 is pulled low, then the address will be 0x68.

Hi HoCo,

Thanks for the idea, but that's not the problem unfortunately.

Hi guys,
same problem,
its not the I2C address i have tried both
my hardware runs IMU6050 perfectly on 0X68

when trying with the IMU9150 i get 36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1

if you try the IMU6050 example with the IMU9150 hardware you can get readings from it.
wow how strange is that.

The code im using is from Arduino Playground - MPU-9150
the arduino playground site.

i know coder hate to be wrong, they are never wrong
but on this occasion i think there is a strong possibility

I suspect its the way the registers are handled, im going to try merging the 9150 code into the 6050 code and see if i can spot the error.

JT007:
Hi guys,
same problem,
its not the I2C address i have tried both
my hardware runs IMU6050 perfectly on 0X68

when trying with the IMU9150 i get 36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1

if you try the IMU6050 example with the IMU9150 hardware you can get readings from it.
wow how strange is that.

The code im using is from http://playground.arduino.cc/Main/MPU-9150
the arduino playground site.

I’m here exactly because of the above. It is extremely odd that the code doesn’t work, plus also extremely disappointing.

I was attempting to try figure out where the issue is, but no luck yet. Although, the very nice thing is that at least another source of test software is working - at:

Here, you have to (manually) copy EACH DIRECTORY - namely ‘mpu6050’ AND ‘I2Cdev’ to your own local ‘Arduino/libraries’ directory - and that ‘libraries’ directory is usually automatically created when the Arduino Sketch software is first installed.

Actually, should copy all the directories (as-is) and their contents contained in my uploaded ZIP archive file to the arduino ‘libraries’ directory. The zip file is attached to my post. That should make things easy.

Next, jump into the ‘MPU6050’ directory, then jump into the ‘Examples’ directory, then jump into ONE of the directories called ‘MPU6050_raw’. There you will find a file called ‘MPU6050_raw.ino’, and this is the one that you need to OPEN or load up in the Arduino Sketch software.

As for the other .ino sketch files (such as MPU6050_AHRS.ino) hanging around in there - just FORGET about those ones, as they are for something else, and won’t work.

ALSO, one SUPER IMPORTANT amendment to be made in the MPU6050_raw.ino file is to change the following line from

Serial.begin(38400);

to this:

Serial.begin(9600);

That is, make the transfer rate 9600 characters per second instead of 38400, otherwise you may see garbage (jumbled weird characters) piling up in the TOOLS → Serial Monitor window. This is because the DEFAULT baud rate for Serial Monitor is 9600 bit per second. If we decide to just stick with Serial.begin(38400); then it’s necessary to go into the Serial Monitor window and manually set 38400 as well.

At the moment, even though I initially had the ‘ADO’ pin (on the MPU9150) totally disconnected, I decided to just connect this ADO pin to the GND pin of the arduino board, which is meant to make the MPU9150 use address 0x68 for I2C communications (instead of address 0x69).

So far, I’m seeing data like this:

a/g/m: -4684 -1332 15672 -141 -47 -75 78 -96 -110
a/g/m: -4856 -1276 16168 -71 -1 -61 79 -101 -109
a/g/m: -5120 -1404 16232 -109 -32 -64 76 -102 -114
a/g/m: -4992 -1288 15972 -148 21 -69 75 -99 -107
a/g/m: -4784 -1344 15672 -160 -20 -74 78 -100 -106

Also, confirming that the ‘Short Example Sketch’ code for the MPU6050 is also working for the 9150, from the link below:

http://playground.arduino.cc/Main/MPU-6050#short

In the above link, you can click on the tiny ‘get code’ tab, and then save the short code as some arbitrary name, such as ‘MPU9150test.ino’. The original name is something like ‘sourceblock_1.txt’, so just call it ‘MPU9150test.ino’. Then just use Arduino Sketch to open it. Yes, it is code for 6050, but it works for 9150. Everything worked just fine for my mpu9150 interfaced with my arduino 2560 MEGA.

Finally… I reckon that whoever put up the software at http://playground.arduino.cc/Main/MPU-9150 should figure out why the output results are:

36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1
36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1
36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1

libraries.zip (72.9 KB)

Southpark:
Finally… I reckon that whoever put up the software at http://playground.arduino.cc/Main/MPU-9150 should figure out why the output results are:

36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1
36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1
36.50 -1 -1 -1 -1 -1 -1 -1 -1 -1

Ok… back again. I’ve identified the issue now. The problem is that the following statement occurs in TWO places (yes! two places) in their code:

int MPU9150_I2C_ADDRESS = 0x69;

So I am guessing that if we decide to set the address to 0x68 (instead of 0x69), then we MUST look for the OTHER statement further down the code, and change the value there too.

What I have done instead is to amend the code a tiny bit, and have added a line:

#define MPU9150_I2C_GLOBAL 0x68 // Put 0x68 if MPU9150 ADO pin is grounded. Change to 0x69 if ADO pin is tied to Vcc.

This means we only need to set the I2C address of the MPU9150 at one spot (instead of 2 spots).

I’ve also commented out the part that says:

//int MPU9150_readSensor(int addr){
// Wire.beginTransmission(MPU9150_I2C_ADDRESS);
// Wire.write(addr);
// Wire.endTransmission(false);

// Wire.requestFrom(MPU9150_I2C_ADDRESS, 1, true);
// return Wire.read();
//}

The reason for commenting it out is because it appears to be redundant code. Anyway, I’ve posted up a working version of the code with the I2C address set at 0x68. See the file attachment.

The output is (in the order of temperature, compass/magnetometer, gyro, accelerometer) is:

26.72 -31 -31 -31 -192 -87 -198 -8628 856 14752
26.72 -31 -32 -32 -64 -175 -243 -8516 788 14412
27.52 -30 -30 -30 -180 49 164 -8568 660 14560
27.52 -33 -33 -33 -231 78 -93 -8544 856 14812
26.72 -31 -31 -31 -137 -130 -229 -8668 568 14680
28.18 -30 -30 -35 -145 -49 54 -8612 860 14632

One noticeable issue I see is that the compass/magnetometer readings in columns 2, 3 and 4 (xout, yout, zout) are very similar. When I physically rotate the MPU9150 around, those values do change significantly in value, but all three values appear to be linked (ie. such as 101 101 100 etc).

mpu9150.ino (9.73 KB)