Im trying to get a simple LM35DZ temperature sensor working, but when I wire it to the arduino and use the sketch on this page, I get an output which:
-does not correspond to the temperature of the room Im in (about 20-25C), and sometimes it even jumps to 200C
-the sensor turns on and off every 5-10 seconds (5-10 on, 5-10 off, ... )
I tried different sensors (I bought 5 LM35's) and different analog ports, but to no avail
This is the code used:
/*
Simple Temperature uses the lm35 in the basic centigrade temperature configuration
*/
float temp;
int tempPin = A2; // analog input pin
int sampleTime = 1000; // 1 second dafault
void setup()
{
Serial.begin(9600);
}
void loop()
{
//gets and prints the raw data from the lm35
temp = analogRead(tempPin);
//Serial.print("RAW DATA: ");
//Serial.print (temp);
//Serial.println(" ");
//converts raw data into degrees celsius and prints it out
// 500mV/1024=.48828125
temp = temp * 0.48828125;
Serial.print("CELSIUS: ");
Serial.print(temp);
Serial.println("*C ");
delay(sampleTime);
}
Michiel_:
sorry, I thought just deleting some lines wasnt a problem
Do you believe that deleting lines never causes a problem? If you can't provide accurate information, how can we help you? Our answers would be based on incorrect assumptions. Surely you can see that.
Now Im trying with this code, but the sensor still goes up and down all the time...
const int analogInPin = A2; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.println(sensorValue);
delay(500);
}
@aarg:
Yes, I know from experience that deleting something can be fatal... But since its such a simple (or so it seems), I thought someone might just recognise the problem. My mistake
"-the sensor turns on and off every 5-10 seconds (5-10 on, 5-10 off, ... )"
What's that supposed to mean? The Arduino is rebooting or what?
Spell it out.
[When all else fails, check your wiring.]
"Nope, can't see it"
Don't be abstruse.
Can't see it? Can't see what?
Well, Im asking you guys... the arduino sends 0's for like 5 seconds, and then sends values again for 5 seconds of something.
And you told me to read is a couple of times, but I still can't find any error...
Seriously, can't you guys be a bit more clear or answer slightly less sarcastic? I already have to collect all my courage to post something here, Im only an electronics enthusiast, so sorry if my replies aren't professional as the guys who post things here on a daily basis...
nothing else is connected, so its 5V to Vin, Gnd to Gnd and A2 to Vout
Michiel_:
Seriously, can't you guys be a bit more clear or answer slightly less sarcastic? I already have to collect all my courage to post something
I haven't been sarcastic, just frank. Nobody knows who you are or where you live, I don't see making a post to an anonymous forum as an act of courage.
Maybe you're overly sensitive.
Maybe you have bad jumpers, a deadspot on your breadboard or a crappy USB cable - none of that should diminish your self-esteem.
I dug out an LM34 and used the following sketch and it works great. Its output is consistent, but there were hiccups till I added the second read (expected).
/*
LM34
10mV / degree_F
750mV = 75F
*/
byte tempPin = A0; // analog input pin
unsigned int sampleTime = 1000; // 1 second dafault
unsigned int ADCresult;
unsigned int w; // whole #
unsigned int m; // mantissa
void setup()
{
Serial.begin(9600);
}
void loop()
{
ADCresult = analogRead(tempPin); // throw-away
ADCresult = analogRead(tempPin);
ADCresult = ADCresult * 48;
w = ADCresult / 100;
m = ADCresult % 100;
Serial.print(w);
Serial.print(".");
Serial.println(m);
delay(sampleTime);
}
OMG, NOW I get what you meant in your previous posts... I thought you told me to re-read everything, that my mistake was obvious and I read over it in the script. I hope that explains my reaction
I tried your script, and for a moment, I thought it kinda worked! (although I think 140*F isnt really the temperature here in the cold netherlands), but then I realised I had to assign it to pinA2, and then, the zeroes came back...
I also tried different sensors and wires, different location on my BB, another usb cable, a capacitor,... but that didnt work either
Maybe Ill buy another at my electronics store instead of ebay, but it would surprise me that all of the sensors are defect
Maybe it's wise to add this line to the "void setup" (just after Serial.begin).
Serial.println("Rebooting");
Then you will know if it's rebooting.
This code might be better/stable once you have sorted out the problem.
Read the comments carefully. You might have to adjust some things for an LM35 or 36 and to your serial monitor.
Just ignore the LCD shield part for now.
// TMP35 or TMP36 temp sensor connected to Analogue input A1, 3.3volt and ground
// or LM35 temp sensor connected to A1, 5volt and ground
// LM35 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 sensor 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
}
Leo..
Edit:
The LM35 is made for positive temps.
The TMP36 has a better coverage for outside in NL.
Code can do both.
For testing, you could use a voltage divider instead of the sensor.
Try 12k from the 3.3volt pin to the analogue pin, and a 1k from the analogue pin to ground.
That should put 1k/(1k+12K) * 3.3volt = ~0.25volt on the pin, and display ~25.4C on the serial monitor.
Leo..
Is the sensor's output voltage stable if you measure the voltage with a DMM (0.25volt@25C).
You could try "loading" the sensor with a ~10k resistor.
So 10k between sensor out and ground.
I doubt if extra decoupling will help with such short wires.
There is enough decoupling on the Arduino board.
Double-check your wires, and use another area of the breadboard.
Leo..
Wawa:
Is the sensor's output voltage stable if you measure the voltage with a DMM (0.25volt@25C).
You could try "loading" the sensor with a ~10k resistor.
So 10k between sensor out and ground.
I doubt if extra decoupling will help with such short wires.
There is enough decoupling on the Arduino board.
Double-check your wires, and use another area of the breadboard.
Leo..
Im puzzled, the MM says there is no voltage over sensor out-ground... And Im SURE that I used the MM right. When loading the sensor, the readings go to 0 immediately.
However, trying the filter on the datasheet, makes the output VERY stable... however, the readings are wrong... sometimes is displays 30C, then 6C... and when I put my finger on it, to check if it gets hotter, there is no response to that... I used a 100Ohm resistor, and tried a 1uF and 100nF caps, both gave about the same results.
I tried it with the script provided by Wawa and an adapted analogread, which I posted a couple of posts ago.
EDIT: Upon reopening the serial monitor, I notice the values kept rising to about 24.5*C, which is plausible! However, upon placing it in front of my laptop fan, which has significant warmer air coming out, the temperature DROPS?!
EDIT2: Now the temperature has settled again, with the sensor in from of the air outlet of my laptop. The temperature indicated is 22*C, while is is definately warmer than the surroundings...