TMP36 temp is correct in one sketch, not in another...

Hi all,

So I’ve got a sketch that simply takes the temp from my TMP36 sensor and plonks it onto my LCD.

The trouble is, with this particular sketch the temperature reads wrong. About 8-10 degrees C too high.

I can’t see that it’s an issue with the programming for the sensor, and it’s not a wiring issue. If I upload a ‘simpler’ sketch that simply takes the reading and outputs it over serial, it’s correct.

Can anyone maybe spot why?

Here’s the sketch. It’s very rough 'n ready, sorry :slight_smile:

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Time.h>
int temperaturePin = 0;
int LED=9;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define   CONTRAST_PIN   9
#define   BACKLIGHT_PIN  7
#define   CONTRAST       100

// Creat a set of new characters
byte smiley[8] = {
  0b00000,
  0b01010,
  0b01010,
  0b00000,
  0b00000,
  0b10001,
  0b01110,
  0b00000
};

  byte all[8] = {
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

void setup()
{
  // Switch on the backlight and LCD contrast levels
  pinMode(CONTRAST_PIN, OUTPUT);
  pinMode(BACKLIGHT_PIN, OUTPUT);

  digitalWrite(BACKLIGHT_PIN, HIGH);
  analogWrite (CONTRAST_PIN, CONTRAST);
  lcd.begin(20,4);               // initialize the lcd 

  lcd.createChar (0, smiley);    // load character to the LCD
  lcd.createChar (3, all);   // load character to the LCD

lcd.home ();                   // go home
lcd.print ("Hello");
lcd.setCursor (7,0);
lcd.print(char(0));  
lcd.setCursor (0,2);
lcd.print("Inside  Temperature:");
lcd.setCursor (0,1);
lcd.print (char(3));
lcd.setCursor (9,1);
lcd.print (char(3));
lcd.setCursor (19,1);
lcd.print (char(3));

  
}

void loop(){

float temperature = getVoltage(temperaturePin);
temperature = (temperature - 0.5) * 100;  



lcd.setCursor (7, 3);
lcd.print (temperature);
lcd.setCursor (12,3);
lcd.print ("c");

if (temperature>= 25)
 {
digitalWrite (LED, HIGH);
 }
 else
 {
   digitalWrite (LED, LOW);
 }
delay(1000);
}
float getVoltage(int pin){
return (analogRead(pin) * .004882814);
}

OK so I started afresh with the sketch from the LadyAda tutorial and that shows the right temp through serial. Fine and dandy.

Then I start adding the includes and the LCD initialisation stuff…

And it’s when I add this line:

lcd.begin(20,4);

That it goes awry.

What the…?

What are you seeing, and what do you expect to see?

~30. It should be nearer ~22. Room temperature, in other words. If I load in one of the simple TMP36 example sketches, it's fine

According to the library reference, lcd.print doesn't take float. Try casting temperature to int when you call it. Assuming that the temperature pin is actually Analog 0, can you use some Serial prints to see what you're really getting for temperature?