I've been trying to read raw values from MPU6050 and my problem is, instead of getting negative values when i go the opposite direction (accelerometer) or rotate the opposite direction (gyroscope), i get the end scale reading.
My gyro FS is set to 1 i.e. +/- 500 deg/s and accelerometer AFS is set to 1 i.e. +/- 8g.
When I try to read Z axis gyro readings, instead of getting negative values, i get something in the range of 65500. And in the case of the accerometer z-axis, when i hold the MPU6050 upside down, I exected to get a raw value in the ballpark of -4096 but again, it reads something in the range of 65500.
Can someone help me with this? What am I missing?
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
long gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
//Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(100);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,1);
display.println("Gyro setup...");
display.display();
delay(500);
//Activate the MPU-6050
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x6B); //Send turn-on command
Wire.write(0x00); //Send reset command
Wire.endTransmission(); //End the transmission
delay(200);
//Configure the accelerometer (+/-8g)
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x1C); //Send accelerometer self-test command
Wire.write(0x10);
Wire.endTransmission();
//Configure the gyroscope (+/-500 deg/s)
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x10);
Wire.endTransmission();
display.println("Setup done");
display.display();//End the transmission
delay(200);
}
void loop() {
readmpu();
display.setCursor(25,10);
display.print(acc_z);
display.display();
delay(50);
display.clearDisplay();
}
void readmpu()
{
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x3B); //Send the requested starting register
Wire.endTransmission(); //End the transmission
Wire.requestFrom(0x68,14); //Request 14 bytes from the MPU-6050
while(Wire.available() < 14); //Wait until all the bytes are received
acc_x = Wire.read()<<8|Wire.read(); //Add the low and high byte to the acc_x variable
acc_y = Wire.read()<<8|Wire.read(); //Add the low and high byte to the acc_y variable
acc_z = Wire.read()<<8|Wire.read(); //Add the low and high byte to the acc_z variable
temperature = Wire.read()<<8|Wire.read(); //Add the low and high byte to the temperature variable
gyro_x = Wire.read()<<8|Wire.read(); //Add the low and high byte to the gyro_x variable
gyro_y = Wire.read()<<8|Wire.read(); //Add the low and high byte to the gyro_y variable
gyro_z = Wire.read()<<8|Wire.read();
}
In the C/C++ language, there is no requirement for left to right ordering of the function calls above, so you may end up with the two bytes swapped. Depends on the compiler.