DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

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.

Gunnar Peipman user avatar by
Gunnar Peipman
·
Feb. 03, 16 · Tutorial
Like (1)
Save
Tweet
Share
6.38K Views

Join the DZone community and get the full member experience.

Join For Free

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   )   =    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

  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:

  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, . 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

  1.  brewing beer with a raspberry pi: measuring temperature 
  2.  brewing beer with a raspberry pi: moving to itemperatureclient interface 
  3.  brewing beer with a raspberry pi: measuring cooling rate 
  4.  brewing beer with a raspberry pi: making cooling rate calculations testable 
  5.  brewing beer with a raspberry pi: reporting measurements to azure iot hub 
  6.  brewing beer with raspberry pi: stream analytics 
  7.  brewing beer with raspberry pi: visualizing sensor data   
  8. brewing beer with raspberry pi: building a universal windows application
Measurement (journal)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Cloud Performance Engineering
  • MongoDB Time Series Benchmark and Review
  • How to Use Buildpacks to Build Java Containers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: