Newbie: LM35 and floating analog pin: Is my sensor or arduino defect ?

Hi everyone,

I am just starting out with arduino's and electronics... Trying to get through the steep learning curve and reading myself senseless :slight_smile:

I connected an LM35 sensor to my arduino: P1 = 5V+, P2 to A0, and P3 to GND (as depicted and shown everywhere on the net)

Now, when I turn on the arduino and do an analogRead(A0), I get a lot of values, floating between 300 and 400. Heating or Cooling the LM35 does change the value a little sometimes, but not all the time.

When I connect nothing to pin A0, I get the exact same behaviour in output: floating between 300 and 400. So, I thought maybe the pin doesnt see the connection at all?

I read something on floating pins with a pull-up or pull down resistor, so I tried to add a 10K resistor between pin A0 and GND, with the Vout of the LM35 also in A0.. That only gives me readings of 0....
This pull-up / pull down to prevent floating I can read about is always described with a button, but I havent found anything on LM35 on this... So I have no idea if I did that right...

The code I have is very straight forward, and searching on the net shows the straight connections everytime and no resistors.

int tempPin = A0; //temp sensor LM35 Vout

void setup()
{
  Serial.begin(9600);
  Serial.println("Device Ready");
  pinMode(tempPin,INPUT);
}


void loop()
{
  delay(500);
  Serial.println(analogRead(tempPin));
}

The code I have is very straight forward, and searching on the net shows the straight connections everytime and no resistors.

Your code and wiring look correct.

So, I thought maybe the pin doesnt see the connection at all?

I think you are on the right track here. There could be a defective jumper wire, so try changing those. The bread board, or how the lm35 is inserted, could be defective, so try changing the location on the breadboard.

If those efforts don't produce stable readings of A0, then just try your sketch with A0 connected to ground, 3.3v and 5v. If the Arduino is working correctly, you will see stable readings of 0, 6xx, and 1024.

If the Arduino is working and the wiring and breadboard holes are confirmed, then you are looking a possible defective lm35. If the ground and 5v are hooked up backwards on the lm35 the device will get very hot, Your orientation looks correct, but if it were hooked up backwards for some period of time it might be damaged.

I never use pinMode to read an analogue pin.

Try this.
Read the comments carefully.
e.g. set the serial monitor to 115200 baud, and connect the sensor to A1.
Ignore the LCD parts of the code.
Leo..

// 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 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
}

@satch72

+1 for your Karma as you are one of the few new that give all information needed in first post.

Thanks :slight_smile:

Thanks Rob!

Lost my Karma probably due to the (very) late reply! (I had seen the replies, but not yet answered)....
Tried the above, and tried several other options, but no luck...

So, I am thinking the LM35 is broken, so I am currently waiting to receive an other one.... (hopefully wednesday) ...

At that point I think it will be resolved... If not, then something else is really weird.

I tried testing the arduino with another sensor (photoresistor) and that seems to work fine, so its not the pin that is broken i guess ....

Will let you know my findings once the new sensor has arrived!