Connected Kettle: Temperature reading

2020-06-25

With the Connected Kettle: Enclosure complete, it's time to add more functionality to the connected kettle. This time, using a thermistor to sense and report on the temperature of the kettle. Is it still warm, or does it need to be reboiled? Stage 3 of this project is to attach the thermistor and use some neat tricks to interpret the thermistor's resistance values as temperature readings.

Hardware assembly

The hardware aspect of this stage is relatively simple, as the thermistor is just a resistor that responds to temperature. The eyelet on the end of the thermistor can be screwed into the handle attachment of the kettle, effectively connecting the thermistor directly to the metal body of the kettle. This isn't a perfect solution (as opposed to sensing the internal temperature), but it is a suitable proxy for the temperature of the water inside the kettle.

Astute readers will have identified a problem with this setup: If the thermistor is permanently connected to the kettle, and needs to be wired up to the ESP32 in order for it to operate, how do we keep the kettle untethered from the enclosure? The current setup means that the thermistor terminals will need to be disconnected and reconnected every time the kettle is removed and returned. Connected Kettle: Handle attachment introduces a better solution to this problem.

Using the thermistor

Given the simplicity of the thermistor circuit, the complexity comes from the code required to transform a resistance value into a temperature. While I don't fully understand the equation below (it was approximated from a number of sources online), I will explain the constants used in the eqution.

_SAMPLE_RATE = 5;
Each sample reading can be wildly different, so we collect a group of samples and then average them out.
_MAX_READ_VALUE = 4095;
When acting as an 'input', the VP pin translates readings to values between 0 (0v) and 4095 (5v).
_SERIES_RESISTOR_OHMS = 100000;
The resistance value of the "series" resistor in the circuit. In this case, it is 100KΩ.
_THERMISTOR_NOMINAL_OMHMS = 100000;
The resistance value of the thermistor itself when at the nominal temperature (see below). For this thermistor, it is 100KΩ.
_TEMPERATURE_NOMINAL_DEGREES = 25;
The nominal temperature (normally "room temperature") that results in the nominal thermistor temperature in °C (see above).
_BETA_COEFFICIENT = 3380;
A divisor to help scale the resistance value to celcius and kelvin values.
_ZERO_DEGREES_KELVIN = -273.15;
Absolute zero temperature.

With an approximate temperature known, the "temperature" component of the program is basically complete. The code below uses the Steinhart-Hart equation, which is effective for approximating temperature values from thermistor resistance.

$ cat thermistor.cpp
    
#include "thermistor.h"

/* ... */

Thermistor::Thermistor(const int thermistor_pin): _thermistor_pin(thermistor_pin) {};

float Thermistor::read_sample() {
  float samples = 0;
  for (int i = 0; i < _SAMPLE_RATE; ++i) {
    samples += analogRead(_thermistor_pin);
    delay(10);
  }

  float resistance = _MAX_READ_VALUE / (samples / _SAMPLE_RATE) - 1;
  return _SERIES_RESISTOR_OHMS / resistance;
}

float Thermistor::read() {
  float resistance = read_sample();

  float value = resistance / _THERMISTOR_NOMINAL_OMHMS;
  value = log(value);
  value = (1.0 / _BETA_COEFFICIENT) * value;
  value = (1.0 / (_TEMPERATURE_NOMINAL_DEGREES - _ZERO_DEGREES_KELVIN)) + value;
  value = 1.0 / value;

  return value + _ZERO_DEGREES_KELVIN;
}
  

Take-aways

  • For simple hardware, the complexity comes from the software. This is why it's nice to have hardware controllers do the work when possible.
  • The sample values of the thermistor lag behind the temperature of the water in the kettle by several seconds. Instead of declaring the kettle "boiled" when it reaches 100°C, it's faster to make that call once it reaches 90°C.

Next steps

Now the temperature and weight can be both be shown on the LCD, our data capture work is now complete. It is annoying to need to disconnect and reconnect the thermistor just to move the kettle, so Connected Kettle: Handle attachment introduces a better hardware interface between the attachment and the enclosure base.

This article is part of the Connected Kettle set.


Feedback? Questions? Email me