CC Blog Projects Research & Design Hub

Barometer

Written by Jeff Bachiochi

Under (Air) Pressure

Changes in barometric pressure are commonly used to predict the weather. In this article, I introduce different types of barometers, then create an application to measure and display barometric pressure using a tiny MEMS barometer. Finally, I describe a project that predicts local weather based on the previous and current barometric pressures.

  • How can I create a barometer application?
  • What DIY projects can I build with a MEMS barometer?
  • What can I do with a HP206C I2C Precision Barometer and Altimeter?
  • HP206C I2C Precision
  • Barometer and Altimeter

According to the Farmers’ Almanac [1], “The idea that body aches can predict the weather is a very ancient bit of weather lore. While not every piece of old weather lore is true, the evidence seems to suggest that this one is based in fact. As far back as the 1960s, medical researchers have found, over and over again, that there is a genuine connection between increased pain and cold, wet weather. While the effect is most commonly linked to arthritis sufferers, many have also reported feeling increased pain from nerve disorders, recently healed fractures, migraines, toothaches, corns, and even scars, when the weather was about to change. No one knows exactly what causes aches and pains to flare up, but the most likely culprit is the drop in atmospheric pressure that occurs right before a storm begins. This shift in air pressure may be enough to dilate the blood vessels in the body, stimulating the nerve endings in sensitive areas, like sore feet, creaky knees, or bad teeth.”

My wife, Beverly, predicts the weather by her knee pain—more achiness, more swelling, and sometimes a throbbing sensation. It usually happens before it rains. But, the rain just happens to coincide with changes in barometric pressure.

Barometric pressure is a measure of the weight of the air in the atmosphere pressing down against us. On average, the atmosphere exerts 14.7 pounds per square inch on the Earth’s surface. But as the weather changes, that pressure can bounce up and down.

Decreasing pressure, which typically brings on worsening weather, means the air presses a little less on our bodies. That lack of compression allows tissues within our bodies to swell slightly, which can irritate joints. The speed at which the pressure changes also makes a difference. A sudden drop in pressure as a storm blows into town creates more noticeable aches than does a slow, gradual pressure decline.

BAROMETRIC PRESSURE

In the mid-1600s, Gasparo Berti uncapped a long, full tube of water in a water-filled bowl, and found that after an initial drop, the water level in the tube remained constant at about 34’. While this experiment was used to investigate why well pumps would not raise water over 34’ in height, it led to the invention of the “weather glass” or “Goethe barometer,” named for Johann Wolfgang von Goethe. The heavier liquid mercury soon replaced water as the measurement medium. The mercury barometer’s design (Figure 1) gives rise to the expression of atmospheric pressure in millimeters of mercury (mmHg). The pressure is quoted as the level of the mercury’s height in the vertical column, typically, between 670mm and 800mm of Hg. One atmosphere (1atm) of pressure is equivalent to 29.92” (760mm) of mercury. FYI: In 2007, a European Union directive was enacted to restrict the use of mercury in new measuring instruments intended for the general public, due to its toxicity.

An “aneroid barometer” was invented in 1844 by French scientist, Lucien Vidi. The model shown in Figure 2 was patented in the US in 1945 by Ira E. McCabe. It measures air pressure using a method that does not involve a liquid. Instead, a small flexible metal box called an “aneroid cell” (capsule) has its air evacuated, but is prevented from collapsing by a strong spring. Changes in external air pressure cause the cell to expand or contract. This expansion and contraction drive mechanical levers, such that the tiny movements of the capsule are amplified and displayed as needle movement on the face of the barometer. (I’ve had one of these on my wall for years.)

Today’s micro-electro-mechanical systems (MEMS) barometric pressure sensors are extremely small devices, about the size of a pea. For this project, I’ll be investigating the HP206C I2C Precision Barometer and Altimeter (Figure 3) by Shenzhen Hope Microelectronics Co., Ltd. (HopeRF). The sensor includes pressure and temperature outputs that are digitized by a high-resolution, 24-bit ADC. The altitude value is calculated internally by algorithm, according to the pressure and temperature data. You can see the internals in the block diagram in Figure 4.

FIGURE 1
The first experiments on air pressure used water as the liquid of choice. They required extremely long tubes—14.7 pounds worth, since that was the pressure of 1atm. Shorter tubes could be used when the heavier liquid, mercury, replaced water [2], since it is 13.5 times denser than water.
FIGURE 1
The first experiments on air pressure used water as the liquid of choice. They required extremely long tubes—14.7 pounds worth, since that was the pressure of 1atm. Shorter tubes could be used when the heavier liquid, mercury, replaced water [2], since it is 13.5 times denser than water.
FIGURE 1
The first experiments on air pressure used water as the liquid of choice. They required extremely long tubes—14.7 pounds worth, since that was the pressure of 1atm. Shorter tubes could be used when the heavier liquid, mercury, replaced water [2], since it is 13.5 times denser than water.
FIGURE 2
Mechanical barometers are still popular today. Air pressure changes one dimension of a cleverly designed sealed container. This movement controls a pointer that indicates the pressure on a calibrated gauge. The one shown here was patented in 1945 by Ira E. McCabe [3].
FIGURE 3
The MEMS sensor requires power to calculate the pressure of its internal full-bridge sensor. It's small size and extreme accuracy allow it to be used in a variety of pressure- and altitude-sensing applications. The HopeRF model HP206C that I investigated is shown here [4].
FIGURE 3
The MEMS sensor requires power to calculate the pressure of its internal full-bridge sensor. It’s small size and extreme accuracy allow it to be used in a variety of pressure- and altitude-sensing applications. The HopeRF model HP206C that I investigated is shown here [4].
FIGURE 4
This block diagram of the HP206C shows more than just a pressure sensor. Amplification, sampling, filtering, compensation, storage, I2C interface and alarm interrupts are packed into this device. Redrawn from HopeRF [4]
FIGURE 4
This block diagram of the HP206C shows more than just a pressure sensor. Amplification, sampling, filtering, compensation, storage, I2C interface and alarm interrupts are packed into this device. Redrawn from HopeRF [4]

This device can initiate an interrupt automatically, if pressure, altitude, or temperature trip high or low boundaries (which you can set). It also has an offset register that you can use to correct the altitude output for your local elevation. I will not be using these features here, however. To simplify the explanation, I will only be discussing one command available to this device, “read the pressure.”

Reading pressure (or temperature or altitude) take on the same format. To read the pressure we issue an I2C address write (0xEC) with the command 0x30 (read the pressure). The HP206C will then prepare a reply with 3 bytes, indicating the 24-bit pressure (MSB first) in millibars (mbr). (One mbr equals 0.750062mmHg.) To receive these bytes, we must then issue an I2C address read (0xED), and then clock in the 3-byte conversion. If you wish to retrieve altitude or temperature, just change the command byte based on Table 1 and Table 2.

table 1
The HP206C has multiple modes of operation. The simplest is to write one of these Commands and then read the 24-bit reply of 3 bytes. The 7-bit I2C device address is 0x76. Source: HP206C Data Sheet, Table 6 [5].
table 1
The HP206C has multiple modes of operation. The simplest is to write one of these Commands and then read the 24-bit reply of 3 bytes. The 7-bit I2C device address is 0x76. Source: HP206C Data Sheet, Table 6 [5].
table 2
The control registers hold an optional Altitude offset and Interrupt threshold levels, as well as some control and status bits. In this application, we need only to test for Bit 6 of the INT_SRC register to determine that the device is ready and communicating. Source: HP206C Data Sheet, Table 8 [5]
table 2
The control registers hold an optional Altitude offset and Interrupt threshold levels, as well as some control and status bits. In this application, we need only to test for Bit 6 of the INT_SRC register to determine that the device is ready and communicating. Source: HP206C Data Sheet, Table 8 [5]
ARDUINO CODE

With a little bit of setup and routines to write and read I2C devices (Listing 1), we have a basic application to read and report pressure values. This avoids using any HP206 libraries. Note: Code for this project can be downloaded from the Circuit Cellar Article Materials and Resources webpage. Execution of this code should display the sign-on message and then repeated pressure measurements as such:

ftb396 HP206 Read Barometer
HP206 is ready
Pressure:992mbars
Pressure:992mbars

OK, so that’s it. Well, not exactly. I don’t like to stop without creating a project using a presented technology. So, let’s create something a little more elaborate. If you remember that the pressure readings are an indicator of potential changing weather patterns, we can take this a step further to predict what we can expect in our immediate area, based on the pressure reading, past and present.

PREDICTING UNDER PRESSURE

Every television news program contains a look at the present and future weather. Often they include a weather chart that shows areas of high- and low-pressure areas (Figure 5). High-pressure areas are usually experiencing fair weather, whereas low-pressure areas can be cloudy and stormy. The isobars around such areas indicate areas of equal pressure. Winds generally blow outward from high-pressure areas. As air leaves the high-pressure area, clouds and precipitation become scarce. As winds blow inward toward low-pressure areas, the air rises, producing clouds and condensation.

So, at the extremes, high-pressure means dry and sunny, whereas low-pressure means stormy. You may have noticed that mechanical barometers often have labels on them indicating a description of the expected weather for particular pressure readings, as shown in Figure 6.

FIGURE 5
Weather maps are similar to topological maps. Instead of showing areas of various elevations, the weather map shows areas of similar air pressure. "H" indicates those areas of the highest pressure, and "L" indicates areas of the lowest pressure. Wind direction is generally from H to L [6].
FIGURE 5
Weather maps are similar to topological maps. Instead of showing areas of various elevations, the weather map shows areas of similar air pressure. “H” indicates those areas of the highest pressure, and “L” indicates areas of the lowest pressure. Wind direction is generally from H to L [6].
FIGURE 6
A typical mechanical barometer that has a scale marked off in millibars and inches of mercury. Also, the gauge's air pressure span is grouped into areas that are labeled with descriptive text according to the weather you might find for those air pressures [7].
FIGURE 6
A typical mechanical barometer that has a scale marked off in millibars and inches of mercury. Also, the gauge’s air pressure span is grouped into areas that are labeled with descriptive text according to the weather you might find for those air pressures [7].

We can make a few tables (arrays) of information that will become useful to our project. The first is an array of descriptions, myModes[]. These will be used to indicate in which zone or area the present pressure reading is. The second array contains the center pressure (in millibars) associated with each description, myModePoints[]. What we are really interested in is the points where we are changing modes. These will be different, depending on whether the pressure is trending up (rising) or down (falling). A bit of hysteresis, myHysteresis, will prevent multiple inadvertent changes as we go from one mode to another mode.

Listing 1
Complete application to read and display the pressure as serial output.

#include <Wire.h>
String SignOn = “ftb396 HP206 Read Barometer”;
byte myCommand;
byte myCount;
long myTotal;
const byte myAddress = 0x76;
const byte resetHP206 = 0x06;
const byte statusHP206 = 0x8D;
const byte readyHP206 = 0x40;	// bit 6 of regster 0x0D
const byte pressureHP206 = 0x30;
const byte convertHP206 = 0x40;
//
void setup() 
{
  delay(1000);
  Serial.begin(115200);		// start serial for output
  Serial.println();
  Serial.println(SignOn);
  Wire.begin();
  delay(1000);
  myCommand = resetHP206;	// soft reset (optional)
  writeI2C();
  delay(100);
  myCommand = statusHP206;	// read register 0x0D
  writeI2C();
  myCount = 1;
  readI2C();
  if(myTotal & readyHP206)		// test for bit 6
  {
    Serial.println(“HP206 is ready”);
  }
  else
  {
    Serial.println(“HP206 is NOT ready”);
    Serial.println(“Program Halted”);
    while(1);
  }
}

void loop() 
{
  myCommand = convertHP206;	//start a conversion for pressure
  writeI2C();
  myCommand = pressureHP206;	//read pressure value
  writeI2C();
  myCount = 3;			// 3 bytes
  readI2C();
  Serial.println(“Pressure:” + String(myTotal/100) + “mbars”);
  delay(1000);
}
void writeI2C()
{
  Wire.beginTransmission(myAddress);	//  start write
  Wire.write(myCommand);		//  sends one byte
  Wire.endTransmission();		//  stop transmitting
}
void readI2C()
{
  myTotal = 0;  
  Wire.requestFrom(myAddress, myCount);	//  start read     
  while (Wire.available()) //slave may send less than requested
  {
    int c = Wire.read();		// receive a byte as character
    myTotal=(myTotal*256)+ c;	// total as a single value
  }
}
Listing 2

This project will make weather predictions based on changes in air pressure. We divide air pressure readings into areas of concern. Each area contains both a description, myModes[], and a pressure reading, myModePoints[]. Using myModePoints[], myRisingTrip[] and my FallingTrip[], arrays are created to establish trip points, adjusted by the hysteresis amount."

const String myModes[5] = {“Stormy “,”Rainy  “,”Change “,”Fair   “,”Dry    “};
const int myModePoints[5] = {930,975,1000,1025,1050};
const byte myHysteresis = 2;            // +/- change
const int myRisingTrip[4] = 
{
  (myModePoints[0] + myModePoints[1]) / 2 + myHysteresis,
  (myModePoints[1] + myModePoints[2]) / 2 + myHysteresis,
  (myModePoints[2] + myModePoints[3]) / 2 + myHysteresis,
  (myModePoints[3] + myModePoints[4]) / 2 + myHysteresis
};
int myFallingTrip[4] = 
{
  (myModePoints[0] + myModePoints[1]) / 2 - myHysteresis,
  (myModePoints[1] + myModePoints[2]) / 2 - myHysteresis,
  (myModePoints[2] + myModePoints[3]) / 2 - myHysteresis,
  (myModePoints[3] + myModePoints[4]) / 2 – myHysteresis
};
 Listing 3

Once we know if the pressure is rising or falling, we can determine if the pressure has passed an hysteresis trip point.

 if(Pressure > lastPressure)           // Rising
  {
    myDirection = true;
    Serial.println(“Rising”);
    checkForChange();    
  }
  if(Pressure < lastPressure)           // Falling
  {
    myDirection = false;
    Serial.println(“Falling”);
    checkForChange();  
  } 
  lastPressure = Pressure;
Listing 4
This routine first divides the flow into two branches, one for rising pressures and one for falling pressures. In this partial listing you can see that the rising pressure is checked to see if it falls into the "Rainy" air pressure, between myRisingTrip[0] and myRisingTrip[1]. If it does, then we can assign myMode = 1 and flag it for updating myUpdate = true.

void checkForChange()
{
  if(myDirection)                       // rising
  {
    if((Pressure >= myRisingTrip[0]) & (Pressure < myRisingTrip[1]))
    {
      if(myMode != 1)
      {
        myMode = 1;
        myUpdate = true;
      }
    }
    else if((Pressure >= myRisingTrip[1]) & (Pressure < myRisingTrip[2]))
    {
…

So there are two additional arrays, one for rising pressures and one for falling pressures. These arrays, myRisingTrip[] and myFallingTrip[], will be used to indicate when the displayed mode actually changes (Listing 2).

To make use of these arrays, we need the present air pressure, which we’ve already captured, and the trend to any air pressure changes. From Listing 1, we assign myTotal/100 to the new variable Pressure and add a new variable lastPressure, which will determine the trend stored in Boolean variable myDirection (Listing 3). This is used to determine which array is used in determining a change in mode via the checkForChange() routine (Listing 3).

The checkForChange() routine is where the final determination is made on whether we have crossed into a new mode (Listing 4). We are using different arrays depending on the trend, so we can introduce a little hysteresis into the decision. First a check of myDirection determines which array is being used. Next, the flow falls through a number of checks, one for each mode’s air pressure range. The upper and lower limits of each range are checked using consecutive values in myRisingTrip[] or myFallingTrip[].

At the end of the checkForChange() routine, if myUpdate = true, then we need to update the present mode. After thinking about how the serial output is fine for demonstrating the HP206C sensor, but not friendly enough, I decided to add a display to the project. But what to show?

WEATHER FORECASTER

The smallest, brightest display I’ve ever used is the SSD1306 (Solomon Systech). It has about a 1” screen size and can display 8 lines of 21 characters each. The text is tiny but very clear. If I could keep the text short enough, I could use the 2x text size and have four lines of 10 characters each. This is quite easily readable without getting up close.

The words chosen in the myModes[] array fit into this form factor, with room for a few extra characters. The idea was to display the modes as they changed, using a FIFO stack. In other words, I have four lines, and any new change would be displayed on the top line, pushing any previous text down 1 line. At any point, you would have a record of the last four changes in air pressure modes. It seemed silly to indicate the trend on the display since it is evident from the last modes which way the pressure is trending. What would be appropriate?

In almost all the discussions of air pressure changes, one thing sticks out—how rapidly the changes are taking place. A fast-moving front (H—>L or L—>H) has more meaning than slowly rising or falling pressure. According to the National Oceanic and Atmospheric Administration, when the pressure changes at a rate of more than 2mbar per hour, the change is considered rapid. If you look at the air pressure differences between modes, most have a span of about 25mbar. So, if we get a change in mode within 12 hours, that is considered a rapidly changing weather pattern. When patterns change rapidly, the change becomes more extreme. Wind speeds increase as the rate of pressure changes.

Since rate of change is so important, what might make sense is a “duration of time since the last change in modes.” We don’t need to know the exact time of day that a change took place, but rather, how long it’s been since the last mode change. To keep track of this, we can use the built-in millisecond counter.

Let’s keep it simple and throttle down the time through the loop. If our loop becomes 1,000ms, then we can increment a second counter (s) once each loop. If the variable s reaches 60, then we can increment a minute counter (m) and reset s=0. Likewise, we would do the same for h (hours) and d (days). This is a very simple way to keep an accurate long-duration time between two events. Whenever we update a mode change on the display, we tack on the value of s, m, h, or d with the highest time frame. After some amount of time, we may get a display similar to this:

Stormy 6h
Rainy 12h
Change 20h
Fair 2d

CONCLUSION

The HP206C barometer is a high-precision instrument. Absolute precision is held to ±3mbar over the entire temperature range of 0-50°C, and relative precision is half that. This means that once you correct with an offset for your location, this sensor can be used to report altitude to within 10cm. Although you can find less expensive pressure sensors out there, this one has great specs and is easy to use. Many of today’s sensors contain a temperature sensor. This gives a device the ability to internally compensate for changes in temperature, increasing the accuracy of the main sensor.

Although you’ll still find mechanical barometers out there, the tiny size of the MEMS sensors really makes for some small packaging possibilities. If you have a home weather station, this is a great addition to the normal wind speed/direction, rain bucket, and hydrometer stack. The local meteorologist can give you a general overview of what is to come, but there is often a large discrepancy in what is occurring between the hills and the river valleys of a state, unless you are in a state that’s flat.

I have this little project on the nightstand in my bedroom, where I can review what has happened over the last day or so and determine if its worth getting out of bed. All kidding aside, there are parts of this country that experience some dramatic weather patterns. On the one hand, if you don’t get your weather forecast from the media, it is nice to have an alternative to wetting your finger and sticking it up in the air. On the other hand, you could just listen to your wife’s, or your own joints. There’s a storm brewing! Too much to do, so little time. 

REFERENCES
[1]-Farmers’ Almanac, “Can Aches and Pains Predict the Rain?” https://www.farmersalmanac.com/can-aches-and-pains-predict-the-rain-10960[2] -How a mercury barometer works:
https://www.explainthatstuff.com/barometers.html[3]– US Patent Office, Patent No. 2,367,034, Aneroid Barometer:
https://patents.google.com/patent/US2367034A/en[4] Shenzhen Hope Microelectronics Co., Ltd. HP206C, Waterproof MEMS Altimetric Pressure Module: https://www.hoperf.com/sensor/pressure_sensor/HP206C.html[5] HP206C Data Sheet:
https://www.hoperf.com/data/upload/portal/20190307/HP206C_DataSheet_EN_V2.0.pdf[6] -Example of weather map showing high- and low-pressure areas:
https://www.americangeosciences.org/education/k5geosource/content/weather/why-is-the-weather-different-in-high-and-low-pressure-areas[7] – A barometer that includes types of weather to expect at various pressures: www.weatherwizkids.com/?page_id=82

Code and Supporting Files

PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JULY 2023 #396 – Get a PDF of the issue

Keep up-to-date with our FREE Weekly Newsletter!

Don't miss out on upcoming issues of Circuit Cellar.


Note: We’ve made the Dec 2022 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Would you like to write for Circuit Cellar? We are always accepting articles/posts from the technical community. Get in touch with us and let's discuss your ideas.

Sponsor this Article
Website | + posts

Jeff Bachiochi (pronounced BAH-key-AH-key) has been writing for Circuit Cellar since 1988. His background includes product design and manufacturing. You can reach him at: jeff.bachiochi@imaginethatnow.com or at: www.imaginethatnow.com.

Supporting Companies

Upcoming Events


Copyright © KCK Media Corp.
All Rights Reserved

Copyright © 2024 KCK Media Corp.

Barometer

by Jeff Bachiochi time to read: 13 min