Reading the temperature a couple of times to watch it rises

Hi all,

My question here is related to the LM35 temperature sensor and Arduino micro controller. I want to make a temperature controller, that initially it will reading the temperature a couple of times to watch the temperature is rising.

I need advice for for code..

Thank you,

If you Google for "arduino LM35 code example" you will find lots of information and examples.

 unsigned long t1=0;    
 unsigned long t2=0;

void setup(void) {
  Serial.begin(14400);
}

void loop(void){

t1=millis();
    
if( t1 - t2 > 500)//read every 1/2 second
{
t2=t1;
Serial.println(analogRead(A3));//print raw value - sensor is 10mv per degree Celsius I think
}

}

This is PSEUDO-Code

  • Make an AnalogRead on your temperature sensor.
  • Store the value in a variable called temperatureold (you can use any name).
  • Use Delay() to determine when you want to make your next reading
  • Make an AnalogRead on your temperature sensor.
  • Store the value in a variable called temperaturenew (you can use any name).
  • Compare the temperatenew with temparatureold
  • Do now what you want to do
  • temperatureold = temperaturenew (free temperaturenew for the next measurement)

For advanced programmers: This is for a NEWBIE, no millis() and other advanced stuff is needed here.

This is basic Code, just the basic instructions needed to get the job done. Even the Pseudo-Code is not really good.

Dear all,

Thanks for suggestion. I like to read only a single couple of time and then compare two value, if it is bigger than old reading then program will run otherwise it will show error message.

Here is may code. Looking for batter idea.

Code:
int Temperatureold;
int Temperaturenew;
unsigned long t1=0;
unsigned long t2=0;

void setup(void) {
Serial.begin(19200);
}

void loop(void){

t1=millis();

for(int i = 0; i > 1; i++){
if( t1 - t2 > 5000) //read every 5 second
{
t2=t1;
Serial.println(analogRead(A3));//print raw value - sensor is 10mv per degree Celsius I think
}
}
if (Temperaturenew - Temperatureold < 5){
Serial.println("ERROR");
}
}

Think this should be easy to follow. Uses 4 different states. NOT tested.

int Temperatureold;//might want to call these temperature1 and temperature2
int Temperaturenew;
byte getreading = 1;//initialize for reading Temperatureold

unsigned long t1=0;   
unsigned long t2=0;

void setup(void) 
{
  Serial.begin(19200);
}

void loop(void)
{
    t1=millis();
    
        if( getreading != 4 && t1 - t2 > 5000 )//read every 5 seconds unless stopped getreading = 4 error
        {
            t2=t1 //reset for next read
            
           int temp = analogRead(A3);//Get current temperature reading - 2.048 counts per degree C
           
           if(getreading == 1)// 1 Temperatureold
           {
              getreading = 2; // set 2 to get Temperaturenew next pass
              Temperatureold = temp;// save reading
           }
           
           if(getreading == 2) // 2 Temperaturenew
           {
              getreading = 3; // set 3 to compare next pass
              Temperaturenew = temp; // save reading
           }
                      
           if( getreading == 3 && (Temperaturenew < Temperatureold) ) // 3 compare - do any condition you want here
           {
              getreading = 4; // 4 stop no more readings 
              Serial.println("ERROR"); 
           }
           else 
           { 
              getreading = 1;  //set 1 to start all over with Temperatureold 
               Serial.println( Temperaturenew ); //print out the temperature
           }
                 
}// end loop

Thanks, Ray_Beebe.

I will check it. But I want to compare the temperature only one time. If this then, may I change the value

" if( getreading != 4 && t1 - t2 > 5000 )"

4 to 2?

Thanks again

Hi shahed_khan,

The problem with just reading the sensor once per comparison is that a small amount of noise can cause the output to become seemingly random. None-the-less, you can still see the trend if you do it that way, but your thermostat would be clicking on and off all the time. Here's the basic, unsmoothed, code:

#define temperaturePin A0

int previousTemperature = 0;
int currentTemperature = 0;

unsigned long previousMillis = millis();
long fiveSeconds = 5000L;

void setup() {
  currentTemperature = analogRead(temperaturePin);
  Serial.begin(9600);
}

void loop() {
  if (millis() - previousMillis >= fiveSeconds) {
    previousMillis += fiveSeconds;

    previousTemperature = currentTemperature;
    currentTemperature = analogRead(temperaturePin);
    
    Serial.print(currentTemperature / 10);
    Serial.print('.');
    Serial.print(currentTemperature % 10);

    if (currentTemperature > previousTemperature) {
      Serial.println(F(" C - Warming"));
    } else {
      if (currentTemperature < previousTemperature) {
        Serial.println(F(" C - Cooling"));
      } else {
        if (currentTemperature == previousTemperature) {
          Serial.println(F(" C - Steady"));
        }
      }
    }
  }
}
1 Like