Issue with MPU 6050 data

Good afternoon all

I am having problems with my data from the mpu 6050 it is a generic type purchased from ebay. However when I run the example code I get sensible reading.
The issue that i am having is more than likley somthing silly that i am missing so i would very much appreciate any help.

here is the code that I am running it runs inset up and will eventually determine the starting angle of my robot.

I am using the MPU’s accelerometer in 2g mode and taking no data from the gyro or temp.

#include <Wire.h>                // pin A4 = serial Data (SDA) pin A5 = serial clock (SDA)

int MPU = 0X68;                  // declare I2C address here 
uint16_t aX = 0, aY = 0, aZ = 0;             // 16bit storage for storing the raw acc data     
float accX = 0, accY = 0, accZ = 0;          // for storing the processed values from the acc
unsigned short led_pin = 13;     // Pin used to supply the LED's with power to for visual notification.


 void setup(){

short i = 0;                      // for counting flashes of LED's using a loop. 

// initialize serial communication 

Serial.begin(9700);
Serial.println("setup initialized. . . . . ");

//  flash leds 5x as indicator that setup is initialized 
pinMode(led_pin,OUTPUT);
digitalWrite(led_pin, LOW);

 while (i <= 4){
  digitalWrite(led_pin,HIGH); 
  delay(500);
  digitalWrite(led_pin,LOW);
  delay(500);
  Serial.println (i);
  i++;
}

Serial.println("visual drone status sent . . . . " );

 
// set up MUP ready to transmit 
 Wire.beginTransmission(MPU);  //set the power register. 
 Wire.write (0x6B);
 Wire.write (0x00); 
 Wire.endTransmission(true);

 Wire.beginTransmission(MPU); 
 Wire.write(0x1B); 
 Wire.write(0x00);
 Wire.endTransmission(true);

 Wire.beginTransmission(MPU);
 Wire.write(0x1C); 
 Wire.write(0x00);
 Wire.endTransmission(true);

 Serial.println("MPU6050 initialized . . . . ");

 // read Acc to obtain orientation 
 
 Wire.beginTransmission(MPU); 
 Wire.write(0x3b);
 Wire.endTransmission(false);
 Wire.requestFrom(MPU,6,true);
 Wire.endTransmission(false);
 aX = Wire.read() <<8| Wire.read ();
 aY = Wire.read() <<8| Wire.read ();
 aZ = Wire.read() <<8| Wire.read ();
 
 Serial.println ("Data recived . . . . ");
 Serial.print ("aX = " ); Serial.println(aX);
 Serial.print (" aY = "); Serial.println(aY);
 Serial.print (" aZ = "); Serial.println(aZ); 
 
 Serial.println ("data processing in progress . . . . ");
 
  accX = aX / 16384.0;
  accY = aY / 16384.0;
  accZ = aZ / 16384.0;
  
 Serial.print ("accX = " ); Serial.println(accX);
 Serial.print (" accY = "); Serial.println(accY);
 Serial.print (" accZ = "); Serial.println(accZ); 

 
 
 Serial.println (" data processing in complete . . . . ");
 // calculate base orientation


 // flash led 3x as indication to set up complete

 while(i <= 7){
  digitalWrite(led_pin,HIGH); 
  delay(500);
  digitalWrite(led_pin,LOW);
  delay(500);
  Serial.println (i); 
  i++; 
 }

 // turn on positioning lights to indicates bluetooth connectivity  

 digitalWrite(led_pin, HIGH);
 Serial.print ("setup complete! \n");   // marks the end of setup for serial interface. 
 }

this is what’s returned in the serial monitor however the mpu is stationary on a flat surface. so I would expect that the ACC reading should be around 1g give or take a little as I am on planet earth, although if I am wrong here please do correct me.

setup initialized. . . . .
0
1
2
3
4
visual drone status sent . . . .
MPU6050 initialized . . . .
Data recived . . . .
aX = 32767
aY = 32767
aZ = 364
data processing in progress . . . .
accX = 2.00
accY = 2.00
accZ = 0.02
data processing in complete . . . .
5
6
7
setup complete!

I have checked all of my connections and wiring i have also tryed 2 MPU’s so i am prety sure i have missed somthing important in the code.

many thanks in advanced

What values are you expecting for:

aX, aY, aZ; accX, accY, accZ?

I have executed your program (your program is missing the loop() function) on my MPU6050, and it is giving the following result for acceleration: My setup is: MPU6050+UNO on the work bench.

@GolamMostafa, did you notice the aX = 60592 and the aY = 59804 ? The MPU-6050 outputs a signed 16 bit integer.

@Adampeirce1988, we don't know what you are expecting, but your code is wrong in at least three ways. Start with this: Arduino Playground - MPU-6050.

Please, never use a Wire.endTransmission() after a Wire.requestFrom() and never leave the I2C bus open with the last I2C transaction that does not have a STOP.

Koepel:
@GolamMostafa, did you notice the aX = 60592 and the aY = 59804 ? The MPU-6050 outputs a signed 16 bit integer.

@Adampeirce1988, we don't know what you are expecting, but your code is wrong in at least three ways. Start with this: Arduino Playground - MPU-6050.

Please, never use a Wire.endTransmission() after a Wire.requestFrom() and never leave the I2C bus open with the last I2C transaction that does not have a STOP.

Actually, I did not look into the OP's declared data types. I noticed that the program was not being compiled due to the absence of the loop() function. Thank you for pointing out this carelessness from my side. Dtat types are changed to int and the sensor readings are now:

Love those helpful prints!

Good morning all thanks for the helpful tips

I will do some more reading up on i2c bus and then correct them error in my code. Thank you for point that out as I am a novic still and have lots to learn.

Although the addition of the extra Wire.endTransmission was an error and I should have read the code more carefully, this is something I trip up on a lot.

I know that there was no loop function on this code my code did however have the loop although I should have put it in the code for other to easily compile.

I think that the error I have made then was to try and be smart and
The uint16_t which is an unsigned 16bit value. I will change this tonight and try again.

Another silly question how was the screen print added for the serial monitor??

Many thanks
Adam