I tried to work with the mpu6050 gyroscope/ accelerometer. I got the code from the internet but the output from the sensor all show -1. I tried to rotate the sensor in the x and y direction but the values do not change. The pins are all soldered and the power stays on.
Pin Connections
Vcc - 5V
GND - GND
SDA - A4
SCL - A5
code:
#include <MPU6050.h>
#include <Wire.h>
const int MPU = 0x68;
int16_t AcX, AcY, AcZ, GyX, GyY, GyZ, temp;
char tmp_str[7];
char* convert_int16_to_str(int16_t i){
sprintf(tmp_str, "%6d", i);
return tmp_str;
}
void setup(){
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,14,true);
//Accelerometer values
AcX = Wire.read()<<8|Wire.read();
AcY = Wire.read()<<8|Wire.read();
AcZ = Wire.read()<<8|Wire.read();
temp = Wire.read()<<8 | Wire.read();
//Gyroscope values
GyX = Wire.read()<<8|Wire.read();
GyY = Wire.read()<<8|Wire.read();
GyZ = Wire.read()<<8|Wire.read();
Serial.print("acX = "); Serial.print(convert_int16_to_str(AcX));
Serial.print(" |acY = "); Serial.print(convert_int16_to_str(AcY));
Serial.print(" |acZ = "); Serial.print(convert_int16_to_str(AcZ));
Serial.print("gyX = "); Serial.print(convert_int16_to_str(GyX));
Serial.print(" |gyY = "); Serial.print(convert_int16_to_str(GyY));
Serial.print(" |gyZ = "); Serial.print(convert_int16_to_str(GyZ));
Serial.print("\n");
delay(100);
}
Output:
acX = -1 |acY = -1 |acZ = -1 gyX = -1 |gyY = -1 |gyZ = -1
acX = -1 |acY = -1 |acZ = -1 gyX = -1 |gyY = -1 |gyZ = -1
acX = -1 |acY = -1 |acZ = -1 gyX = -1 |gyY = -1 |gyZ = -1
Any suggestion?