Hi guys.
i have issue with lm35 sensor i tried many codes and many sensors(3-4) but i still don't get normal values from the sensor its shows me not reasonable values like:
17
5
0
0
0
12
3
4
0
0
etc...
and this my last code that i use and i used many others.
float tempC; // create variable to store the temperature in.
int tempPin = 0; // Attach vout to analog pin 0.
void setup() // Will execute once at the start of the code.
{
Serial.begin(9600);
}// opens serial port, sets data rate to 9600 bps
void loop() // code here will continue to replay nutil powered off.
{
tempC = analogRead(tempPin); // read the analog value from the lm35 sensor.
tempC = (5.0 * tempC * 100.0)/1024.0; // convert the analog input to temperature in centigrade.
Serial.print((byte)tempC); // send the data to the computer.
delay(3000);
}
alexzi13:
i use this schematic:
see the attachment ...
and this my last code that i use and i used many others.
float tempC; // create variable to store the temperature in.
int tempPin = 0; // Attach vout to analog pin 0.
void setup() // Will execute once at the start of the code.
{
Serial.begin(9600);
}// opens serial port, sets data rate to 9600 bps
void loop() // code here will continue to replay nutil powered off.
{
tempC = analogRead(tempPin); // read the analog value from the lm35 sensor.
tempC = (5.0 * tempC * 100.0)/1024.0; // convert the analog input to temperature in centigrade.
Serial.print((byte)tempC); // send the data to the computer.
delay(3000);
}
try :
void loop(){
tempC = analogRead(tempPin); // read Temperature 10mV/C
tempC= (tempC*5.0/1024.0)*100.0; // convert to Celsius
Serial.print(tempC,2); // display value as float, two digit decimal
}
Your Serial.print((byte) tempC); told the compiler to treat tempC as a byte, so, it just printed the byte value of the first byte. It did not convert the VALUE of tempC to value that would fit in a byte. It just grabbed the first byte (floats use 4 bytes of storage) and displayed it's value.
If you actually wanted to convert it to a byte use byte(tempC). Notice the placement of the parenthesis.
Compliers are very picky. They only do what you tell them to do, not what you want them to do.
Try this.
Code prints to serial monitor and/or LCD shield.
Connect LM35 to A1.
For calibration, adjust the Aref value in line 15.
// TMP35 or TMP36 temp sensor connected to Analogue input A1, 3.3volt and ground
// or LM35 temp sensor connected to A1, 5volt and ground
// temp range ~2C to ~105C
// display on serial monitor and/or LCD
// for a TMP36 (-40C to ~55C), change line 45 to: tempC = total * Aref * 0.1 / numReadings - 50.0;
//
#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 analog input
byte index = 0; // index of the current reading
unsigned int total = 0; // running total
int inputPin = A1; // the pin that the TMP35 is connected to
float Aref = 1.0759; // change this value to the actual Aref voltage of ---YOUR--- Arduino (1.0 - 1.2), or adjust to get accurate 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 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
tempC = total * Aref * 0.1 / numReadings; // value to celcius conversion
tempF = tempC * 1.8 + 32; // Celcius to Fahrenheit conversion
// 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
}
Wow.
Granularity is ~50 digital values at room temp, and you want to print it with two decimal places as 25.00 degrees C.
Amazing.
Leo..
I just wanted him to see the changing values, I realize that a LM35 -> Arduino.analogRead() has a range of 0..307 full range and 0..80 from freezing to body temp.
So, the values returned are going to step over 100th and possibly 10th of degrees, but they will change.
@chucktodd
I didn't want to correct you.
Just added the fact of bad resolution without jumping through a few hoops, like 1.1volt Aref, multiple reads and smoothing.
Leo..
@Wawa Thanks i think your code is pretty stabilize the sensor,but after 10-15 mints the temp is starting to climb up, weird...
i changed the Aref to 5 i figure it out that its the voltage that my arduino use right?
@chucktodd i tried your code but it showed me not reasonable values.
maybe the problem is in correct adjustment of Aref...
You have 1024 steps in your A/D. If you want to display numbers with 1 decimal point,
each step would be at lest one tenth of a degree.
the sensor is 0.01 volt per degree. .001 volt per 1/10 degree.
.001 * 1024 is 1.024 V reference.
With the A/Ds linearity and sampling error, your reading will be accurate to
+- 0.2 degrees, even though it will give you number that are finer. That does
not count sensor error, just the arduinos error.
How much clime are you seeing? It may be warming from your breathing
on it, the heat from the Arduino board and a little from self heating.
Dwight
+- 43 what?
43 degrees or 43 counts?
If using 1.1V full scale, 43 counts
is about 0.5C.
43C, would be that you have something wrong.
Just curious, what range of temperature are you intending
to use it for?
Dwight
alexzi13: @Wawa Thanks i think your code is pretty stabilize the sensor,but after 10-15 mints the temp is starting to climb up, weird...
i changed the Aref to 5 i figure it out that its the voltage that my arduino use right?
No.
The code uses the internal more stable 1.1volt Aref, not the default 5volt Aref.
Every Arduino is slightly different, so you have to adjust that 1.0759 value to calibrate.
It could be anything between 1volt and 1.2volt, but it's usually is just bellow 1.1volt.
Make very small adjustments of .01volt or 0.001volt to get the temp right.
Leo..
Then you have a problem.
Check with you finger. If the sensor is at 43 degrees hotter, you'll
notice it right away.
Check your wiring. If the LM35 is wired upside down, it will do strange things.
Dwight