I get wrong temperature status from my code (Celsius)

hi :slight_smile:

I get wrong temperature status on my code.

feks:

Here is now 24 Celsius
But my code say it is 43

What is wrong? I like to get Celsius

#include <LiquidCrystal_I2C.h>
#include <Wire.h>  // Comes with Arduino IDE

// CaddyCamping v2
// Styring til kj√łlleskap

//declare variables

const int relayPin1 = 7; //Relepin for 12V kj√łleskapp
const float fanOffTemp = 42.0; // Kj√łleskappet sl√•r seg av p√• 5C Rele g√•r High
const float baseTemp = 43.0;
const float fanOnTemp = 44.0; // Kj√łleskappet sl√•r seg p√• 10c rele g√•r low

float tempC = 0;  // variable for holding Celcius temp (floating for decimal points precision)
int tempPin = 0;  // Declaring the Analog input to be 0 (A0) of Arduino board.
float samples[8]; // array to hold 8 samples for Average temp calculation
float maxi = 0, mini = 100; // max/min temperature variables with initial values. LM35 in simple setup only measures Temp above 0.
int i;
//int val;

// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  //
void setup() {

  Serial.begin(9600); //opens serial port, sets data rate to 9600 bps

  pinMode(relayPin1, OUTPUT);

  digitalWrite(relayPin1, HIGH);// ** USE Normaly Open on relay

  lcd.begin(20, 4); // set up the LCD's number of columns and rows

  lcd.setCursor(0, 0); // set LCD cursor position (column, row)
¬† lcd.print("Kj√łllesystem V2.0"); // print text to LCD
  lcd.setCursor(0, 1);
¬† lcd.print("Kj√łlleskap");
  delay(2000); // wait 2000ms
  lcd.clear(); // clear LCD display}
  

  
}

void loop() {

  Serial.println(analogRead(tempPin)); // Displays on serial monitor the sampled value before conversion to real Temperature reading

  // Start of calculations FOR loop.
  for (i = 0; i <= 7; i++) { // gets 8 samples of temperature
    samples[i] = ( 1.5 * analogRead(tempPin) * 100.0) / 1024.0; // conversion math of LM35 sample to readable temperature and stores result to samples array. 1024 is the Bit depth (quantization) of Arduino.
    // 5 is the supply volts of LM35. Change appropriatelly to have correct measurement. My case is 4.4Volts.

    // Serial.println(samples[i]);
    Serial.print("."); // print a dot for every sample at serial monitor

    // Display Current Celcius Temp to LCD
    // ( LCD note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 0); // set LCD cursor position
    lcd.print("Tempratur: "); // print to LCD
    lcd.setCursor(12, 0);
    lcd.print(samples[i]);  // print current Temp sample to LCD
    tempC = tempC + samples[i]; // do the addition for average temperature
    delay(800); // wait 800ms
  }
  // END of FOR loop

 //Statren
  // if (digitalRead(button) == LOW)
   { 
    //Her er coden
  }
  // Slutten
    
                       

// Max temp og min Temp

Serial.println(""); // Like and CR at serial monitor
Serial.println("");

tempC = tempC/8.0; // calculated the averare of 8 samples in Celcius
if(tempC > maxi) {maxi = tempC;} // set max temperature
if(tempC < mini) {mini = tempC;} // set min temperature
// Send Results to Serial Monitor
Serial.println("New measurement");
Serial.print(" Average Temperature in Celcius is " );
Serial.println(tempC-2);
Serial.print(" MAX Temperature in Celcius is " );
Serial.println(maxi);
Serial.print(" MIN Temperature in Celcius is " );
Serial.println(mini);

// Send results to LCD.
lcd.setCursor(0, 0);
lcd.print("Av.T   Max   Min");
lcd.setCursor(0, 1);
// print the measured temp average
lcd.print(tempC-2);
lcd.setCursor(6, 1);
// print the maximum temp
lcd.print(maxi);
lcd.setCursor(12, 1);
// print the minimum temp
lcd.print(mini);
delay (1000);

  if (tempC - 2 <= baseTemp && tempC - 2 < fanOnTemp) {
    digitalWrite(relayPin1, HIGH);
                 lcd.setCursor(0, 2);
           
    lcd.print("ON ");
    delay(500);

  } else if (tempC - 2 >= fanOnTemp) {
    digitalWrite(relayPin1, LOW);
         lcd.setCursor(0, 2);
             
    lcd.print("OFF");
    delay (3000);
  } else if (tempC - 2 <= fanOffTemp) {
    digitalWrite(relayPin1, HIGH);

             lcd.setCursor(0, 2);
          lcd.print(""); 
    lcd.print("ON----");

    delay (500);
    delay(3000);   // Wait about 3 seconds to display the results to LCD screen befor starting the loop again
    tempC = 0; // Set tempC to 0 so calculations can be done again
    
  }
}

Code is from him: Arduino LCD Display Temperature Sensor Plus Relay - YouTube

The only time the code resets tempC to zero is if ‚ÄútempC - 2 <= fanOffTemp‚ÄĚ. If the temperature is greater than fanOffTemp, the ‚Äúaverage‚ÄĚ will continue to increase.
Add
tempC = 0;
after this:

 // Start of calculations FOR loop.

There’s also a problem with the code using tempC-2 almost everywhere. It looks like that was added to correct for an offset in the temperature measurement. Your installation may not have the same, or any, offset.

Pete

Thx but not work

I use LTMP35 sensor

I going to make a temp sens then turn off a fan if it hot

This doesn't give you the average temperature, only the cumulative result of your samples.

tempC = tempC + samples[i]; // do the addition for average temperature

To get the average you will have to divide tempC by the number of samples.

tempC = tempC/8.0; // calculated the averare of 8 samples in Celcius

Pete