LM35 is unstable

Hello, im working a simple project.
to make relay controlled by lm35 temperature.

i make it with

  • 3 push buttons as an up down, and start button to start the program
  • lm35
  • 5VDC relay - i connect it to DC heater (5v)

The sketch is ok, lm35 also good when i tested it with another sketch, as a thermometer only. But in this project the problem is, when i set temperature example 40 Celsius, relay on to make heater keep heating to reach the set level., but the output of lm35 is getting unstable, its changing so many level and then turn back to previous level and continue like that, it makes relay also turning off and on and off and off and so on because relay will turning off if the temperature reached the setting level.

when i disconnect the heater from relay, lm35 read is stable, but when i reconnect, its getting unstable.
i've checked the supply, its also unstable. maybe this caused the lm35 output fluctuate.

the question is why the supply is getting unstable when i connect the heater to relay?

where did i go wrong??
please help.

This is the sketch

#include <LiquidCrystal.h>
int reading = 0;
int sensorPin = A0;
int relay =12;
int buzzer =13;
int celsius = 0;
int suhu = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
 
void setup() {
    Serial.begin(9600);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
 pinMode(relay,OUTPUT);digitalWrite(12,HIGH);
 pinMode(buzzer,OUTPUT);digitalWrite(13,HIGH);
 pinMode(11, INPUT);digitalWrite(11,HIGH);  // pin start
 pinMode(9, INPUT);digitalWrite(9,HIGH);   // pin up
 pinMode(10, INPUT);digitalWrite(10,HIGH);  // pin down
 lcd.clear();
 delay (200);
 lcd.setCursor (0,0);
 lcd.print("  Alat Terapi   ");
 lcd.setCursor(0,1);
 lcd.print("   Sinusitis    ");
 delay(2000);
  lcd.setCursor(0,0);
 lcd.print("   Berbasis   ");
 lcd.setCursor(0,1);
 lcd.print("  Arduino Uno  ");
 delay(2000);
  lcd.setCursor (0,0);
 lcd.print("     Oleh      ");
 lcd.setCursor(0,1);
 lcd.print("    Albama     ");
 delay(2000);
  lcd.setCursor(0,0);
 lcd.print("     NIM     ");
 lcd.setCursor(0,1);
 lcd.print("    14.006    ");
 delay(2000);
  lcd.setCursor (0,0);
 lcd.print("  Pembimbing I  ");
 lcd.setCursor(0,1);
 lcd.print("  Syahir,ST.MT  ");
 delay(2000);
  lcd.setCursor(0,0);
 lcd.print("  Pembimbing II ");
 lcd.setCursor(0,1);
 lcd.print(" Muh. Nurman, BE");
 delay(2000);
 lcd.clear();
}
 
void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.begin(16, 2);
 lcd.print("Setting Suhu"); 
 delay(2000);
 // lcd.scrollDisplayLeft();
 // wait a bit:

 if(digitalRead(9)==LOW||digitalRead(10)==LOW)
{delay(200);
  
    while(1)
  {
    if(digitalRead(9)==LOW){suhu++;}
    if(digitalRead(10)==LOW){suhu--;}
    if(digitalRead(11)==LOW){delay(200);break;}
    lcd.begin(16, 2);lcd.print("Setting Suhu    ");
    lcd.setCursor(4, 2);lcd.print(suhu);
    delay(200);
  }
    while(1)
  {
   program(); 
  }
}
}
void program()
{

 reading = analogAvg(sensorPin);
 celsius= (5.0*reading)*100/1024;
 if (celsius < suhu)
  {
     digitalWrite(12, LOW);
       }
  else
  
  {digitalWrite(12,HIGH);
 
  }
 
 lcd.begin(16, 2);lcd.print("Suhu");
  lcd.setCursor(12,2);
  lcd.print(celsius);
  lcd.print("C");
  delay(1000);
 }


 
 int analogAvg (int sensorPin)
 {
 unsigned int total=0;
 for(int n=0; n<suhu; n++ )
    total += analogRead (sensorPin);
 return total/suhu;
 }

What kind of heater? How much current? You need some hysteresis in your switching program.

// pseudo code //
if(celsius < suhu)
  digitalWrite(12,LOW);
else if( celsius  > suhu +1)
  digitalWrite(12,HIGH);

When you say "unstable" how much is celsius changing over how long a time period?

edgemoron:
What kind of heater? How much current? You need some hysteresis in your switching program.

// pseudo code //

if(celsius < suhu)
  digitalWrite(12,LOW);
else if( celsius  > suhu +1)
  digitalWrite(12,HIGH);



When you say "unstable" how much is celsius changing over how long a time period?

The current when power connected is 4mA
the fluctuation is 6 to 8 celsius up and down, its changing per one second

thank you for the code, but its still fluctuating

What is the heater voltage? Is it AC or DC? Are the wires to the heater run close to the LM35 and Arduino? 4mA is not much of a heater unless the voltage is high.
Can you post a picture and / or wiring diagram?

edgemoron:
What is the heater voltage? Is it AC or DC? Are the wires to the heater run close to the LM35 and Arduino? 4mA is not much of a heater unless the voltage is high.
Can you post a picture and / or wiring diagram?

Thank you for still replying, i've solved the problem, the power supply i used is not enough voltage to supply all the components (relay, lcd, lm35, heater - all 5V supply), so i try to connect heater to another power supply and the lm35 stops fluctuating.

Power supply i use is DVD power supply, the output is 12V and 5V

so i just have to find new power supply which can supply all the components without decreasing voltage.

I have another question if you don't mind.

im new to arduino so it still difficult to me to modify the code.

i want to use selection between heater and cooler that connected to one relay. so before setting the temperature, first choose we want to use heating or cooling.

between heater and cooler i put switch, so i can manually switch it. but the program must be different according to different temperature level.

i dont know where i add the code

Changeover switch probably easiest (SPCO).
Connect the common to ground and set two digital inputs to internal high (uses the inbuilt pull-up resistors)
Read the state of the inputs with digital read.
Include a decision line like "if switch position A low, turn heating on by energising the relay" - cooling is the default here.
You will need a changeover relay with the heating/cooling power connected to relay contact COMMON and use the NORMALLY OPEN to heat and NORMALLY CLOSED to cool.
On second thoughts, you could get away with a simple ON/OFF switch, provided one position is the default.
Personally, I might use two independent relays and maybe a centre off changeover switch so that you can power everything off.
Usual warnings if working with mains electricity.
Size your power supply large enough to easily power all the non-Arduino bits and if using a relay, don't forget to protect the Arduino with some diodes at the very least.