# Brewing Beer With a Raspberry Pi: Measuring Cooling Rate

### Using a Raspberry Pi and two thermal sensors, we're going to make ourselves some beer. We'll start by calculating the cooling rate of our beer.

Join the DZone community and get the full member experience.

Join For Freeas 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 ) = t a + ( t 0 − t a ) e^ − k t *

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 =[ l n([ t 0 − t a]/[ t ( t ) − t a] )] / 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 = l n ([ 22 − 8]/[ 19 − 8] ) / 120 = l n ( 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

- brewing beer with a raspberry pi: measuring temperature
- brewing beer with a raspberry pi: moving to itemperatureclient interface
**brewing beer with a raspberry pi: measuring cooling rate**- brewing beer with a raspberry pi: making cooling rate calculations testable
- brewing beer with a raspberry pi: reporting measurements to azure iot hub
- brewing beer with raspberry pi: stream analytics
- brewing beer with raspberry pi: visualizing sensor data
- brewing beer with raspberry pi: building a universal windows application

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Comments