Sensor Unstable after digitalWrite

I tried to make an LCD that show the temperature using LM35 sensor. When the temperature reach 40 degrees, the arduino should turn on the relay in pin 10. The code is like this :

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int sensor1 = A0;
int sensor2 = A1;
int suhuadc1;
int suhuadc2;
float tegangan1;
float tegangan2;
float nilaisuhu1;
float nilaisuhu2;
int suhuakhir2;
int suhuakhir1;
int relay1 = 10;

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Dispenser");
  lcd.clear();
  pinMode(relay1, OUTPUT);
}

void bacasuhu() {
  suhuadc1 = analogRead(sensor1);  
  suhuadc2 = analogRead(sensor2);  
 // converting that reading to voltage, for 3.3v arduino use 3.3
  tegangan1 = suhuadc1 * 5000.0 / 1024; 
  tegangan2 = suhuadc2 * 5000.0 / 1024; 
  nilaisuhu1 = tegangan1/10;
  nilaisuhu2 = tegangan2/10;
  suhuakhir1 = (int) nilaisuhu1;
  suhuakhir2 = (int) nilaisuhu2;
 
  lcd.setCursor(0,0);
  lcd.print("T1 :");
  lcd.setCursor(5,0);
  lcd.print(suhuakhir1);
  lcd.setCursor(0,1);
  lcd.print("T2 :");
  lcd.setCursor(5,1);
  lcd.print(suhuakhir2);
  delay(1000); 
}

void loop() {
  bacasuhu();
  if (suhuakhir1>=40) {
    digitalWrite(relay1,HIGH);
  }
  else {
    digitalWrite(relay1,LOW);
  } 
}

I don't know why, but after the temperature reach 40 degrees the temperature that read from the sensor become so unstable and inaccurate. So what's going on and what to do to fix that?

Thanks Before

So what's going on and what to do to fix that?

What ever it is it is not your software.

Probably either the way you have wired it up, or your power supply not being able to provide enough current. But that is just a guess as you have posted no schematic nor photograph (not exceeding 1000 pixels).

I wonder if this bit of code has something to do with the problem:

 if (suhuakhir1>=40) {
    digitalWrite(relay1,HIGH);
  }

Is a relay connected to your device? If so, what type, how is it wired and how much current does it take?

Thanks @Grumpy_Mike and @jremington for the response. I connected the 5v relays direct to the arduino without driver. It takes about 40-90mA current to move the coil. It is SRS songle relay.
Should I use the driver with a transistor to solve the problem?

You will eventually destroy the Arduino, or at least burn out the output pin, by connecting the relay as you have. Connect it using a circuit similar to that shown below (but use a smaller base resistor, say 470 ohms).

Relay for bulb.jpg

The Arduion output is only good for 20 to 30 mA. At a draw of 40mA damage starts to happen.
With your realy directly connected and no back EMF diode I assume, you are killing your processor so no wonder the A/D converter is playing up.
Use the relay driver like jremington said.

Thanks @jremington and @Grumpy_Mike the problem was solved using driver with 2N3904 Transistor!
I use schematic from @jremington.
Thanks again!

I don't know why, but it still error in reading Second sensor (A1). Is that because I used wrong transistor (2N3904)? is 2N3904 has too small current? what is the best transistor to drive 5v relay?

At first you said it was unstable, now you say there is an error. Is this the same thing because in electronics they mean two different things?

@Grumpy_Mike sorry for taking so long time to reply. Yes, i mean the same. I mean error is unstable. Did that because of the transistor?

You still haven't posted any schematics, code or photographs of your setup.

I use schematic exactly like @jremington post. But use 5v power supply.

relay copy.jpg

I think you keep missing the point.

This fault can be caused by ground lift if the sensor and the relay are wired so that there is a section of wiring that carries the ground for both the relay and the sensor. I want to know if you have made that error.

The solution is to use star wiring of both power and ground. That is have the power and ground for relay board and sensor wired to physically the same point, not chained.

OK, I try to post the complete schematic. I hope this will more helpful.
So, is that caused by ground lift? and how is the sample of star wiring?

I hope this will more helpful.

well sorry but no.

So, is that caused by ground lift?

It can be.

and how is the sample of star wiring?

With star wiring the grounds must physically meet at one point and separate as much as possible the analogue from the digital side. On the Arduino there are three ground points so use one for the analogue ground returns and the other for the digital ground returns. Have the power wires physically wired to the same spot, NOT chained one to the other. This is why I am asking for a photograph of your wiring. Why will you not co-operate with me?

Here is a better way of wiring up what you have but as you insist on not providing a FULL circuit of what you have it is hard to be definitive.

Here is a picture illustrating the difference between star wiring and daisy chained wiring.

Daisychain vs Star.JPG