# Getting temp from MPU6050 that is returned in 2's compliment

#### KrisKasprzak

##### Well-known member
All,

I'm using an MPU605 with Teensy 3.2 and I'm trying to get the temp, which is returned in 16-bit 2's compliment. I've found some explanations on converting but my conversions are way off.

Code:
``````int16_t MPU6050::getTemperature() {
return (((int16_t)buffer[0]) << 8) | buffer[1];
}``````

My MCU and MPU605 are currently at around 73 deg f and the MPU6050 is returning -2857 or so.

Anyone have some code to go from 16-bit 2's compliment to it's decimal?

That actually looks correct to me. Look at the register map and the formula is:

Temperature in degrees C = (TEMP_OUT Register Value as a signed quantity)/340 + 36.53

For your example, I get 28 C, about 83 F. Considering it's die temperature, I would expect it to read a little higher than room temp.

Edit, here's a link to the register map for that IMU:

Also, I have a library for working with the InvenSense IMUs - currently it doesn't have the MPU-6050 in it, but shouldn't be hard to adapt to that sensor:
https://github.com/bolderflight/invensense-imu

Twos complement is just the typical number format used by machines.

From the MPU6050 register map and descriptions datasheet:
The temperature in degrees C for a given register value may be computed as:
Temperature in degrees C = (TEMP_OUT Register Value as a signed quantity)/340 + 36.53

-2857 / 340 + 36.53 = 28.127 degrees celsius = 82.63 degrees F.

My reply was beaten while looking stuff up.

Thanks.

I should have consulted the data sheet first. All is fine now.

Kris