Arduino TMP36 Setup

(Arduino Uno R3)

I have seen quite a few questions regarding the setup and accuracy of using the TMP36 temperature sensor. Essentially, the key for me was using the Arduino’s reference voltage to make the setup independent of the voltage source and obtain accurate reading from the sensor. I am also capturing several samples for a more consistent reading.

Setup

You need to connect the 3.3V output to the AREF pin. This will result in readings relative to this reference. Connect the sensor to 5V, GND and an analog pin (I used A0).

Code

The code below can easily be adapted to log the results to an SD card, or display the readings on an LCD.

int sensorPin = A0;

const int samples = 63; // number of samples to obtain average temperature
const float aref_voltage = 3.3;

void setup() {
  analogReference(EXTERNAL);
}

void loop() {
  unsigned int reading = 0;

  // Take several samples over the period of one second
  for (int i = 0; i < samples; i++) {
    reading += analogRead(sensorPin);
    delay(1000 / samples);
  }

  // Average reading
  reading /= samples;

  float voltage = reading * aref_voltage;
  voltage /= 1024.0; 

  // The 0.5V offset for the tmp36
  float temperatureC = (voltage - 0.5) * 100;

  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;

  // Capturing the samples above takes one second,
  // and we delay an additional two for a three-second interval.
  delay(2000);
}

Enjoy!

blog comments powered by Disqus