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() {
    Spark.variable("temperature",&celsius,DOUBLE);
    
    //Serial.begin(9600);
    Wire.begin();
}

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

float getTemperature() {
    byte MSB=0;
    byte LSB=0;
    Wire.requestFrom(tmp102Address,2);
    
    MSB = Wire.read();
    LSB = Wire.read();
    
    int16_t TemperatureSum = ((MSB << 8) | LSB) >> 4;
    
    float celsius = TemperatureSum*0.0625;
    return celsius;
}

When I run the following:

curl https://api.spark.io/v1/devices//temperature?access_token=

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:
i

nt tmp102Address=0x48;
double celsius = 0;

void setup() {
    Spark.variable("temperature2",&celsius,DOUBLE);
    
    //Serial.begin(9600);
    Wire.begin();
}

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

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

Thanks for the help!

1 Like