MPU6050 - strange values
Posted: Fri Feb 03, 2017 4:51 pm
Hi,
I'm trying to use MPU6050 accelerometer, however I'm getting strange values. I have the accelerometer connected to ESP8266 over level shifter (so the accelerometer could run at 5V).
What I was able to find yet(but not sure if it's true):
The accelerometer needs to be initialized by sending 2 bytes, 0x6B and 0x00.
The accelerometer's data are readable after sending 0x3B byte, and I have to read 14 bytes - 2 bytes for each of 7 values in this order - Accelerometer X, Y, Z, Temperature, Gyroscope X, Y, Z.
At I2CDevLib forums, someone said that the value is calculated by this formula:
int16_t accel_x = (ACCEL_XOUT_H << 8) + ACCEL_XOUT_L;
(when the H is the first byte, L the second. Same for all 7 values, only temperature is then divided by 340 and 36 is added)
When I read and calculate these values (see code below), I'm getting random values in range 0-65535(int16 range). However the values I get looks random and I can't see any similarities to how I'm moving the mpu6050 (I even created simple gui app to visualize these numbers to be sure). And also, my room temperature is 220°C, according to the mpu6050.
So in case there is just something wrong with the accelerometer, so I connected it to the Arduino and used this sketch http://playground.arduino.cc/Main/MPU-6 ... lock&num=1 however everything is working fine here. The values I'm getting are as expected according to the mpu6050 movement and the temperature is also ok, so there must be error in my code, propably something with the calculation or, what I think, I'm reading from wrong address.
Could you please help me?
Thanks.
Thys is my mpu6050.py
import machine
class accel():
def __init__(self):
self.iic = machine.I2C(machine.Pin(5), machine.Pin(4))
self.iic.start()
self.iic.writeto(0x68,bytearray([107,0]))
self.iic.stop()
def get_raw_values(self):
self.iic.start()
self.iic.writeto(0x68, bytearray([0x3B]))
self.iic.stop()
self.iic.start()
a = self.iic.readfrom(0x68,14)
return a
def get_values(self):
raw_ints = self.get_raw_values()
vals = {}
vals["AcX"] = (raw_ints[0] <<8)+ raw_ints[1]
vals["AcY"] = (raw_ints[2] <<8)+ raw_ints[3]
vals["AcZ"] = (raw_ints[4] <<8)+ raw_ints[5]
vals["Tmp"] = ((raw_ints[6] <<8)+ raw_ints[7])/340.00+36.53
vals["GyX"] = (raw_ints[8] <<8)+ raw_ints[9]
vals["GyY"] = (raw_ints[10] <<8)+ raw_ints[11]
vals["GyZ"] = (raw_ints[12] <<8)+ raw_ints[13]
return vals
And this is the output:
>>> import mpu6050
>>> acl = mpu6050.accel()
>>> acl.get_values()
{'GyZ': 65326, 'GyY': 203, 'GyX': 65357, 'Tmp': 219.3064, 'AcZ': 58600, 'AcY': 50720, 'AcX': 928}
>>> acl.get_values()
{'GyZ': 65374, 'GyY': 177, 'GyX': 116, 'Tmp': 219.3535, 'AcZ': 58284, 'AcY': 50444, 'AcX': 872}
>>> acl.get_values()
{'GyZ': 65304, 'GyY': 218, 'GyX': 65310, 'Tmp': 219.3535, 'AcZ': 59112, 'AcY': 51244, 'AcX': 976}
>>> acl.get_values()
{'GyZ': 65398, 'GyY': 179, 'GyX': 60, 'Tmp': 219.3535, 'AcZ': 58460, 'AcY': 50508, 'AcX': 1000}
>>> acl.get_values()
{'GyZ': 65312, 'GyY': 211, 'GyX': 65326, 'Tmp': 219.3535, 'AcZ': 58644, 'AcY': 50904, 'AcX': 980}
>>> acl.get_values()
{'GyZ': 65335, 'GyY': 198, 'GyX': 65482, 'Tmp': 219.3064, 'AcZ': 58704, 'AcY': 51056, 'AcX': 940}
>>> acl.get_values()
{'GyZ': 65384, 'GyY': 196, 'GyX': 88, 'Tmp': 219.2593, 'AcZ': 58560, 'AcY': 50832, 'AcX': 848}
>>> acl.get_values()
{'GyZ': 65373, 'GyY': 181, 'GyX': 62, 'Tmp': 219.3535, 'AcZ': 58728, 'AcY': 50896, 'AcX': 1000}
>>> acl.get_values()
{'GyZ': 65402, 'GyY': 164, 'GyX': 260, 'Tmp': 219.3064, 'AcZ': 58436, 'AcY': 50608, 'AcX': 1008}
>>> acl.get_values()
{'GyZ': 65294, 'GyY': 215, 'GyX': 65274, 'Tmp': 219.2593, 'AcZ': 58768, 'AcY': 51040, 'AcX': 912}
I'm trying to use MPU6050 accelerometer, however I'm getting strange values. I have the accelerometer connected to ESP8266 over level shifter (so the accelerometer could run at 5V).
What I was able to find yet(but not sure if it's true):
The accelerometer needs to be initialized by sending 2 bytes, 0x6B and 0x00.
The accelerometer's data are readable after sending 0x3B byte, and I have to read 14 bytes - 2 bytes for each of 7 values in this order - Accelerometer X, Y, Z, Temperature, Gyroscope X, Y, Z.
At I2CDevLib forums, someone said that the value is calculated by this formula:
int16_t accel_x = (ACCEL_XOUT_H << 8) + ACCEL_XOUT_L;
(when the H is the first byte, L the second. Same for all 7 values, only temperature is then divided by 340 and 36 is added)
When I read and calculate these values (see code below), I'm getting random values in range 0-65535(int16 range). However the values I get looks random and I can't see any similarities to how I'm moving the mpu6050 (I even created simple gui app to visualize these numbers to be sure). And also, my room temperature is 220°C, according to the mpu6050.
So in case there is just something wrong with the accelerometer, so I connected it to the Arduino and used this sketch http://playground.arduino.cc/Main/MPU-6 ... lock&num=1 however everything is working fine here. The values I'm getting are as expected according to the mpu6050 movement and the temperature is also ok, so there must be error in my code, propably something with the calculation or, what I think, I'm reading from wrong address.
Could you please help me?
Thanks.
Thys is my mpu6050.py
import machine
class accel():
def __init__(self):
self.iic = machine.I2C(machine.Pin(5), machine.Pin(4))
self.iic.start()
self.iic.writeto(0x68,bytearray([107,0]))
self.iic.stop()
def get_raw_values(self):
self.iic.start()
self.iic.writeto(0x68, bytearray([0x3B]))
self.iic.stop()
self.iic.start()
a = self.iic.readfrom(0x68,14)
return a
def get_values(self):
raw_ints = self.get_raw_values()
vals = {}
vals["AcX"] = (raw_ints[0] <<8)+ raw_ints[1]
vals["AcY"] = (raw_ints[2] <<8)+ raw_ints[3]
vals["AcZ"] = (raw_ints[4] <<8)+ raw_ints[5]
vals["Tmp"] = ((raw_ints[6] <<8)+ raw_ints[7])/340.00+36.53
vals["GyX"] = (raw_ints[8] <<8)+ raw_ints[9]
vals["GyY"] = (raw_ints[10] <<8)+ raw_ints[11]
vals["GyZ"] = (raw_ints[12] <<8)+ raw_ints[13]
return vals
And this is the output:
>>> import mpu6050
>>> acl = mpu6050.accel()
>>> acl.get_values()
{'GyZ': 65326, 'GyY': 203, 'GyX': 65357, 'Tmp': 219.3064, 'AcZ': 58600, 'AcY': 50720, 'AcX': 928}
>>> acl.get_values()
{'GyZ': 65374, 'GyY': 177, 'GyX': 116, 'Tmp': 219.3535, 'AcZ': 58284, 'AcY': 50444, 'AcX': 872}
>>> acl.get_values()
{'GyZ': 65304, 'GyY': 218, 'GyX': 65310, 'Tmp': 219.3535, 'AcZ': 59112, 'AcY': 51244, 'AcX': 976}
>>> acl.get_values()
{'GyZ': 65398, 'GyY': 179, 'GyX': 60, 'Tmp': 219.3535, 'AcZ': 58460, 'AcY': 50508, 'AcX': 1000}
>>> acl.get_values()
{'GyZ': 65312, 'GyY': 211, 'GyX': 65326, 'Tmp': 219.3535, 'AcZ': 58644, 'AcY': 50904, 'AcX': 980}
>>> acl.get_values()
{'GyZ': 65335, 'GyY': 198, 'GyX': 65482, 'Tmp': 219.3064, 'AcZ': 58704, 'AcY': 51056, 'AcX': 940}
>>> acl.get_values()
{'GyZ': 65384, 'GyY': 196, 'GyX': 88, 'Tmp': 219.2593, 'AcZ': 58560, 'AcY': 50832, 'AcX': 848}
>>> acl.get_values()
{'GyZ': 65373, 'GyY': 181, 'GyX': 62, 'Tmp': 219.3535, 'AcZ': 58728, 'AcY': 50896, 'AcX': 1000}
>>> acl.get_values()
{'GyZ': 65402, 'GyY': 164, 'GyX': 260, 'Tmp': 219.3064, 'AcZ': 58436, 'AcY': 50608, 'AcX': 1008}
>>> acl.get_values()
{'GyZ': 65294, 'GyY': 215, 'GyX': 65274, 'Tmp': 219.2593, 'AcZ': 58768, 'AcY': 51040, 'AcX': 912}