I've connected the LM35DZ temperature sensor to my Arduino Mega board.
I'm getting really high temperature values . I guess i'm missing something basic and it would be really great if you could help me find my error.
An example from my serial monitor display:
Calculated val is : 842
Vout is = 4.11V
TEMPRATURE = 411.13*C
val is the analogRead result.
Vout is val * (5/1024) = val * resolution.
Temperature is Vout *100. (voltage to Celcius degrees conversion factor).
My question is:
if Vout = resolution * analogRead result (adc)
using the example from the serial monitor :
resolution * analogRead * 100 = 5/1024 * 842* 100 = 4.11 * 100 = 411 Celcius degrees.
Any idea where the problem is?
I would really appreciate your assistance.
Some remarks:
I've measure using a multimeter and got the same Vout.
my room temperature is around 25 degrees.
My arduino board is connected via USB cable from my laptop. i've measure the voltage the LM35DZ gets: 4.96V.
I've used another LM35DZ and another Arduino Mega board and got the same results.
I also tried using different analog pins and got the same results.
The LM35DZ GND is connected directly to the Arduino Mega ground . It doesn't share GND with another sensors.
Please see the attached:
1.image (board , sensor and wires).
ino file. code can also be read below:
My Code:
const int tempPin = A0;
void setup()
{
// Variable declaration
pinMode(tempPin,INPUT);
Serial.begin(9600);
}
void loop()
{
measureTemp(tempPin);
delay(1000);
}
void measureTemp(int temperaturePin)
{
int val;
val = analogRead(temperaturePin);
Serial.print("Calculated val is : ");
Serial.print(val);
Serial.println();
float cel = ( 5.0 * val*100.0) / 1024.0;
Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);
}
Could be a couterfeit LM35 if you have them from an ebay seller.
Try a 100n decoupling capacitor on the VCC/GND pins on the sensor (close to the sensor).
If that doesn't work, throw them away, and buy the digital DS18B20.
If it works, use this sketch, with a 5x higher resolution.
Leo..
// connect LM35 to 5volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"
float tempC; // Celcius
void setup() {
analogReference(INTERNAL1V1); // Mega's internal 1.1volt Aref
Serial.begin(9600);
}
void loop() {
tempC = analogRead(A0) * 0.1039;
Serial.print("Temperature is ");
Serial.print(tempC, 1); // one decimal place
Serial.println(" Celcius");
delay(1000); // use a non-blocking delay when combined with other code
}
Thanks for your answers.
Wawa, I don't have a capcitor , i doubt a smoothing capacitor will help when the ADC output is so high... but i'll give it a shot . it will take time for me to get a capacitor.
Wawa and outsider,
Regarding the sensors: i have 2 sensors: one was bought in Amazon. i got the other one from the laboratory in the college i study at. so these sensors are not from the same batch.
My project is a autonomous robot (4WD) which detects fire and extinguishes it. It's also supposed to take the temperature into considerations and alert in case of high temperatures.
I've considered purchasing a digital (DHT11 or DHT22) sensor, but the fact this sensor comes with a header file with ready functions will not be appreciated by the proffessors. It's much easier to deal with analog sensor (as i have plenty of work with other sensors ahead of me).
Check that the sensor is working. Print out the analogRead value, without any calculations. It should be about 40 in a 20 degree C environment. If ARef = 5v.
Serial.println( analogRead( tempPin ));
If that is reasonable then this should give you the temperature in degrees C.
vitzmuni:
Wawa, I don't have a capcitor , i doubt a smoothing capacitor will help when the ADC output is so high... but i'll give it a shot . it will take time for me to get a capacitor.
My project is a autonomous robot (4WD) which detects fire and extinguishes it. It's also supposed to take the temperature into considerations and alert in case of high temperatures.
I've considered purchasing a digital (DHT11 or DHT22) sensor, but the fact this sensor comes with a header file with ready functions will not be appreciated by the proffessors. It's much easier to deal with analog sensor (as i have plenty of work with other sensors ahead of me).
A bypass capacitor has nothing to do with the A/D.
It's wise to use a cap across the POWER pins of the temp sensor if the wires are long.
You will find that it's much harder to make an analogue sensor stable in a bigger project.
Stable temps depends on two things.
Sensor output voltage.
Reference voltage of the A/D.
That's why I suggested using the internal 1.1volt Aref instead of the potentially unstable default Aref (VCC).
Ground current is another problem. The LM35 must have it's own ground wire to the Arduino (not shared).
Leo..