LM35 sensor problems (flickering on and off)

Hi everyone!

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 :frowning:

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);

}

This is the output:

CELSIUS: 38.57*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 12.70*C 
CELSIUS: 36.62*C 
CELSIUS: 34.67*C 
CELSIUS: 28.81*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 28.32*C 
CELSIUS: 36.62*C 
CELSIUS: 32.71*C 
CELSIUS: 18.55*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 11.72*C 
CELSIUS: 37.11*C 
CELSIUS: 36.13*C 
CELSIUS: 32.23*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 15.14*C 
CELSIUS: 37.11*C 
CELSIUS: 35.64*C 
CELSIUS: 32.23*C 
CELSIUS: 17.58*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 0.00*C 
CELSIUS: 29.30*C 
CELSIUS: 36.13*C

The output you posted does not correspond with the code you linked to. Please post the code with your modifications included.

aarg:
The output you posted does not correspond with the code you linked to. Please post the code with your modifications included.

sorry, I thought just deleting some lines wasnt a problem :slight_smile:

Michiel_:
sorry, I thought just deleting some lines wasnt a problem :slight_smile:

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.

Read it two or three times in succession

float temp;
byte tempPin = A2; // analog input pin
unsigned int sampleTime = 1000; // 1 second dafault
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  temp = analogRead(tempPin);
  temp = analogRead(tempPin);
  temp = analogRead(tempPin);
  temp = temp * 0.48828;
  Serial.print(temp);
  Serial.println(" *C ");
  delay(sampleTime);
}

Nope, can't see it... :confused:

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 :wink:

"-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?

It's time to post a photo of your wiring.

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? :frowning: 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? :frowning: 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);
}

Adding a 0.1uF (not your issue) at the breadboard made it a rock-solid "Superstar".

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 :wink:

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 :confused:

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.

Before purchasing another sensor, try:

  1. Check that you're not using 3.3V by mistake (can't tell from the picture).

  2. Use 0.01uF (or 0.1uF) bypass capacitor and RC of 75 ohm / 1uF as per figure 13 in the datasheet.

Michiel_:

  1. OMG, NOW I get what you meant in your previous posts... I thought you told me to re-read everything, ...

  2. Maybe Ill buy another at my electronics store instead of ebay, ...

  1. I did post a sketch (#4)
  2. I'll drink to that!

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..

@Wawa, I tried your code, and it doesnt go to zero anymore! But its still wrong, and is fluctuates a lot...

The voltage divider gave a constant output though, so its the sensors for sure...

@dlloyd: Im using 5V for sure! Im gonna look for the RC filter first thing tomorrow!

@Runaway Pancake: I thought you wanted me to focus on that part of the code... :-[

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...

http://playground.arduino.cc/Main/LM35HigherResolution

I think your wiring is wrong.

It looks to me as face on it should be:
GND Vout Vin

Assuming RED is V+ ?
Black is GND ?