# Weird TMP36 results

Hi all,

I am using a TMP36 Temperature sensor but getting some really strange results…prob me being silly but not only am I seeing large temperatures (109 deg C apparently) the sensor also seems to work in reverse i.e. when it should get colder it says its getting hotter and visa versa.

I suspect I have the code wrong…see below (which was copied from somewhere)

``````#include <math.h>
const int PULLUP_RES = 100000; // in Ohm( 100kOm )
const double BETA = 4390; // in K for Semitec 104 GTA-2
const double THERMISTOR_RES = 100000; // in Ohm
const double THERMISTOR_NOM_TEMP = 25; // Celsius, C
void setup()
{
}
void loop()
{
delay(1000);
}
void thermister_temp(int aval)
{
double R, T;
R = (double) PULLUP_RES / ( (4095 / (double) aval ) - 1 );

T = 1 / ( ( 1 / (THERMISTOR_NOM_TEMP + 273.15 )) + ( ( 1 / BETA) * log ( R / THERMISTOR_RES ) ) );

T -= 273.15; // converting to C from K

// return degrees C
Spark.publish("Temperature", String(T) + " °C");
}
``````

Any help appreciated.

@Nnpnew the TMP36 is a bit finicky so check this thread out to see how to improve your results

EDIT: Looking at your code, yeah that’s for a thermistor. Check out the code in the thread I mentioned

1 Like

4 posts were split to a new topic: Trouble using NTC thermisistor

This is the formula I use for the TMP36:

``````float getTempF() {

// 12-bit precision. 0 = 0V, 4095 = 3.3V, 0.0008 volts (0.8 mV) per unit
// The temperature sensor docs use millivolts (mV), so use 3300 as the factor instead of 3.3.
float mV = ((float)tempValue) * 3300 / 4096;

// According to the TMP36 docs:
// Offset voltage 500 mV, scaling 10 mV/deg C, output voltage at 25C = 750 mV (77F)
// The offset voltage is subtracted from the actual voltage, allowing negative temperatures
// with positive voltages.

// Example value=969 mV=780.7 tempC=28.06884765625 tempF=82.52392578125

// With the TMP36, with the flat side facing you, the pins are:
// Vcc | Analog Out | Ground

float tempC = (mV - 500) / 10;

float tempF = (tempC * 9) / 5 + 32;

return tempF;
}
``````