Reading the TMP36GT9 temperature sensor

Reading from the sensor

This is a little code snippet that can be used to read the TMP36GT9 temperature sensor with an electric imp and get it to return the temperature in Celsius.

// Reading the TMP36GT9 temperature sensor
function getTemp()
    // configure pin 9 to analog input (this is not needed to do every time but we do it for a easy modular function)

    // get the raw voltage value from temp sensor (0-65535)
    local reading =;

    // needs mapping to the the voltage we are getting from the regulator
    local voltage = reading * hardware.voltage();

    // divide by the raw value range
    voltage /= 65535.0;

    // subtract offset and multiply by 100
    return (voltage - 0.5) * 100;

To connect the sensor you only need 3 wires, one for 3.3V, one for GND, and then the wire with the voltage from the sensor, which this code snippet is fetching from pin 9.

imp_tmp36Average the temperature

I found the readings from the sensor to jump around quite a bit (still within 1c), and to get that a bit more smooth I am using this snippet.

// Collect the temperature and calculate an average
avgTemperature <- getTemp();
function avgTemp()
  avgTemperature += getTemp();
  avgTemperature /= 2;
  imp.wakeup(20, avgTemp);

By adding this, the temperature from the getTemp() function will be read by avgTemp() and stored in avgTemperature, this function will then repeat over and over with a 20 second delay. (I have found the 20 second delay to be good when posting the temperature every 60 seconds)

If you want to use this average temperature, you need to get it from avgTemperature instead of getTemp(), and that could be done like this.

server.log("Temperature: " + avgTemperature + "c");

4 thoughts on “Reading the TMP36GT9 temperature sensor”

Leave a Comment