Lesson 7 Barometer

Share for us

Introduction

A barometer is a scientific instrument used to measure the air pressure. In this experiment, the SunFounder Uno board is used as the master board to read the value from BMP180 and display it on Serial Monitor.

Components

– 1 * SunFounder Uno board

– 1 * BMP180 module

– 1 * USB cable

– 1 * 4-Pin anti-reverse cable

Experimental Principle

Use a barometer to measure air pressure and temperature.

The schematic diagram of the module:

Experimental Procedures

Step 1: Build the circuit

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Note: Here you need to add a library. Refer to the description in Lesson 1 previously in the manual.

Step 3: Compile

Step 4: Upload the sketch to the SunFounder Uno board

Now you can see the value of temperature and air pressure displayed on Serial Monitor.

Values displayed on Serial Monitor

Code

/*****************************************************
* name:Barometer
* function:you can see the value of temperature and air pressure displayed on Serial Monitor.
*****************************************************/
//Email:support@sunfounder.com
//Website:www.sunfounder.com#include <SFE_BMP180.h>
#include <Wire.h>// You will need to create an SFE_BMP180 object, here called “pressure”:
SFE_BMP180 pressure;void setup()
{
  Serial.begin(9600); //initialize the serial monitor
  Serial.println(“REBOOT”);//ptint “REBOOT”  // Initialize the sensor (it is important to get calibration values stored on the device).
  if (pressure.begin())
  Serial.println(“BMP180 init success”);
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.    Serial.println(“BMP180 init fail\n\n”);
    while(1); // Pause forever.
  }
}void loop()
{
  char status;
  double T,P,p0,a;  // Loop here getting pressure readings every 10 seconds.  // If you want sea-level-compensated pressure, as used in weather reports,
  // you will need to know the altitude at which your measurements are taken.
  // We’re using a constant called ALTITUDE in this sketch:  // If you want to measure altitude, and not pressure, you will instead need
  // to provide a known baseline pressure. This is shown at the end of the sketch.
  // You must first get a temperature measurement to perform a pressure reading.  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Function returns 1 if successful, 0 if failure.
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      Serial.print(“temperature: “);
      Serial.print(T,2);
      Serial.println(” deg C “);
      float tempF = 1.8*T + 32.0; // convert °C to °F
      Serial.print(” “);
      Serial.print(tempF,2);
      Serial.println(” deg F “);
      Serial.println(” “); // skip a line      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          // Print out the measurement:
          Serial.print(“pressure: “);
          Serial.print(P,2);
          Serial.println(” mb “);
          Serial.print(” “);
          Serial.print(P*0.0295301,2); //To convert millibars to inches of mercury
          Serial.print(” inches “);
          Serial.println(” “); //
          Serial.println(” “); // skipping 3 lines for easier reading
          Serial.println(” “); //
           //The pressure sensor returns abolute pressure,which varies with altitude.
          // To remove the effects of altitude, use the sealevel function and your current altitude.
          // This number is commonly used in weather reports.
          // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
          // Result: p0 = sea-level compensated pressure in mb
        }
        else Serial.println(“error retrieving pressure measurement\n”);
      }
      else Serial.println(“error starting pressure measurement\n”);
    }
    else Serial.println(“error retrieving temperature measurement\n”);
  }
  else Serial.println(“error starting temperature measurement\n”);  delay(1000); // Pause for 5 seconds.
}