MAX31855:如何跳过无效值

使用 MAX31855 作为热电偶放大器和数字化器有时会导致无效值,可能是由于模拟(热电偶)端的噪声或数字信号完整性问题如 GND 噪声。

此代码修改自我们关于 使用 PlatformIO 的 ESP32 MAX31855 热电偶 LCD 最小示例 的文章,提供了如何跳过和忽略这些值的示例,但如果超过 10 个(可配置)连续值无效则报告错误。

根据你的应用,你可能需要在 IsValidReading() 中选择更合适的温度范围。选择尽可能窄的有效范围将避免更多坏值被发送到下游代码。

max31855_skip_invalid.cpp
SPIClass vspi(VSPI);
Adafruit_MAX31855 thermocouple1(Pin_MAX31855_A_CS, &vspi);

bool IsValidReading(double reading) {
  return !isnan(reading) && reading > -40.0 && reading < 1000.0;
}

void ReadTemperature() {
  double celsius = thermocouple1.readCelsius();
  if(!IsValidReading(celsius)) {
    consecutiveNaNReads++;
    if(consecutiveNaNReads >= maxConsecutiveNaNReads) {
      // Thermocouple is disconnected
      Serial.println("Thermocouple error");
    }
  } else {
    consecutiveNaNReads = 0;
    Serial.println(celsius);
  }
}

Check out similar posts by category: C/C++, Electronics