My basic TMP36 setup measuring works fine, gives stable temperatures, only jumping .30 sometimes, as seen in the serial monitor.
My next build had the lcd panel on the breadboard to show the temp there..now the measured temp jump erratic up and down, sometimes at much as 3 degrees.
As soon as remove the lcd panel, all returns to normal and I get stable measurements again...
Some example code to test.
It uses the internal 1.1volt Aref and smoothing.
Read the comments. You might have to change some things.
Leo..
// Thermometer in Celcius and Fahrenheit
// displays on serial monitor and/or LCD
// TMP35 or TMP36 temp sensor connected to Analogue input A1, 3.3volt and ground
// or LM35 temp sensor connected to A1, 5volt and ground
// TMP35 and LM35 temp range ~+2C to ~+105C
// TMP36 temp range ~-40C to ~+55C
// see line 46 for TMP35/LM35/TMP36 code selection
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // your LCD pins could be different
byte ledPin = 10; // backlight pin
const byte numReadings = 32; // number of readings for smoothing (max 64)
int readings[numReadings]; // readings from the analogue input
byte index = 0; // index of the current reading
unsigned int total = 0; // running total
int inputPin = A1; // the pin that the sensor is connected to
float Aref = 1.0759; // temp calibration | change in small 0.000x steps to the actual Aref voltage of ---YOUR--- Arduino for accurate temp readings
float tempC; // Celcius
float tempF; // Fahrenheit
void setup() {
//analogWrite(ledPin, 200); // optional dimming
analogReference(INTERNAL); // use the internal ~1.1volt reference | change (INTERNAL) to (INTERNAL1V1) for a Mega
Serial.begin(115200); // ---set the serial monitor to this value---
lcd.begin(16, 2); // shield with 2x16 characters
lcd.print("Thermometer"); // info text
lcd.setCursor(0, 1); // second row
lcd.print("0-100 Celcius");
for (index = 0; index < numReadings; index++) { // fill the array for faster startup
readings[index] = analogRead(inputPin);
total = total + readings[index];
}
index = 0; // reset
delay(2000); // info display time
}
void loop() {
total = total - readings[index]; // subtract the last reading
readings[index] = analogRead(inputPin); // one unused reading to clear ghost charge
readings[index] = analogRead(inputPin); // read from the sensor
total = total + readings[index]; // add the reading to the total
index = index + 1; // advance to the next position in the array
if (index >= numReadings) // if we're at the end of the array
index = 0; // wrap around to the beginning
// convert value to temp | leave only one of these two lines uncommented
//tempC = total * Aref * 0.1 / numReadings; // value to celcius conversion for TMP35 or LM35
tempC = total * Aref * 0.1 / numReadings - 50.0; // value to celcius conversion for TMP36
// Celcius to Fahrenheit conversion
tempF = tempC * 1.8 + 32;
// print to LCD
if (total == 1023 * numReadings) { // if overflow
lcd.clear();
lcd.print("---TOO HOT---");
}
else {
lcd.clear();
lcd.print(tempC, 2); // two decimal places
lcd.setCursor(6, 0); // position 6, first row
lcd.print("Celcius");
lcd.setCursor(0, 1); // second row
lcd.print(tempF, 1); // one decimal place
lcd.setCursor(6, 1); // position 6, second row
lcd.print("Fahrenheit");
}
// print to serial monitor
Serial.print("Raw average = ");
Serial.print(total / numReadings);
if (total == 1023 * numReadings) {
Serial.println(" ----too hot----");
}
else {
Serial.print(" The temperature is ");
Serial.print(tempC, 2);
Serial.print(" Celcius ");
Serial.print(tempF, 1);
Serial.println(" Fahrenheit");
}
delay(1000); // use a non-blocking delay when combined with other code
}
Without lcd, the list is like this:
18.36
18.36
18.36
18.56
18.36
18.36
18.36
18.66
I will try your code but principally I am happy with the values I get when I don´t have an lcd on the board; I don´t understand why it changes so much after that
FvanT:
I don´t understand why it changes so much after that
The value you get from the A/D converter depends on two things.
The input voltage (from the temp sensor), and a reference voltage (the ruler that measures it all).
Default reference voltage is the 5volt supply.
If that dips a bit from current variations (e.g. a flashing LED), or voltage variations (USB supply), that variation shows up in the A/D values.
Arduino has an inbuild very stable reference voltage (a Mega has two) that is independent of supply variations.
You have to enable that in the code.
It now doesn´t jump around anymore but is probably too slow for the project I was aiming for.
I also wondered why you use an array, while you only use a running avarage and never loop over the array itself ?
I believe this logic accomplishes the same
setup ()
...
last =0
loop()
total = total - last; // subtract the last reading
dummy = analogRead(inputPin); // one unused reading to clear ghost charge
last = analogRead(inputPin); // read from the sensor
total = total + last; // add the reading to the total
but you probably wanted this line to substract the first item from the array, not the previous reading ?
total = total - readings[index]; // subtract the last reading