Taking the derivative of temperature using Max 6675 thermo and ESP32

Hi everyone,

I am trying to take the temperature measurement using a max 6675 thermocouple (which has a library of its own) and ESP32 to send the data to my phone. I am using Blynk, and I am pushing the data using "myTimerEvent()" to their server which runs every second in the loop. The problem is that I want to take the derivative of the temperature and I am using millis() and if statement as said in this link. However, I am having issues since this spits out a value and then zeros out in the middle. What is the best way to tackle this? I want to take the derivative of the temperature readings every 2 seconds.

Here is myTimerEvent() code:

void myTimerEvent()
{
  MAX6675 ktc(ktcCLK, ktcCS, ktcSO);

  int REFRESH_INTERVAL = 1000*2; // ms
  

  double temp[2];
  static unsigned long lastRefreshTime = 0;
  static unsigned long lastRefreshTime2 = 0;
  double deriv;

    if(millis() - lastRefreshTime >= REFRESH_INTERVAL)
    {
      lastRefreshTime += REFRESH_INTERVAL;
      temp[0]  = ktc.readFahrenheit();
    }
    else if(millis() - lastRefreshTime2 >= 2*REFRESH_INTERVAL)
    {
      lastRefreshTime2 += 2*REFRESH_INTERVAL;
      temp[1]  = ktc.readFahrenheit();
      }

  deriv = (temp[1] -temp[0])/(2.000);
  Blynk.virtualWrite(V6, deriv);

}

I would appreciate any help. Thank you.

ESP32_Thermo1_mar09a.ino (1.82 KB)

However, I am having issues since this spits out a value and then zeros out in the middle.

I'm sure that you know what this means, but I sure don't.

What value is "spit out"? What "zeroes out in the middle"? "The middle" of what?

Please edit your post to replace quote tags with code tags.

The derivative is (second reading - first reading)/(time between readings).

PaulS:
I'm sure that you know what this means, but I sure don't.

What value is "spit out"? What "zeroes out in the middle"? "The middle" of what?

Sorry should have been more clear. Here is the photo. photo of the results I get for the derivative. It seems that I am getting wrong values even at those peaks.

jremington:
Please edit your post to replace quote tags with code tags.

The derivative is (second reading - first reading)/(time between readings).

Thanks for the suggestion. Sorry this is my first post in this forum.
And for the derivative that is exactly what I used, but I am getting strange results, which I think mainly is because the first and second reading change outside the loop at a few instances of time. My question is how to implement that so that this doesn't happen.

There is no sense of calculating the derivative of the curve shown in your link. You should first get correct measurements before trying to get the derivative.

lesept:
There is no sense of calculating the derivative of the curve shown in your link. You should first get correct measurements before trying to get the derivative.

I measured the temperature and it worked fine. Now, my next process is to get the derivative. Sorry, I should have mentioned that.

The approach below certainly won't work, since you calculate the derivative regardless of whether one or both measurements has been updated. The value you calculate is usually meaningless.

Best to just throw this out and start over.

    if(millis() - lastRefreshTime >= REFRESH_INTERVAL)
    {
      lastRefreshTime += REFRESH_INTERVAL;
      temp[0]  = ktc.readFahrenheit();
      Blynk.virtualWrite(V4, temp[0]);
    }
    else if(millis() - lastRefreshTime2 >= 2*REFRESH_INTERVAL)
    {
      lastRefreshTime2 += 2*REFRESH_INTERVAL;
      temp[1]  = ktc.readFahrenheit();
      Blynk.virtualWrite(V5, temp[1]);
      }

  deriv = (temp[1] -temp[0])/(2.000);

Simplest (but blocking) approach:

temp0 = ktc.readFahrenheit();
delay(2000);
temp1 = ktc.readFahrenheit();
deriv = (temp1-temp0)/2.0;  //change per second

jremington:
The approach below certainly won't work, since you calculate the derivative regardless of whether one or both measurements has been updated. The value you calculate is usually meaningless.

Best to just throw this out and start over.

    if(millis() - lastRefreshTime >= REFRESH_INTERVAL)

{
      lastRefreshTime += REFRESH_INTERVAL;
      temp[0]  = ktc.readFahrenheit();
      Blynk.virtualWrite(V4, temp[0]);
    }
    else if(millis() - lastRefreshTime2 >= 2REFRESH_INTERVAL)
    {
      lastRefreshTime2 += 2
REFRESH_INTERVAL;
      temp[1]  = ktc.readFahrenheit();
      Blynk.virtualWrite(V5, temp[1]);
      }

deriv = (temp[1] -temp[0])/(2.000);




Simplest (but blocking) approach:


temp0 = ktc.readFahrenheit();
delay(2000);
temp1 = ktc.readFahrenheit();
deriv = (temp1-temp0)/2.0;  //change per second

Nice! This was exactly what I was looking for. I guess I over complicated this, haha. Thank you so much.