Go Down

Topic: Help with TMP37 temperature sensor (Read 1 time) previous topic - next topic

Magicj

I always consider Google to be my friend :)

I am not sure where this equation came from

Code: [Select]
float temperatureC = (voltage - 0.5) * 200 ;


Not sure why you are subtracting the 0.5, nor why you are multiplying by 200 but I must admit to not looking too closely.


Have a read of http://shop.moderndevice.com/products/temperature-sensor or http://www.bristolwatch.com/arduino/arduino_tmp37.htm and, there is also a library available http://code.google.com/p/arduino-tmp-temperature-sensor-library/

Hope this helps.

HaLo2FrEeEk

#6
Feb 02, 2013, 03:00 am Last Edit: Feb 02, 2013, 03:49 am by HaLo2FrEeEk Reason: 1
To be honest, I'm not completely sure why I'm subtracting .5 either...I just thought about it. The article I was reading was about the TMP36, which reads 500mV at 0°C, so that's why they subtracted .5 (.5V = 500mV), but the TMP37 reads 500mV at 25°C, and has an interval of 20mV/°C, 500/20 = 25, which means that at 0°C, the reading is 0mV, so I should just take the mV reading and divide by 20.

The reason I'm multiplying by 200 is because, in the sketch I'm using, the voltage is calculated to actual volts, not millivolts (.65 instead of 650, for example), but even now that doesn't make sense to me...I've confused myself.

I think I need to change my sketch to calculate actual mV and not volts.

Code: [Select]
int reading = analogRead(A0);
double mv = reading * ([input voltage] / 1024); // To convert the analog 0-1023 value to a mV value relative to the input voltage
double tempC = mv / 20;
double tempF = (tempC * 9 / 5) + 32;


Edit: Read through the second link you posted (the one from bristolwatch.com) and the sketch used in that example is awful. Tons of errors that needed to be fixed, and when I finally got it working it says it's 481°F in my room. Also looked at the library, also bad, said that it was like 302°F (and I did use setARefVoltage(3.3), so it wasn't that)). I'm not sure what the heck the problem is here, but I'm starting to get frustrated because I know that it's not 62°F in my room, which is what my modified code (from above in this post) is saying.

Magicj

What result does this sketch provide (from the first link above)

Code: [Select]
/*TMP37.pde 
* Arduino Sketch to read a TMP37 Temperature Sensor
* Paul Badger
* 2010
* Sensor works between 5 degrees & 100 degrees C
* 20 mV / degree C
* Can be used with TMP35 & TMP36 by changing voltsPerDegree
*/

float voltsPerDegree = 0.02; // change to 0.01 for TMP35 & 36

void setup() {
    Serial.begin(9600);
}

void loop() {
    int sensorValue;
    float volts;
    float celsius;
    float farenheit;

    sensorValue = analogRead(0);
    volts = sensorValue * 5.0 / 1024.0;            // convert AD units to volts
    // convert volts to celsius
    celsius = (sensorValue * 5.0 / 1024.0) / voltsPerDegree;
    // standard conversion from celsius to farenheit
    farenheit = (((sensorValue * 5.0 / 1024.0) / voltsPerDegree)  * 9.0 / 5.0) + 32;

    Serial.print(sensorValue, DEC);
    Serial.print(" A/D units   ");
    Serial.print(volts);
    Serial.print(" volts   ");
    Serial.print(celsius);
    Serial.print(" degrees C   ");
    Serial.print(farenheit);
    Serial.println(" degrees F");
}

HaLo2FrEeEk

I think I've figured it out. My math for the C to F conversion was wrong (backwards), so I was getting the right C temperature but the wrong F temp.

Since the TMP37 reads 500mV at 25° and has a scale of 20mV/°C, that means that 0°C is 0mV, so I didn't need to subtract the .5 (500 mV), just divide the calculated mV by 20. Right now it's reading 30.29°C/86.53°F in my room, which feels right (I said before I really hate cold). Final sketch:

Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
#define aref_voltage 3.3

int tempPin = 1;
long tempReading;
float avgTemp;

void setup(void) {
  analogReference(EXTERNAL);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
}

void loop(void) {
  for(int i = 0; i < 400; i++) {
    tempReading += analogRead(tempPin);
    delay(1);
  }
 
  avgTemp = tempReading / 400;
 
  double mv = avgTemp * ((aref_voltage * 1000) / 1024);
  double tempC = mv / 20;
  double tempF = (tempC / 5 * 9) + 32.0;
  lcd.clear();
  lcd.print(tempC);
  lcd.print(" C");
  lcd.setCursor(0, 1);
  lcd.print(tempF);
  lcd.print(" F");
  tempReading = 0;
  delay(1000);
}


I'm sampling the sensor 400 times then averaging the reading, then converting that to a mV value by multiplying by ((aref_voltage * 1000) / 1024), then dividing by 20 to get °C. The reason I do aref_voltage * 1000 is because aref_voltage is 3.3, I need 3300. All seems well now.

Magicj


Go Up