Problem with using a photoresistor and lm35 temperature sensor simultaneously

I'm using a photoresistor(vt93n2) and a temperaturesensor(lm35) in my project. They are both connected to the same 5V and GND rails on my breadboard.

Scenario 1
When i'm reading the photoresistor and temperature sensor pins sequentially in every loop iteration like this:

void loop(){
 getPhotoRes();
 getTemp();
}

void getPhotoRes(){
  photoResVal = analogRead(photoResPin);
  Serial.print("photoResVal   :");Serial.println(photoResVal);
}

void getTemp(){
  float mVolts = analogRead(tempPin)*5000.0/1024.0;
  float c = mVolts/10;
  Serial.print("TEMP          :");Serial.println(c);
}

The temperature is highly inaccurate. It's always somwhere in the range of 12 to 40 degrees celsius. My room temperature is 20 degrees celsius.

Scenario 2
Just by toggling between reading the photoresistor or temperature sensor every loop iteration like this:

boolean toggle = false;
void loop(){
    if(toggle==true)getPhotoRes();
    else getTemp();
    toggle=!toggle;
}

The temperature reading is accurate and around 20 degrees celsius.

So what's exactly happening? Is it that the temp sensor is getting less voltage from the 5V because the photoresistor is using some of it in scenario 1?

Can you give a link to a datasheet of the vt3n2 ?

Please give a full sketch, and tell us which Arduino board you use.
This website is for you : http://snippets-r-us.com/

Do you have two breadboards ? Try to put each sensor on its own breadboard with wires directly from the breadboard to the Arduino board. Breadboards can have bad contacts, or maybe a ground wire is missing.

How many resistors do you use ? which value ?
Can you make a photo of your project ?

I think the ADC in the AT328 chip uses successive approximation. it takes 13 clock cycles to run the full gamit to come to a result on a sample.

when you think of taking a reading, you think there is no buffer an you can look at one scan and get a value. in application, the input from the last reading is the starting point and you may or may not get a correct reading if you try to read both on the same scan in rapid succession.

one of the tricks we use to get past this problem is to read the input more than once

void getPhotoRes(){
photoResVal = analogRead(photoResPin);
 photoResVal = analogRead(photoResPin);
 Serial.print("photoResVal   :");Serial.println(photoResVal);


void getTemp(){

float mVolts = analogRead(tempPin)*5000.0/1024.0;
float mVolts = analogRead(tempPin)*5000.0/1024.0;

 float c = mVolts/10;[/color]
 Serial.print("TEMP          :");Serial.println(c);

http://meettechniek.info/embedded/arduino-analog.html

there are lots on results on google about the adc

Koepel:
Can you give a link to a datasheet of the vt3n2 ?

Please give a full sketch, and tell us which Arduino board you use.
This website is for you : http://snippets-r-us.com/

Do you have two breadboards ? Try to put each sensor on its own breadboard with wires directly from the breadboard to the Arduino board. Breadboards can have bad contacts, or maybe a ground wire is missing.

How many resistors do you use ? which value ?
Can you make a photo of your project ?

I had a typo in my opening post, the photoresistor is VT93N2. http://www.farnell.com/datasheets/919043.pdf

I’m using an arduino UNO, here is the full code:

#define redm 0
#define greenm 1
#define bluem 2
#define redmt 3
#define greenmt 4
#define bluemt 5
const int photoResPin = 0;
int photoResVal = 0;

const int potPinTresh = 1;
int potPinThreshR = 0, potPinThreshG = 0, potPinThreshB = 0;

const int potPinRGB = 2;
int potPinRGBVal = 0;

const int buttonPin = 7;
int buttonVal = 0;

const int redPin=13, greenPin=12, bluePin=11;
const int rPin=6, gPin=3, bPin=5;

const int tonePin = 10;

const int tempPin = 3;

long lastTime = 0;
boolean toggle = false;

void setup(){
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT); 
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT); 
}

void loop(){
  getButtonState();
  getRGB();
  
  if(toggle==true)getPhotoRes();
  else getTemp();
  toggle=!toggle;
}

void getButtonState(){
   if(digitalRead(buttonPin) == HIGH){
    if(buttonVal<5){
      buttonVal++;
    }
    else{
      buttonVal=0;
    }
    delay(200);
  }
}

void getRGB(){
  potPinRGBVal = map(analogRead(potPinRGB),0,1023,0,255);
  switch(buttonVal){
      case redm:
        setRGB(potPinRGBVal,-1,-1);
        break;
      case greenm:
        setRGB(-1,potPinRGBVal,-1);
        break;
      case bluem:
        setRGB(-1,-1,potPinRGBVal);
        break;
      case redmt:
        potPinThreshR = analogRead(potPinTresh);
        break;
      case greenmt:
        potPinThreshG = analogRead(potPinTresh);
        break;
      case bluemt:
        potPinThreshB = analogRead(potPinTresh);
        break;
  }
}

void setRGB(int red, int green, int blue){
  if(red!=-1)analogWrite(rPin, red);
  if(green!=-1)analogWrite(gPin, green);
  if(blue!=-1)analogWrite(bPin, blue); 
}

void getTemp(){
  float mVolts = analogRead(tempPin)*5000.0/1024.0;
  float c = mVolts/10;
  Serial.print("TEMP          :");Serial.println(c);
}

void getPhotoRes(){
  photoResVal = analogRead(photoResPin);
  Serial.print("photoResVal   :");Serial.println(photoResVal);
  if(photoResVal<potPinThreshR){digitalWrite(redPin, HIGH);}else{digitalWrite(redPin, LOW);}
  if(photoResVal<potPinThreshG){digitalWrite(greenPin, HIGH);}else{digitalWrite(greenPin, LOW);}
  if(photoResVal<potPinThreshB){digitalWrite(bluePin, HIGH); tone(tonePin, 500);}else{digitalWrite(bluePin, LOW); noTone(tonePin);}
}

And here is a picture of it:

I don’t have two breadboards. I am using 6 150 Ohm resistors for the leds and 2 10 kOhm resistors for the switch and photoresistor.

Thanks for the extra information and photo.

The lower part of the breadboard is not connected to the upper part, so you do have two seperate parts. At this moment the current through the leds does not influence the ground of the sensors, that's good.

I can not see a problem, other than the common LM35 problems. It could be instability, because I can't spot a real problem.

That is an Arduino.org board, and we are Arduino.cc. Please support Arduino.cc by buying Arduino.cc or Genuino boards.

The LM35 can be unstable. The Arduino needs a good reference voltage to calculate the temperature, and the 5V is not very good, since the USB voltage could change between 4.5 and 5.2V. That is why most of us use a digital temperature sensor, the DS18B20.

Make the LM35 more stable by adding a capacitor to the GND and 5V of the breadboard.
Add a resistor in the analog line from the LM35 to the Arduino. About 2k2 or 4k7. In the datasheet of the LM35 that resistor is mentioned for the capacitance of a cable, but you don't have a cable, so perhaps that resistor doesn't help.

In your sketch please use A0 and A3 instead of 0 and 3 for the analog pins. The analogRead() function accepts both, the 'A0' is more obvious that it is an analog pin.

Try to read the temperature sensor before the light sensor. Maybe it helps a little.

The Arduino Uno has a stable 3.3V, and an internal reference voltage of 1.1V. Those can be used for a more accurate temperature.
It is explained in this tutorial : Overview | TMP36 Temperature Sensor | Adafruit Learning System

Hi,
dave-in-nj solution should work.
The arduino only has one ADC, it switches between the ADC inouts, it takes time to switch and convert.
The ADC has a small capacitor on its input, when you switch from one input to another the charge on the cap has to stabilize.
If you read two different analog inputs after each other without letting the charge stabilize you will get varying readings.
The trick as shown is two read the analog input twice and use the last value.

Tom...... :slight_smile: