LM35, Photocell, Servo, and Relay causing erratic fluctuation in LM35

I cannot seem to figure out why this is happening. I am running a Servo, Relay, LM35 for temp monitoring, and a photocell. My temp sensor fluctuates a lot while looping through my program. The goal is to have my Arduino Uno R3 Plus, control 2 Relays (for 120v 5A loads), when temperatures reach a threshold. The Servo motor is to run when the photocell reaches a certain brightness, and moves back as a certain darkness (not calibrated fully yet).

This is the output I get from my code. Notice the temp fluctuation. As a side note, even when disconnecting the Servo and Relay, I still get erratic behavior.

Output:
Temp: 18.55good morningvoidloopstart
Celcuis: 18.55
Temp: 18.55good morningvoidloopstart
Celcuis: 20.51
Temp: 43.51good morningvoidloopstart
Celcuis: 16.11
Temp: 16.11good morningvoidloopstart
Celcuis: 21.48
Temp: 21.48good morningvoidloopstart
Celcuis: 16.11
Temp: 16.11good morningvoidloopstart
Celcuis: 25.88
good morningvoidloopstart
Celcuis: 24.90
good morning

/*
 * Tutorial 2a: Sensing Light
 * 
 * Measure brightness using a photocell over serial.
 *
 *
 * To see this sketch in action, put the board and sensor in a well-lit
 * room, open the serial monitor, and and move your hand gradually
 * down over the sensor.
 *
 * The circuit:
 * - photoresistor from analog in 0 to +5V
 * - 10K resistor from analog in 0 to ground
 *
 *
 * created 1 Jul 2009
 * modified 9 Apr 2012
 * by Tom Igoe 
 * modified 13 August 2013
 * by Blaise Jarrett
 *
 * This example code is in the public domain.
 *
 * Derivative work from:
 * http://www.arduino.cc/en/Tutorial/SwitchCase
 *
 */
#include <Servo.h>
#include <math.h>

Servo myservo;
const int sensorMin = 0;
const int sensorMax = 800;

int photocellPin = A5;
int LEDPin = 2;
int LEDPin2 = 4;
int pos = 0;
int lm35Pin = A0;
int RelayPin = 10;


void setup()
{
    // set up serial at 9600 baud   
    Serial.begin(9600);
    pinMode (LEDPin, OUTPUT);
    pinMode (LEDPin2, OUTPUT);
    myservo.attach(9);
    myservo.writeMicroseconds(1500);
    pinMode(10, OUTPUT);

}

void loop()
{
    Serial.println("voidloopstart");
    int photocellValue;
    int tempValue;
    int range;
    float temperature;

    tempValue = analogRead(lm35Pin);

    temperature = float(tempValue) * 0.48828125;
    Serial.print("Celcuis: ");
    Serial.println(temperature);

    if(temperature <= 24){
      digitalWrite(RelayPin, HIGH);
      Serial.print("Temp: " + String(temperature));

  }
    else {
              digitalWrite(RelayPin, LOW);
             // Serial.print("else");

  }
         // digitalWrite(RelayPin, LOW);

    // do something different depending on the 
    // range value
 delay(1000);
 
    photocellValue = analogRead(photocellPin);
    range = map(photocellValue, sensorMin, sensorMax, 0, 2);
    
    switch (range) 
    {
        // your hand is on the sensor
        case 0:
            Serial.print("good night");
            digitalWrite(LEDPin, HIGH);
            digitalWrite(LEDPin2, LOW);
            myservo.write(0);
            
            break;
  
        case 1:
            Serial.print("good morning");
            digitalWrite(LEDPin, LOW);
            digitalWrite(LEDPin2, HIGH);
            myservo.write(90);

            break;
            // your hand is a few inches from the sensor
            
        case 2: 
            Serial.println("bright");
            digitalWrite(LEDPin, LOW);
            digitalWrite(LEDPin2, HIGH);

            break;
    }

    delay(20000);
}

PS, I am not a programmer, but learned Java way back 10+ years ago. Ive lost a lot of my abilities, but am using this as a way to build my coding again. Any input is appreciated.

Any ideas on what could be going on?

I have executed only the LM35+Relay portion of your codes; the temperature reading is quiet stable. Please, check the connection diagram of LM35 or the sensor itself.

#include <Servo.h>
#include <math.h>

Servo myservo;
const int sensorMin = 0;
const int sensorMax = 800;

int photocellPin = A5;
int LEDPin = 2;
int LEDPin2 = 4;
int pos = 0;
int lm35Pin = A0;
int RelayPin = 10;


void setup()
{
  // set up serial at 9600 baud
  Serial.begin(9600);
  pinMode (LEDPin, OUTPUT);
  pinMode (LEDPin2, OUTPUT);
  myservo.attach(9);
  myservo.writeMicroseconds(1500);
  pinMode(10, OUTPUT);

}

void loop()
{
  Serial.println("voidloopstart");
  int photocellValue;
  int tempValue;
  int range;
  float temperature;

  tempValue = analogRead(lm35Pin);

  temperature = float(tempValue) * 0.48828125;
  Serial.print("Celcuis: ");
  Serial.println(temperature);

  if (temperature <= 24)
  {
    digitalWrite(RelayPin, HIGH);
    Serial.print("Temp: " + String(temperature));

  }
  else
  {
    digitalWrite(RelayPin, LOW);
    // Serial.print("else");

  }
  delay(1000);
/*
  photocellValue = analogRead(photocellPin);
  range = map(photocellValue, sensorMin, sensorMax, 0, 2);

  switch (range)
  {
    // your hand is on the sensor
    case 0:
      Serial.print("good night");
      digitalWrite(LEDPin, HIGH);
      digitalWrite(LEDPin2, LOW);
      myservo.write(0);
      break;

    case 1:
      Serial.print("good morning");
      digitalWrite(LEDPin, LOW);
      digitalWrite(LEDPin2, HIGH);
      myservo.write(90);
      break;
    // your hand is a few inches from the sensor

    case 2:
      Serial.println("bright");
      digitalWrite(LEDPin, LOW);
      digitalWrite(LEDPin2, HIGH);
      break;
  }

  delay(20000);
  */
}

lm35SM.png

lm35SM.png

Analogue sensors are tricky to get right.
They need a 'clean' supply, with a bypass cap on the LM35 if the wiring is long.
And a ground to the Arduino that is not shared by other devices.
Best solution is to dump the LM35, and use a digital DS18B20.

An LM35 outputs 10mV per degree C, so 250mV@25C.
That results in an A/D value of 51@25C.
About two A/D values per degree C, so a resulution of 0.5C.
Trying to display that low resolution with two decimal places will result in weird gaps.

Resolution can be improved about five times (to ~0.1C) if the internal 1.1volt Aref is used.
Leo..

// LM35_TMP36 temp
// works on 5volt and 3.3volt Arduinos
// connect LM35 to 5volt A0 and ground
// connect TPM36 to 3.3volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"

const byte tempPin = A0;
float calibration = 0.1039;
float tempC; // Celcius
float tempF; // Fahrenheit

void setup() {
  analogReference(INTERNAL); // use internal 1.1volt Aref
  Serial.begin(9600);
}

void loop() {
  tempC = analogRead(tempPin) * calibration; // use this line for an LM35
  //tempC = (analogRead(tempPin) * calibration) - 50.0; // use this line for a TMP36
  tempF = tempC * 1.8 + 32.0; // C to F
  Serial.print("Temperature is  ");
  Serial.print(tempC, 1); // one decimal place
  Serial.print(" Celcius  ");
  Serial.print(tempF, 1);
  Serial.println(" Fahrenheit");

  delay(1000); // use a non-blocking delay when combined with other code
}

This may help you Reading LM34, LM35, LM335 (LM3x) Temperature Sensors Accurately – Arduino++

So mainly sounds like stable supply voltage would be the concern, not the code?

Makes sense, @Wawa. Ill try updating my code. Thank you for the input.

@GolamMostafa, I believe the LM35 is wired correctly. I ran a separate LM35 code to test, and it responds well without the additional devices.

It works, +/- 1 F is fine for a chicken coop.

Stable voltage and the improved resolution did the tick, thanks!

@OP

It is important for us to know the real cause of the fluctuating/unstable readings of the LM35 sensor so that we are safe for the future. If you have powered the the LM35 from the UNO's 5V, the supply voltage is already a stable one. Experience says that the bad/intermittent contacts of the sensor pins lead to erratic temperature readings. It is hard to believe that the new VREF (1.1V) has cured the problem. With VREF set at 5V, the ADC's resolution is about 4.8 mV; whereas, the LM35's resolution is 10 mV.

UPDATE:

So Ive determined it is not actually the LM35, I STILL am having major fluctuation. What I determined is when I pulled the Photocell, the temperature output varies only 1F +/-

The photocell is causing the issue.

So new question is, is it my coding somehow? The photocell? or the configuration?

Before you do an analogRead, take a reading from the pin you iintend, and throw away the result, e.g.

  (void)analogRead(lm35Pin);
  tempValue = analogRead(lm35Pin);

@AWOL ill give it a try

So Ive determined it is not actually the LM35, I STILL am having major fluctuation.

Power the servo and relay from one source and the Arduino from another. Never power a servo from the Arduino 5V output, as it will cause huge voltage fluctuations and might even destroy the regulator.

Don't forget to connect the source grounds.