MPU6050 - strange values

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
adamjezek
Posts: 2
Joined: Thu Feb 02, 2017 12:50 am

MPU6050 - strange values

Postby adamjezek » 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}

User avatar
pythoncoder
Posts: 1564
Joined: Fri Jul 18, 2014 8:01 am

Re: MPU6050 - strange values

Postby pythoncoder » Sat Feb 04, 2017 7:32 am

Have you looked at https://github.com/micropython-IMU? There are drivers for the MPU9150 and MPU9250 here https://github.com/micropython-IMU/micropython-mpu9x50. If I remember correctly the MPU6050 is similar to the MPU9150 but lacks the magnetometer chip. The base class in imu.py is close to what you need. A forum search on MPU6050 indicates that others have had success with this.
Peter Hinch

adamjezek
Posts: 2
Joined: Thu Feb 02, 2017 12:50 am

Re: MPU6050 - strange values

Postby adamjezek » Sat Feb 04, 2017 5:47 pm

[quote="pythoncoder"]Have you looked at [url]https://github.com/micropython-IMU[/url]? There are drivers for the MPU9150 and MPU9250 here [url]https://github.com/micropython-IMU/micropython-mpu9x50[/url]. If I remember correctly the MPU6050 is similar to the MPU9150 but lacks the magnetometer chip. The base class in imu.py is close to what you need. A forum search on MPU6050 indicates that others have had success with this.[/quote]
I first didn't wanted to copy it from working library. I managed to find out that my reading was wrong. However, I was still getting strange values, so I look into the MPU9X50 library and figured out I was just wrongly calculating the numbers. Btw, wasn't this library created by you?


If anybody will want to use the code above, you just have to change few parts:
def get_raw_values(self):
self.iic.start()
a = self.iic.readfrom_mem(0x68, 0x3B, 14)
self.iic.stop() #start and stop maybe not neccessary, too lazy to test and it's working like this
return a

And in get_values, the calculations aren't
result =( firstbyte << 8)+| secondbyte
It has to be changed to this: (better write some function for that)
if not firstbyte & 0x80:
result = firstbyte << 8 | secondbyte
else
result = - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1)

the temperature still has to be divided after it

User avatar
pythoncoder
Posts: 1564
Joined: Fri Jul 18, 2014 8:01 am

Re: MPU6050 - strange values

Postby pythoncoder » Sun Feb 05, 2017 6:31 am

adamjezek wrote:...Btw, wasn't this library created by you?...
It was a collaboration between myself and Sebastian Plaumauer (@turbinenreiter). He wrote the original MPU9150 library. I extended it to enable allocation-free access and support for the MPU9250. I wrote the vector3d module and ported the sensor fusion module from C. The other modules in https://github.com/micropython-IMU are Sebastian's work.
Peter Hinch


Return to “Drivers for External Components”

Who is online

Users browsing this forum: No registered users and 1 guest