Help with temperature sensor using mcp9700

My wiring is as follows:

Arduino 5V and GND to respective + and - rails.

Left pin of the MCP9700 (from flat side) to 5v, middle pin to A0 of arduino, right pin to GND.

Left and right pins of 10k potentiometer respectively at GND and 5v, middle pin to the V0 of LCD.

In the LCD, my VSS is to GND, VDD to 5V, RS to arduino pin 12, R/W to GND, E to arduino pin 11, DB4 to arduino pin 5, DB5 to arduino pin 4, DB6 to arduino pin 3, DB7 to arduino pin 2, LED+ to 220 ohm resistor to 5V, LED- to GND.

The LCD I am using is 16x2, and the temperature is all in celsius.

My code is as follows:

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 =2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16, 2); //rows, colums
  lcd.print("Temperature Sensor");

}

void loop() {
  // put your main code here, to run repeatedly:
  int value = analogRead(A0); //read from analogue pin
  float voltage = value*(5.0/1024.0); //resolution from 0 to 1023, where 1024 is 5v and 0 is 0v
  float temp = (voltage - 0.5) * 100.0; //celsius has voltage offset of 500mV

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(temp);
  lcd.print("C");

  delay(1000);


}

Originally, I had thought the offset was 400mV, and the temperature displayed on the LCD screen was 37.64 celsius which is way too high. I used an offset of 500mV this time but the temperature on the LCD changed to 27.64 C which is still unusually high for the room temperature.

Also, when I used a fan near the project in order to see if the temperature would go any lower, it instead went up a degree.

Could anyone take a look at this and suggest what might be wrong?

As a reference, the temperature should be at least 25 degrees or less. Is it normal to be off a few degrees?

That's the MCP9701. The MCP9700 has an offset of 500mV (or 50 degrees C), with a temp coefficient of 10mV/C. Which is the same as a TMP36, so the same code can be used.

Analogue sensors are sensitive to grounding, so a dedicated ground wire to the Arduino is needed (no sharing with the LCD or breadboard).
Accurate readout and resolution also depends on which Arduino, which you forgot to tell us.
Here is a test sketch for an Uno R3.
Leo..

const byte tempPin = A0; // connect TPM36 to 3.3volt A0 and (not-shared) ground
float calibration = 0.1039; // calibrate temp by changing the last digit(s) of "0.1039"
float tempC;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use internal 1.1volt Aref
}

void loop() {
  tempC = (analogRead(tempPin) * calibration) - 50.0;
  Serial.print("Temperature:  ");
  Serial.print(tempC, 1); // one correct decimal place is all you get
  Serial.print(" C");
  delay(1000); // use a non-blocking delay when combined with other code
}

If you are using a Nano powered from USB, your AREF voltage may only be 4.7V instead of 5, there's your error.

I am using an Uno R3.

However, it seems like my problem was just time as the temperature did go down to 24.7C, which was close enough to the room temperature.

I did change it to a 500mV calibration in my code so it seems to work alright now. The datasheet also suggests an uncertainty of +/- 2 degrees C so this fits in with the room temperature.

Could you please explain the calibration value of 0.1039 as I am not sure where that number comes from.

Ahh, you were squeezing it in them hot little fingers. :wink:

I’ve finally managed to figure out the code you’ve written, thanks for the help in getting it more precise. Previously with the 5v reference the temperature could only go up in .3 degrees, now it can go up in .1 degrees.

Good to hear you have it sorted.
Analogue sensors are only good for learning (how to do it differently).

A digital factory-calibrated sensor, like the DS18B20, is much easier to use.
Accurate to 0.5C and a resolution of 0.0625C.
Leo..