Beer IoT: Measuring cooling rate

As thermal sensors are connected and we have code to read temperatures it’s time to get serious and start real work on supporting the cooling process of eisbock. We start with measuring temperatures, calculating cooling rate and estimating how long it takes for beer to freeze. This post focuses on cooling rate.

In this blog post we are living in ideal world where there is always same temperature outside. We make this idealization just to get going with mathematics part of beer IoT solution. I have two thermal sensors connected to RaspberryPi. One of them measures beer temperature and the other measures ambient temperature.

Update. Source code of my TemperatureStation solution is available at Github. The solution contains IoT background service and web application you can use right away. Readings are reported to MSSQL or Azure IoT Hub. Documentation is available at TemperatureStation wiki. Feel free to try out the solution.

Newton’s law of cooling

Freezing of beer is covered by Newtons’s law of cooling:

The rate of heat loss of a body is proportional to the difference in temperatures between the body and its surroundings while under the effects of a breeze.

I don’t go here through all differential equations stuff – you can find it with good explanations from Newton’s Law of Cooling page by University of British Columbia. I start with equation we can use to calculate temperature of beer at given moment of time:

$$T(t)=T_a+(T_0-T_a)e^{-kt}$$

where

t – time
T(t) – temperature at given time
Ta – ambient temperature
T0 – initial temperature of beer
k – cooling rate

For our measurement k is constant because things like shape of container, chemical content of beer and thermal properties of container are all constants through our process. As k is not the same for different beers it is constant for given beer.

To have better understanding of cooling let’s see the following chart:

Example of Newton’s Law of CoolingExample of Newton’s Law of Cooling
Picture borrowed from
University of British Columbia

We see that there is exponential relation between time and temperature at given time. We can make some conclusions:

  • the closer are beer and ambient temperatures the slower is cooling,
  • the bigger the difference between ambient and expected beer temperature the faster cooling goes.

Those who want to go through all the calculation fun I suggest to go with Newton’s Law of Cooling by University of British Columbia.

Finding cooling rate

We start with measuring cooling rate as we need it for estimating how long it takes for beer to start freezing. First we find equation for cooling rate based on equation above:

$$k=\frac{ln\left(\frac{T_0-T_a}{T(t)-T_a}\right)}{t}$$

To find k we need the following data:

  • ambient temperature (here it’s constant),
  • initial temperature of beer,
  • current temperature of beer,
  • time between these two measurements.

All this data is available for us – we just have to make two measurements with both sensors to find initial temperatures and temperatures after some short period of time.

Example

Suppose we have beer at temperature 22°C. It’s 8°C outside or in a room where we start cooling beer down. We wait for 2 hours and measure the temperature of beer again. Now it’s 19°C.

We have the following data now:

Ta = 8°C
T0 = 22°C
T(t) = 19°C
t = 120 min

Using the equation above we can calculate cooling rate:

$$k=\frac{ln\left(\frac{22-8}{19-8}\right)}{120}=\frac{ln\left(\frac{14}{11}\right)}{120}=0.002$$

Measuring cooling rate

Now let’s implement this in our code. Here is how we go step by step with finding the cooling rate of beer:

  1. Measure ambient and initial beer temperature.
  2. Wait for five minutes.
  3. Measure beer temperature now.
  4. Calculate cooling rate.

NB! We are using the code from my previous IoT post Windows 10 IoT Core: Moving to ITemperatureClient interface. Here we are making just some simple updates to Startup class.

As our temperature sensors have measuring context we have to identify what each of sensors is measuring, For this we add to new constants to Startup class.


private const string AmbientSensorId = "SENSOR ID HERE";
private const string BeerSensorId = "SENSOR ID HERE";


We need to measure cooling rate k once for freezing process but we want to use it every time we create new estimate for cooling time. Measuring of k means two measurements of beer temperature and there’s some time interval before these two measurements. As there are two related measurements we need to save data of first measurement so it is available when we start calculating cooling rate. We need to save also ambient temperature because we need it later when calculating estimates. Here are additional fields we need in class scope:

  • k – cooling rate
  • time of first measurement
  • initial temperature of beer
  • ambient temperature
  • is first measurement done
  • is cooling rate measurement done

We add the following fields to Startup class.


private double _k = 0;
private DateTime _firstMeasurementTime;
private double _Ta = 0;
private double _T0 = 0;
private bool _initialMeasurementsDone;
private bool _kMeasurementDone;


Now we need two steps method to measure and calculate cooling rate. For this we add the following method to Startup class.


private void MeasureCoolingRate(IEnumerable<Measurement<double>> temps)
{
    if (!_initialMeasurementsDone)
    {
        var ambient = temps.First(t => t.DeviceId == AmbientSensorId);
        _Ta = ambient.Value;
        _T0 = temps.First(t => t.DeviceId == BeerSensorId).Value;
 
        _firstMeasurementTime = ambient.Time;
        _initialMeasurementsDone = true;
        return;
    }
 
    var timeDelta = (temps.First().Time - _firstMeasurementTime).TotalMinutes;
    if (timeDelta >= 5)
    {
        var beer = temps.First(t => t.DeviceId == BeerSensorId);
        var d1 = _T0 - _Ta;
        var d2 = beer.Value - _Ta;
        _k = Math.Log(d1 / d2) / timeDelta;
 
        _kMeasurementDone = true;
 
        Debug.WriteLine("k = " + _k);
    }
}


As we want to measure k only once for cooling process we need to run this method for every measurement until k is found. If we take measurements after every five seconds but for cooling rate we wait by example 30 minutes between measurements we get load of “empty” runs of MeasureCoolingRate method. We have to consider this in our measurements callback.


private void TemperatureCallback(object state)
{
    var now = DateTime.Now;
    var temps = _tempClient.Read();
 
    if (!_kMeasurementDone)
        MeasureCoolingRate(temps);
 
    foreach (var temp in temps)
    {
        Debug.WriteLine(now + " " + temp.DeviceId + ": " + temp.Value);
    }
}


Now we are ready to find cooling rate of beer. To test this class you can write ITemperatureClient implementation that returns you exactly the temperatures you need.

Wrapping up

This was our first practical solution for thermal solution – we found cooling rate of our beer. Although the math side is not very easy on theory level we were able to find equation that was easy enough to use in our code with some measurement data. Next blog post focuses on estimating the time it takes for beer to start freezing.


2 thoughts on “Beer IoT: Measuring cooling rate

Leave a Reply

Your email address will not be published. Required fields are marked *