jnsbyr / esp8266-intexsbh20

MQTT WiFi remote control for the Intex PureSpa SB-H20 and SJB-HS whirlpools
Other
64 stars 20 forks source link

Temperature sensor #21

Closed louis49 closed 2 years ago

louis49 commented 2 years ago

Hi,

I tried the temperature sensor part and discover 2 things :

then, when computing temperature :

const int NTC_SAMPLES = 100;
const int NTC_PIN = A0;
const float NTC_BETA = 4300.f; // from https://cdn-reichelt.de/documents/datenblatt/B300/B57164K_ENG_TDS.pdf
const float NTC_VOLTAGE = 3.3f;
const float NTC_NOMINAL_RESISTANCE = 10000.f;
const float NTC_NOMINAL_TEMPERATURE = 25.f;
const float NTC_REF_RESISTANCE = 21600.f;
const float NTC_A0_RESISTANCE = 320000.f;

ADC_MODE(ADC_TOUT)

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

void loop() {
 float average = 0.0f;
  for (uint8_t i=0; i<NTC_SAMPLES; i++) {
    average += analogRead(NTC_PIN);
    delay(10);
  }

  float V_OUT = average / NTC_SAMPLES;
  V_OUT /= 1024;
  V_OUT *= NTC_VOLTAGE;

  float PAR_EQ = (NTC_REF_RESISTANCE*NTC_A0_RESISTANCE)/(NTC_REF_RESISTANCE+NTC_A0_RESISTANCE);  
  float DIV_R2 = (V_OUT/NTC_VOLTAGE) * PAR_EQ/(1 - (V_OUT/NTC_VOLTAGE));

  float t_k = 1 / (
    (1.f / (NTC_NOMINAL_TEMPERATURE + 273.15f)) + 
    (1.f / NTC_BETA) *
    (log(DIV_R2 / NTC_NOMINAL_RESISTANCE))
    );
    float t_c = t_k - 273.15f;

  LOG_D("V_OUT : %f ; Resistance : %f ; %f ; %f", V_OUT, PAR_EQ, DIV_R2, t_c);
}

Have you observed such a thing?

jnsbyr commented 2 years ago

The temperature sensor in this project was an afterthought. I added it because it was easy and because it might be good to know how hot it gets inside an waterproof case - but accuracy was never a design goal here.

The Wemos D1 has an internal voltage divider at its analog input, so yes this will affect the mathematics for the temperature. The internal resistance (320 k) is higher by a factor of more than 10 in comparison to R2 (22 k). So this changes the effective resistance by less than 10 %. As mentioned before - accuracy was not important and the the initial accuracy of my setup was better than expected. If you follow the calibration procedure I described in the README.md it will not matter if you swap R1 and R2 or not.

If you want to go for higher accuracy you should also consider the following aspects:

You can optimize the resistor values and maybe switch R1 and R2, but with the ESP8266 you will loose at the 3rd point I mentioned above because of its 10 bit resolution. You can compensate this with high oversampling but some limits remain.

In another project of mine I added a ADS1115 to the Wemos D1, used a differential voltage measurement combined with a reference voltage auto calibration and the difference in accuracy in comparison to the build in ADC of the ESP8266 was really significant.