analogRead

Hello, I have a problem reading my analog inputs. I have an lm35 tempeture sensor and four ldrs. In separate programs the temperature reading I get is correct (25C) but when I put the whole program (with lds) together the temperture value falls to 17C . Why does that happen? I have Arduino mega2560.

#include <LiquidCrystal.h>

LiquidCrystal lcd(53,52,51,50,49,48);

int ldr1 = A0;                                    
int ldr2 = A1;                                    
int ldr3 = A4;                                  
int ldr4 = A5; 
int i=0;     
float temp=0; 
int tempPin = A10;

void setup() 
{
  Serial.begin(9600);
  pinMode(ldr1, INPUT);                            
  pinMode(ldr2, INPUT);                          
  pinMode(ldr3, INPUT);                          
  pinMode(ldr4, INPUT);
  pinMode(temp, INPUT);
  lcd.begin(16, 2);  
}

void loop()
{

    temp = analogRead(tempPin);                  
    temp = temp * 0.48828125;                       
    lcd.setCursor(0, 0);                           
    lcd.print("Temp :");                          
    lcd.setCursor(6, 0);                        
    lcd.print(temp);                          
    lcd.print(" C");                                                     
    delay(10);
    if (temp>28)                                  
      digitalWrite(37, HIGH);                     
    else
      digitalWrite(37, LOW);                    
  int value1 = analogRead(ldr1);                   
  int value2 = analogRead(ldr2);                   
  int value3 = analogRead(ldr3);                  
  int value4 = analogRead(ldr4);
}

Does the reported temperature change if you leave the code as it is and completely disconnect the LDRs ?
What LDRs are you using and how are they wired ?

Thanks for quick reply :slight_smile: .. Correct, the reported temperature changes to 26*C immediately after disconnecting the four ldrs. Ldrs used are 10K-1M. The ldrs are connected exactly like in picture without the potentiometers. Where can the problem be?

You should try using and external power supply to power up your servos and other component on the breadboard

Try changing this line

temp = analogRead(tempPin);                  
temp = temp * 0.48828125;
//to

temp = analogRead(tempPin)* (float)0.48828125;

I use an external power source for the servos ( the sketch is old but ldrs sketchare correct) . I changed the line and nothing changed but like it more like this. The thing I just noticed is that when I put light on one of the four ldrs (ldr3 pin A3) the temperature value changes from 18 to 25 (temperature pin is A10). Is analog pin A3 damaged?

Is analog pin A3 damaged?

Move the sensors around between the pins. Does the problem still occur on the same pin ? Disconnect just the LDR on A3. Does that fix the temperature reading ?

Try changing this line

How is that going to make a difference?

Why are you casting a float to a float?

PaulS:
How is that going to make a difference?

Why are you casting a float to a float?

You are right. That is not correct.

I think you problem is that you are sampling multiple analog inputs and it takes sometime for the voltage to stabilize. To fix it try sampling one sensor a few times, ignore the first few reading and go with the 3rd one.
pseudo code

loop 3 time
{
temp = analogread

}
loop 3 time
{
ldr1 = analogread

}
and so on

alehandro:
Hello, I have a problem reading my analog inputs. I have an lm35 tempeture sensor and four ldrs. In separate programs the temperature reading I get is correct (25C) but when I put the whole program (with lds) together the temperture value falls to 17C . Why does that happen? I have Arduino mega2560.

Probably because you have ground return currents interfering with the LM35 measurements - that
sensor puts out only 0.01V per degree, so you need to use precision circuit design if you
are going to get away with it. In particular you must measure the LM35's output w.r.t. its
exact ground potential, not some random bit of ground wiring in your lash up. The
best way to do that is use differential amplifier circuit (using an opamp or instrumentation
amp).

That means you can both amplify the voltage and refer it to the Arduino ground directly.
(use star-grounding scheme).

If the LM35 signal is coming in from an external cable you will need to filter out RFI too
with a small capacitor.

UKHeliBob:
Move the sensors around between the pins. Does the problem still occur on the same pin ? Disconnect just the LDR on A3. Does that fix the temperature reading ?

Will I took a video with the lcd showing temperture value with and without ldrs. I have put a delay(500) for this. https://www.dropbox.com/s/ogphh3yl68gihqm/VID_20150308_233157.3gp?dl=0 ( temperture gets crazy when I conect all ldrs)
I disconected pin A3 but the temperture afterall just corrects a little. Disconnecting all ldrs fixes the problem. I moved the sensors to other pins (A6-A9) and the problem still occurs.

MarkT:
Probably because you have ground return currents interfering with the LM35 measurements - that
sensor puts out only 0.01V per degree, so you need to use precision circuit design if you
are going to get away with it. In particular you must measure the LM35's output w.r.t. its
exact ground potential, not some random bit of ground wiring in your lash up. The
best way to do that is use differential amplifier circuit (using an opamp or instrumentation
amp).

That means you can both amplify the voltage and refer it to the Arduino ground directly.
(use star-grounding scheme).

If the LM35 signal is coming in from an external cable you will need to filter out RFI too
with a small capacitor.

I dont exactly understand what you are telling me to do. I have connected lm35 straight to arduino mega like scheme. Are u telling me that because I have ldrs and the lm35 together they may interfear with each other? What circuit do you mean? "so you need to use precision circuit design".

Found the answer here analogRead problem - Troubleshooting - Arduino Forum