Over a million developers have joined DZone.

Brewing Beer With a Raspberry Pi: Measuring Cooling Rate

DZone's Guide to

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.

· IoT Zone
Free Resource

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 Newton’s Law of Cooling page by the University of British Columbia. I'll start with an equation we can use to calculate the temperature of beer at a given moment of time:



  • 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 a given beer.

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

    Example of Newton’s Law of Cooling

    Example of Newton’s Law of Cooling

    Picture borrowed from University of British Columbia

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

    k=[ln([T0Ta]/[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.


    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:

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

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

    ln([228]/[198]) / 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:

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

    N.B. 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 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;
        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)
        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.

    internet of things ,math ,raspberry pi

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

    Opinions expressed by DZone contributors are their own.


    Dev Resources & Solutions Straight to Your Inbox

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}