Strange values from TMP102

So awhile back with quite a bit of guidance I was able to get my TMP102 sensors working.

I fired them up again today and have been getting strange results. I’m sure it’s just user error, but I’m not sure where to look.

Here’s my code,

// For the TMP102 sensor
int tmp102Address=0x48;
float celsius = 0;

void setup() {

void loop() {
    float celsius = getTemperature();

float getTemperature() {
    byte MSB=0;
    byte LSB=0;
    MSB =;
    LSB =;
    int16_t TemperatureSum = ((MSB << 8) | LSB) >> 4;
    float celsius = TemperatureSum*0.0625;
    return celsius;

When I run the following:


This is what I get back,

  "cmd": "VarReturn",
  "name": "temperature",
  "result": 4.090223858389538e-270,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-09-14T02:06:43.643Z",
    "connected": true,
    "deviceID": "<snip>"

My question is why am i getting those strange values in result?

Just a quick glance, but in your loop function you have:
float celsius = getTemperature();

But you already declared celsius at the top of your code, and that is the one you need to set the temperature to since that is the one you made a ‘variable’. You are defining a new variable in loop(), so you should change the code in loop() to this:
celsius = getTemperature();

As for the strange value you get back, that is probably from float precision, it is really 0 which is what you set it to in the beginning.

one more thing, i tried this out this morning, and it doesn’t seem to like it when you have a ‘float’ datatype but feed it to the variable function as a DOUBLE. even when i fixed the loop, i still got bad values.

this seemed to work though, changing the datatype to a double:

nt tmp102Address=0x48;
double celsius = 0;

void setup() {

void loop() {
    celsius = getTemperature();

@eely22…sorry about my slow response. That did it for me as well.

Thanks for the help!

1 Like