That's with the sensor lying on a wooden flat table (I understand this will cause some interference). However, if the sensor is relatively level, how am I getting values that range as high as 16000? I guess my question is what do these numbers mean? I'm having troubles finding some beginner documentation.
I just purchased a MPU-9150 and I'm trying to learn the most basic way to read a value off the sensor. I'm brand new to I2C. To learn I usually start from the bottom so I've created this real simple code:
#include "Wire.h"
#define ADDRESS 0x3B // this points to the ACCEL_XOUT_H register
float a; // to hold the returned value
void setup()
{
Wire.begin(); // wake up I2C bus
Serial.begin(9600);
}
void loop()
{
// Reset the register
Wire.beginTransmission(ADDRESS);
Wire.write(0);
Wire.endTransmission();
// Request the value from the register
Wire.requestFrom(ADDRESS, 16);
a = Wire.read();
Serial.println(a);
delay(1000);
}
However, this code returns -1.00 continuously. What am I doing wrong? Shouldn't it give me something a little more useful? I appreciate any support.
According to the datasheet, the ADDRESS should be 0xB0 or 0xB1 depending upon how you have the AD0 pin on the device wired.
Try defining ADDRESS to be 0xB0. If that still gives -1, try 0xB1.
Hi pete, actually I think my address was correct as per the data sheet I have. Here's the updated code I tried troubleshooting and it looks like this is starting to get some data back. Now I just have to piece more information together.
#include "Wire.h"
#define MPU9150_PWR_MGMT_1 0x6B // apparently this has to be cleared
#define ADDRESS 0x3B // this is what I'm trying to read
void setup()
{
// wake up I2C bus
Wire.begin();
Serial.begin(9600);
// Clear the 'sleep' bit to start the sensor.
Wire.beginTransmission(0x68);
Wire.write(MPU9150_PWR_MGMT_1);
Wire.write(0);
Wire.endTransmission();
}
void loop()
{
// Select what we want to read
Wire.beginTransmission(0x68);
Wire.write(ADDRESS);
Wire.endTransmission();
// Read and print the results
Wire.requestFrom(0x68, 1);
int a = Wire.read();
Serial.println(a);
delay(1000);
}
No problem, I'm glad I actually knew enough to figure out your address was wrong. I'm learning haha!
By any chance do you have insight as to what the output actually means? Why are some numbers really high and others closer to 0? Is there some sort of filtering that needs to happen?
The first three numbers are probably your accelerometer output.
An accelerometer which is not moving or accelerating will report an apparent acceleration of +1g or -1g , which represents the effect of the earth's gravitation on one or more of the sensing elements inside the chip.\
So that 16000 number for the third data item is probably 1g in the z direction. In fact these chips are more commonly used to attempt to detect the direction of gravity than they are to actually measure acceleration.
The next three numbers are you gyro readings , which are small because you are not spinning the device around.
The third three numbers are a vector representing the direction of the geomagnetic field vector at your location. You should note that altough you are normally concerned with the projection of this vector into the horizontal plane to determine the direction of magnetic north, the vector itself is actually pointing diagonally up or down into the earth.
I have no idea what those numbers mean. Post the code which produces them.
And, I've never used this device so I can only go by what the datasheets say.