Weird Analog Readings From TMP

Hi,

I have been using the TMP36 sensor and I have been getting weird readings. I have my circuit set up using the TMP 36 datasheet and nothing else. Here is my code :

int tempSensor=A0;
int volRead;
float vol;
float temp;
float tempF;

void setup() {
    pinMode(tempSensor,INPUT);
    Spark.variable("tempF",&tempF,DOUBLE);
}

void loop() {
    volRead=analogRead(tempSensor);
    vol= volRead*(3300/1024);
    temp=(vol-500)/10;
    tempF=temp*(9/5)+32;
}

Here are the results and note the weird voltage:

{
  "cmd": "VarReturn",
  "name": "tempF",
  "result": 2.364143650983172e-308,
  "coreInfo": {
    "last_app": "",
    "last_heard": "2015-10-02T01:32:48.078Z",
    "connected": true,
    "last_handshake_at": "2015-10-02T01:21:01.942Z",
    "deviceID": "54ff70066678574911450767"
  }
}

What’s going on?

That’s an old C problem.
Very brief:
Even if you lvalue is of a floating point type, the resulting type of the calculation is derived from the parameters.
And since almost all your parameters are integer types you will get rather small results.

Try this instead

void loop() {
    volRead = analogRead(tempSensor);
    vol = volRead * (3300.0/4095.0); // Particles have 12 bit vs Arduino 10bit 1024
    temp = (vol-500.0) / 10.0;
    tempF = temp * (9.0/5.0) + 32.0;

   delay(100); // give the ADC some slack ;-)
}

Perfect now I get normal voltage readings because of my parameters. However, I am getting weird temperatures. My room is at 74 degrees F and i’m getting 50. Any idea what could be going on?

Could you try another pin and also add some Serial.print() statement to get the raw analog reading of the pin.
You might also not quite have 3300.0mV as reference. You could measure the voltage and replace 3300.0 with your reading.

And here are also some tips and foremost add a capacitor and/or a 1k resistor (read posts #4 and #7)
Cannot read TMP36 values correctly, please help