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 an ideal world where it is always the same temperature outside. We make this idealization just to get going with the mathematics part of our beer IoT solution. I have two thermal sensors connected to the RaspberryPi. One of them measures beer temperature and the other measures ambient temperature.

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 through all of the differential equations stuff here–you can find it with good explanations from the . I'll start with an equation we can use to calculate the temperature of beer at a given moment of time:

T(t)=Ta+(T0−Ta)e^−kt

Key:

t – time

T(t) – temperature at given time

T a – ambient temperature

T 0 – 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 a given beer.

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

Example of Newton’s Law of Cooling

Picture borrowed from

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

the closer beer and ambient temperatures are the slower cooling will be

the bigger the difference between ambient and expected beer temperature the faster cooling will be

Those who want to go through all of the calculation fun, I suggest to go with .

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 the equation for cooling rate based on the equation above:

k=[ln([T0−Ta]/[T(t)−Ta])] / 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 of 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 our beer at a temperature of 22°C. It’s 8°C outside or in the room where we start cooling our beer down. We wait for 2 hours and measure the temperature of our beer again. Now it’s 19°C.

We have the following data now:

T a = 8°C

T 0 = 22°C

T(t) = 19°C

t = 120 min

Using the equation above, we can calculate the cooling rate:

k = ln([22−8]/[19−8]) / 120 = ln(14/11) / 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:

Measure ambient and initial beer temperature. Wait for five minutes. Measure beer temperature now. Calculate cooling rate.

N.B. We are using the code from my previous IoT post, . Here, we are making just some simple updates to the Startup class.

As our temperature sensors have measuring context, we have to identify what each of the sensors is measuring, For this, we add 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 a 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 our first measurement so it is available when we start calculating cooling rate. We also need to save 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 for 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 the cooling rate of our beer. To test this class you can write ITemperatureClient implementation that returns to you exactly the temperatures you need.

Wrapping Up

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

Brewing Beer With a Raspberry Pi: Table of Contents