#include <math.h>
const int NTC_PIN = A0;
const float VREF = 5.0;
const float ADC_MAX = 1023.0;
const float FIXED_RESISTOR = 10000.0;
const float R0 = 10000.0;
const float T0_K = 298.15;
const float BETA = 3950.0;
const unsigned long SAMPLE_INTERVAL_MS = 1000;
unsigned long lastSampleTime = 0;
const char* getStatus(float temperatureC) {
if (temperatureC >= 50.0) {
return "FAIL";
} else if (temperatureC >= 40.0) {
return "WARN";
} else {
return "NORMAL";
}
}
void setup() {
Serial.begin(9600);
Serial.println("time_ms,adc_value,voltage,resistance_ohms,temperature_c,status");
}
void loop() {
unsigned long now = millis();
if (now - lastSampleTime >= SAMPLE_INTERVAL_MS) {
lastSampleTime = now;
int adcValue = analogRead(NTC_PIN);
float voltage = adcValue * VREF / ADC_MAX;
float safeVoltage = voltage;
if (safeVoltage <= 0.001) {
safeVoltage = 0.001;
}
if (safeVoltage >= VREF - 0.001) {
safeVoltage = VREF - 0.001;
}
float resistanceOhms = FIXED_RESISTOR * safeVoltage / (VREF - safeVoltage);
float temperatureC = 1.0 / (
log(1.0 / (1023.0 / adcValue - 1.0)) / BETA + 1.0 / T0_K
) - 273.15;
const char* status = getStatus(temperatureC);
Serial.print(now);
Serial.print(",");
Serial.print(adcValue);
Serial.print(",");
Serial.print(voltage, 3);
Serial.print(",");
Serial.print(resistanceOhms, 1);
Serial.print(",");
Serial.print(temperatureC, 2);
Serial.print(",");
Serial.println(status);
}
}