faulty reading from LM35 temparature sensor when interfaced with UNO

hello,

I'm trying to measure temperature,soil moisture and humidity using different sensors.(LM35 for temp DHT11 for humidity).when i connect these sensors individually and run, they work perfectly fine.but when i connect them together and run, LM35 does not give out correct temperature.I'm not able to figure out a the reason behind that.the code is provided below.
Please help.

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
Serial.begin(9600);
dht.begin();
Serial.println("Weather Sensor");
}

void temparature()
{
float temp=analogRead(5);
temp=temp5100/1024;
Serial.print("temparature:");
Serial.print(temp);
Serial.println("^C");
delay(2000);

}

void moisture()
{

float moist=analogRead(1);
Serial.print("soil moisture:");
Serial.print(moist);
Serial.println(".");

delay(2000);
}

void humidity()
{

float humid=dht.readHumidity();
float t=dht.readTemperature();
Serial.print("humidity:");
Serial.print(humid);
Serial.println("%");

delay(5000);
}

void loop()
{
temparature();
moisture();
humidity();
}

Hi, welcome to the forum.

I can't see it either. Sorry.
You use the LM35 for temperature, that means that the 5V is used as reference.
When you power the Arduino with the USB, and that is not 5.0V, but 4.8V, the temperature you get seems to get higher due the lowering of the 5V.

Can you tell more ?
What are the temperatures that you get ? Do you use a breadboard for the sensors ? (some breadboards have bad contacts) Do you use long wires ?

You could print the raw (integer) value from analogRead(), to be sure that the trouble is not caused in the calculation.

void temparature()
{
  int rawADC = analogRead(5);
  Serial.print("rawADC:");
  Serial.print(rawADC);
  float temp = (float) rawADC * 5.0 * 100.0 / 1024.0;
  Serial.print(", temparature:");
  Serial.print(temp);
//  Serial.println("^C");
  delay(2000); 
}

Do you see how I use "5.0" instead of "5" in the floating point calcuation and also for the other numbers. That is to remind myself (and the compiler) that the calculation is done with floating point.

hi Peter,

Sorry, i didn't explain about the procedure that i followed.

I use the 5V reference voltage provided on the UNO board as the energy source for all the sensors.Yes, i use a bread board to make the connections.I use jumper wires to make those connections(they are only few cms in length).I also ran a small program to check the voltage that i was getting from the 5V pin, and it was 5.0V. I connected a wire from 5V pin to the bread board.I then made parallel connections to three analog pins from the bread board and found out all the three pins read 5.0V .

But then, I noticed a strange thing. when I removed the 5V connection, those pins started to give out random voltages ranging between 1.5V to 2.0V(seems mysterious).

Later, I connected those sensors and ran the program.It was then that i encountered the problem. Temperature sensor was acting weird. Just to check, without removing any hardware connections, I ran a program to sense temp alone. It gave a constant output at 24.90 C. i ran the previous program again and there was the same problem with the temp sensor. the temperature varied drastically. Sometimes it would shoot upto 41 C n sometimes come down to 19 C.

I ran another program just know what was going on.I found that when i run humidity and temperature sensor together there was no problem. humidity sensor was connected to digital pin and LM35 to the analog pin. but when soil moisture sensor and LM35 were run together there was the problem with LM35( both were connected to analog pins A1 and A5). I hope u got a clear picture of what is going on.

I tried your code to get the raw values from the temp sensor.this time i got consistent output :)!!. But the value that i got was pretty high.(temperature is around 25 C in my city).

rawADC:649, temparature:316.89soil moisture:1023.00.
humidity:43.00%
temp:24.00
rawADC:650, temparature:317.38soil moisture:1023.00.
humidity:42.00%
temp:24.00
rawADC:650, temparature:317.38soil moisture:1023.00.
humidity:42.00%
temp:24.00
rawADC:648, temparature:316.41soil moisture:1023.00.
humidity:42.00%
temp:24.00
rawADC:649, temparature:316.89soil moisture:1023.00.
humidity:42.00%

here's the code once again

#include "DHT.h"
#define DHTPIN 2 
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
  Serial.begin(9600);
  dht.begin();
  Serial.println("Weather Sensor");
 }

/*void temparature()
{ 
  float temp=analogRead(5);
  temp=temp*5*100/1024;
  Serial.print("temparature:");
  Serial.print(temp);
  Serial.println("^C");
  delay(2000);
 
}*/

void temparature()
{
  int rawADC = analogRead(5);
  Serial.print("rawADC:");
  Serial.print(rawADC);
  float temp = (float) rawADC * 5.0 * 100.0 / 1024.0;
  Serial.print(", temparature:");
  Serial.print(temp);
//  Serial.println("^C");
  delay(2000); 
}


void moisture()
{ 
  
  float moist=analogRead(1);
  Serial.print("soil moisture:");
  Serial.print(moist);
  Serial.println(".");
  
  delay(2000);
}

void humidity()
{ 
  
  float humid=dht.readHumidity();
  float t=dht.readTemperature();
  Serial.print("humidity:");
  Serial.print(humid);
  Serial.println("%");
  Serial.print("temp:");
  Serial.println(t);
  
  delay(5000);
}


void loop()
{
  temparature();
  moisture();
  humidity();
  }

I hope i was clear in explaining the problem. :slight_smile:
Thanks.

Yes, I understand your circuit.

Someone had the same problem two weeks ago. A single LM35 was working, and together with other sensors on the breadboard is was fluctuating. I don't know if that was solved in the end.

The LM35 is not very stable, and the breadboards can be very bad. So even if you measure 5.0V, there might still some bad things going on.

Could you try a few tests ?
Do not mix the LM35 with other sensors on the same breadboard. Use a seperate breadboard for the LM35, or connect it directly to the pins of the Arduino. Add a capacitor of 100nF to 5V and GND next to the LM35.
To avoid oscillation, a resistor can be used in the analog output line from the LM35 to the Arduino. I forgot the value, I think it is in the datasheet.
If these tests make a difference, you might have a (very) bad breadboard.

If those tests didn't change a thing, then you could make a photo of your wiring.