Go Down

Topic: Trouble regarding usage of TMP36 sensor (Read 441 times) previous topic - next topic

adeelshams

I am using a TMP36 temperature sensor along with Arduino Uno to display the temperature on the Serial Monitor. It displays the temperature, but it is gibberish, such as -19 C and -25 C. I have modified my code several time to resolve the discrepancy, but without success. Here's my code:-

const int temp=14;
void setup()
{
  Serial.begin(9600);
  pinMode(temp,INPUT);
}
void loop()
{
  int reading;
  reading=analogRead(temp);
  float voltage;
  voltage=(reading*5);
  voltage=voltage/1024;
  float sense;
  sense=(voltage-0.5)*100;
  Serial.print(sense);
  Serial.println(" C ");
  delay(1000);
}
Please try to help me resolve the error over here.
"Efforts and courage are not enough without purpose and direction" - John F. Kennedy

robtillaart


maybe you should use A0 instead of 14?
furthermore you can shorten your code a bit by combining expressions.

Code: (not tested) [Select]

const int temp=A0;

void setup()
{
  Serial.begin(9600);
  pinMode(temp,INPUT);
}

void loop()
{
  int reading = analogRead(temp);

  float voltage = reading*5.0 / 1024;

  float sense = (voltage-0.5) * 100;

  Serial.print(sense);
  Serial.println(" C ");
  delay(1000);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Wawa

Try this with the sensor output connected to A1.
Leo..
Code: [Select]

// TMP36 temp sensor connected to Analogue-in 1, +5volt and ground
// ~-50 to ~+150 degrees C
//
unsigned int total;
float tempC;
float tempF;
//
void setup() {
  //analogReference(INTERNAL2V56); // ---uncomment this line is for a MEGA---
  Serial.begin(9600); // set serial monitor to this value
}
//
void loop() {
  analogRead(1); // one unused reading
  for (int x = 0; x < 20; x++) { // 20 readings for averaging
    total = total + analogRead(1); // add each value to a total
  }
  Serial.print("Raw average = "); // debugging
  Serial.print(total * 0.05, 1); // 1/20 of 20 readings, one decimal place

  // leave ONE of these three lines uncommented

  //tempC = total * 3.3 * 5 / 1024 - 50; // ---this line is for 3.3volt Arduinos---
  tempC = total * 5.0 * 5 / 1024 - 50; // ---this line is for 5volt Arduinos---
  //tempC = total * 2.56 * 5 / 1024 - 50; // ---this line is for a MEGA with 2.56volt Aref enabled---

  // calibration: change voltages to actual voltage for accurate temps, e.g. 5.0 to 5.136

  tempF = tempC * 1.8 + 32; // Fahrenheit conversion
  Serial.print("   The temperature is  ");
  Serial.print(tempC, 1); // one decimal place
  Serial.print(" Celcius  ");
  Serial.print(tempF, 0); // no decimal place
  Serial.println(" Fahrenheit");
  delay(1000); // slows readings
  total = 0; // reset value
}

Go Up