Dynamic carbonation calculator

A printed carbonation chart is right only for the conditions it assumes, and most brewers aren't in them. This is how I replaced one with a browser calculator that corrects for elevation and ABV and runs inside a Magento page with no dependencies.

Topic
E-commerce tooling
Built with
JavaScript, HTML and CSS, no dependencies
Status
Live on a supplier's storefront

Overview

Brewers set carbonation targets in volumes of CO₂ or grams per liter. To hit a target, brewers need to know the right pressure to apply at a given temperature. For decades the answer has been a laminated chart on the wall of a homebrew shop, or a PDF someone found on a forum in 2009.

The equations are standard. A static chart, though, assumes conditions that most users aren't in, and its precise-looking numbers are often wrong for them.

Note. I built this for a brewing and winemaking supplier, to embed directly on product pages in their Magento storefront. The aim was fewer support questions and a better answer for customers at the moment they need one.

The problem

Standard carbonation charts assume a narrow set of conditions, and outside them the chart gives the wrong number without any warning. Most real users are outside them.

What charts assume

A static chart assumes

  • Sea level atmospheric pressure
  • Standard ambient conditions
  • No alcohol content (water-based solution)
  • One target unit (volumes or g/L, not both)

Real users have

  • Variable elevation (Colorado is 5,000+ ft)
  • Different cellar and keezer temps
  • Beers from 4% to 12%+ ABV
  • Mixed unit preferences

The real-world gap

At altitude, atmospheric pressure is lower, and a regulator gauge reads pressure above the local air, not absolute pressure. So the same gauge setting pushes less CO₂ into the beer. A brewer in Denver following a sea-level chart will consistently undercarbonate. A higher-ABV beer has a different CO₂ solubility profile than water, which shifts the result further.

Most users are in at least one of these situations. The published formulas already correct for both. A printed chart just never asks for elevation or ABV.

Example. At 5,280 ft the air is about 2.6 PSI thinner than at sea level. At 38°F, 2.5 volumes takes 11.2 PSI on a sea-level chart but 13.8 PSI in Denver. Set 11.2 there and you get about 2.25 volumes, a noticeably flat beer.

What I built

A browser-based carbonation chart that rebuilds itself for your conditions. Enter your elevation and ABV and pick your units. Every cell, with beer temperature down the side and regulator pressure across the top, then shows the carbonation you will actually get.

Parameter Type Why it matters
Elevation Input Air pressure drops with altitude. The gauge reads above local air, so the same setting carbonates less the higher you are
ABV Input Alcohol changes CO₂ solubility relative to water; a printed chart is fixed at one ABV
Units Input Volumes with PSI and °F, or g/L with bar and °C, whichever the recipe uses
Carbonation chart Output Carbonation at equilibrium for every beer temperature (30 to 65°F) and regulator setting, shaded by style range
Elevation offset Output How many PSI to add to a sea-level chart's setting for the same carbonation

There's no backend and no API call. The calculation runs entirely in the browser, so it loads instantly and can be dropped into any Magento CMS block or product description without touching the app layer.

Technical approach

It couldn't touch the app layer, need a build step or load anything external, and it had to work on a phone in a brewery. So it is one self-contained HTML file.

The core formula

The chart uses the standard empirical carbonation equation, which gives sea-level gauge pressure from beer temperature and volumes of CO₂, solved the other way round for volumes. On top of that, the elevation correction uses the barometric formula to convert each local gauge reading into the sea-level reading that would put the same CO₂ in the beer, and the ABV correction adjusts the result.

JavaScript
// Air pressure at elevation, in PSI (barometric formula, feet)
function atmPsiAtElevationFt(ft) {
  return 14.696 * Math.pow(1 - 6.8754e-6 * ft, 5.2559);
}

// A gauge reads pressure above local air. Convert a local reading
// to the sea-level gauge pressure that holds the same CO2.
function seaLevelEquivalentPsi(gaugePsi, ft) {
  return gaugePsi - (14.696 - atmPsiAtElevationFt(ft));
}

// Standard carbonation equation (sea level), solved for volumes V:
// P = -16.6999 - 0.0101059T + 0.00116512T^2 + 4.24267V - 0.0684226V^2 + 0.173354TV
function co2Volumes(tempF, psi) {
  const T = tempF, P = Math.max(0, psi);
  const a = -0.0684226;
  const b = 0.173354 * T + 4.24267;
  const c = -16.6999 - 0.0101059 * T + 0.00116512 * T * T - P;
  const disc = b * b - 4 * a * c;
  if (disc <= 0) return 0;
  const roots = [(-b + Math.sqrt(disc)) / (2 * a), (-b - Math.sqrt(disc)) / (2 * a)]
    .filter(r => r > 0 && r < 10);
  return roots.length ? Math.min(...roots) : 0;
}

// ABV correction used in the tool (BevSense Dynamic Henry's Law; Dalmolin et al. 2006)
function abvAdjustedVolumes(vol, abv) {
  const abw = abv * 0.00789;
  return vol * (1 + 0.211 * abw);
}

// One cell of the chart
const vol = abvAdjustedVolumes(co2Volumes(tempF, seaLevelEquivalentPsi(gaugePsi, elevationFt)), abv);

Deployment

Magento's CMS allows raw HTML blocks on product and content pages. Because the calculator is one file with nothing external to load, embedding it takes a paste into a CMS block, with no module or extension to install.

  1. Author the calculator as a standalone HTML file with inline styles and script.
  2. Paste the markup into a Magento CMS block via the admin panel.
  3. Assign the block to the relevant product page or category landing page.
  4. The tool renders immediately, with no CDN, runtime, or API quota to worry about.
Note. A client-side tool with no dependencies can't go down separately from the storefront: there is no third-party service, rate limit or cold start to fail.

Outcome

The calculator replaced a static chart that had been linked from product pages for years. Customers now get the setting for their own conditions instead of reading a reference and adjusting by guesswork.

What improved

  • More accurate carbonation setups for customers at altitude
  • Higher-ABV beers dialed in correctly on the first attempt
  • Volumes and g/L in one place
  • Embeds anywhere Magento renders HTML

What stayed the same

  • The chemistry: no new math needed
  • Deployment: still a paste into a CMS block
  • Page load: no measurable change

Why it worked

The equation and both corrections were already published. The calculator puts them in front of the customer at the moment they need a number. A brewer in Denver enters an elevation and gets a different, correct setting from the one the chart gave.

Often the cheapest fix a business can make is getting information it already has to the person who needs it.