thermocouple.readCelsius() value not refreshing. Suggestions are appreciated.

Hello World,

I am trying to take thermocouple reading after every 1-second interval. I have tried two approaches, one with delay() & the other with millis() function. The latter is not updating the temperature value. Code, wiring example & results are attached. Thanks for your time & concern, any help shall be appreciated.

Thanks,
:slight_smile:

with delay() function

#include <max6675.h>
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
int sl = 0;
float sum = 0;
float avg = 0;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

void setup() {
  analogReference(INTERNAL1V1);
  Serial.begin(115200);
  delay(200);
  Serial.println("Data logging started...");
}

void loop() {
  delay(200);
  sl = sl + 1;
  sum = 0;
  Serial.print("time (s)");
  Serial.print(" \t");
  Serial.print(sl);
  Serial.print(" \t");
  Serial.print("Temperature = ");
  Serial.print(" \t");
  for (int i = 0; i < 20; i++) {
    sum = sum + thermocouple.readCelsius();
    delay(39);
  }
  avg = sum / 20;
  Serial.print(avg);
  Serial.print(" \t");
  Serial.println("*C");
}

Results: working

Data logging started...
time (s) 1 Temperature =   69.25 *C
time (s) 2 Temperature =   70.00 *C
time (s) 3 Temperature =   69.75 *C
time (s) 4 Temperature =   70.25 *C
time (s) 5 Temperature =   70.00 *C
time (s) 6 Temperature =   71.00 *C
time (s) 7 Temperature =   71.25 *C
time (s) 8 Temperature =   71.25 *C
time (s) 9 Temperature =   72.00 *C
time (s) 10 Temperature =   71.00 *C
time (s) 11 Temperature =   72.25 *C
time (s) 12 Temperature =   72.50 *C
time (s) 13 Temperature =   72.00 *C
time (s) 14 Temperature =   73.25 *C
time (s) 15 Temperature =   72.50 *C
time (s) 16 Temperature =   73.25 *C
time (s) 17 Temperature =   73.25 *C
time (s) 18 Temperature =   73.25 *C
time (s) 19 Temperature =   73.50 *C
time (s) 20 Temperature =   73.50 *C
time (s) 21 Temperature =   74.00 *C
time (s) 22 Temperature =   73.50 *C
time (s) 23 Temperature =   74.25 *C
time (s) 24 Temperature =   74.75 *C
time (s) 25 Temperature =   74.50 *C
time (s) 26 Temperature =   75.25 *C
time (s) 27 Temperature =   75.00 *C
time (s) 28 Temperature =   75.25 *C
time (s) 29 Temperature =   75.75 *C
time (s) 30 Temperature =   75.00 *C

with millis() function

#include <max6675.h>
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
int sl = 0;
float sum = 0;
float avg = 0;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  analogReference(INTERNAL1V1);
  Serial.begin(115200);
  delay(200);
  Serial.println("Data logging started...");
}

void loop() {

  unsigned long currentMillis = millis();
  sum = 0;
  for (int i = 0; i < 20; i++) {
    sum = sum + thermocouple.readCelsius();
  }
  avg = sum / 20;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    sl = sl + 1;
    Serial.print("time (s)");
    Serial.print(" \t");
    Serial.print(sl);
    Serial.print(" \t");
    Serial.print("Temperature = ");
    Serial.print(" \t");
    Serial.print(avg);
    Serial.print(" \t");
    Serial.println("*C");
  }
}

Results: Not working (temp. value static)

Data logging started...
time (s) 1 Temperature =   84.75 *C
time (s) 2 Temperature =   84.75 *C
time (s) 3 Temperature =   84.75 *C
time (s) 4 Temperature =   84.75 *C
time (s) 5 Temperature =   84.75 *C
time (s) 6 Temperature =   84.75 *C
time (s) 7 Temperature =   84.75 *C
time (s) 8 Temperature =   84.75 *C
time (s) 9 Temperature =   84.75 *C
time (s) 10 Temperature =   84.75 *C
time (s) 11 Temperature =   84.75 *C
time (s) 12 Temperature =   84.75 *C
time (s) 13 Temperature =   84.75 *C
time (s) 14 Temperature =   84.75 *C
time (s) 15 Temperature =   84.75 *C
time (s) 16 Temperature =   84.75 *C
time (s) 17 Temperature =   84.75 *C
time (s) 18 Temperature =   84.75 *C
time (s) 19 Temperature =   84.75 *C
time (s) 20 Temperature =   84.75 *C
time (s) 21 Temperature =   84.75 *C
time (s) 22 Temperature =   84.75 *C
time (s) 23 Temperature =   84.75 *C
time (s) 24 Temperature =   84.75 *C
time (s) 25 Temperature =   84.75 *C
time (s) 26 Temperature =   84.75 *C
time (s) 27 Temperature =   84.75 *C
time (s) 28 Temperature =   84.75 *C
time (s) 29 Temperature =   84.75 *C
time (s) 30 Temperature =   84.75 *C

With your millis version, you are reading the thermocouple continuously, far far more frequently than you do in the version with delay. As a first attempt to debug, put a delay in the for loop where you sum readings, as you do in the first sketch.

WindBill is correct, but i would say to prevent adding delay() how about you do the reading just before the conditional printing as such:

#include <max6675.h>
int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
int sl = 0;
float sum = 0;
float avg = 0;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  analogReference(INTERNAL1V1);
  Serial.begin(115200);
  delay(200);
  Serial.println("Data logging started...");
}

void loop() {

  unsigned long currentMillis = millis();

  avg = sum / 20;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    sum = 0;
    for (int i = 0; i < 20; i++) {
      sum = sum + thermocouple.readCelsius();
    }
    sl = sl + 1;
    Serial.print("time (s)");
    Serial.print(" \t");
    Serial.print(sl);
    Serial.print(" \t");
    Serial.print("Temperature = ");
    Serial.print(" \t");
    Serial.print(avg);
    Serial.print(" \t");
    Serial.println("*C");
  }
}

wildbill:
With your millis version, you are reading the thermocouple continuously, far far more frequently than you do in the version with delay. As a first attempt to debug, put a delay in the for loop where you sum readings, as you do in the first sketch.

Thanks a lot, put delay function inside the for loop but results are same.

Deva_Rishi:
WindBill is correct, but i would say to prevent adding delay() how about you do the reading just before the conditional printing as such:

#include <max6675.h>

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
int sl = 0;
float sum = 0;
float avg = 0;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

unsigned long previousMillis = 0;
const long interval = 1000;

void setup() {
  analogReference(INTERNAL1V1);
  Serial.begin(115200);
  delay(200);
  Serial.println("Data logging started...");
}

void loop() {

unsigned long currentMillis = millis();

avg = sum / 20;
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    sum = 0;
    for (int i = 0; i < 20; i++) {
      sum = sum + thermocouple.readCelsius();
    }
    sl = sl + 1;
    Serial.print("time (s)");
    Serial.print(" \t");
    Serial.print(sl);
    Serial.print(" \t");
    Serial.print("Temperature = ");
    Serial.print(" \t");
    Serial.print(avg);
    Serial.print(" \t");
    Serial.println("*C");
  }
}

This fixes the issue. Putting avg value before the loop. The first value seems zero but it works. Thank you.

Hint: The average of n readings is not sum/20, unless n is 20.

nightcrawler218:
Thanks a lot, put delay function inside the for loop but results are same.
This fixes the issue. Putting avg value before the loop. The first value seems zero but it works. Thank you.

Oh hey i meant to put the avg=sum/20 after the loop !! (Oops, just didn't select it before i moved the loop), that will fix the first value being 0 as well i think. Main thing was to not read all the time but only when the timing interval has expired.