This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMP275: convert 2 bytes of data to temperature

Part Number: TMP275

Good afternoon,

I was hoping you could clarify how to convert the 2 bytes of temperature data to a temperature? It appears that the second byte is fractional temperature (according to a previous post). I see Table 1 but that gives a 12 bit binary number, and we are receiving 16 bits. Any information is helpful.

Thank you 

  • Hello J.P.,

    You can do this a few ways. The simplest is if you interpret all 16 bits of temperature data as a single 2s Complement number, you can divide by 256 (2^8) to return the full 12 bits including fractional data as degrees C. Your code could look something like this:

    //Holds bytes read over I2C
      uint8_t temp[2];
    
    //Holds 16-bit temperature read as a single number
      int16_t tempc;
    
    //read out the data
      temp[0] = I2C.readByte(); //MSB
      temp[1] = I2C.readByte(); //LSB
    
    //combine to make one 16 bit 2s Complement number
      tempc = ((temp[0] << 8) | temp[1]);
    
    //Convert to celcius (0.0625C resolution) and return
    //Have to typecast as a float/double to avoid losing 
    //fractional portion.
      return (double)tempc/256;

    Let me know if you have any other questions.

    Best Regards,
    Brandon Fisher

  • i dont believe /256 gets us the correct answer. it appears that /16 is what is needed.
  • Hi J.P.,

    You are correct looking at table 1 in the datasheet, but bear in mind that it only considers the most significant 12 bits of temperature data.

    Dividing by 16 (/2^4) will get you the correct answer if you interpret the whole 16 bit number as a 12 bit. You could do this by shifting out the 4 least significant bits after you combine the two bytes you read, and then dividing by 16 to get temperature in Celsius. Dividing by 256 is just a short way to take both these steps at once.

    Best Regards,
    Brandon Fisher