Pantech.AI

How to Interface BPM280 Barometer Sensor with Arduino

Description

Description

BMP280 Barometer Sensor

The BMP280, developed by Bosch Sensortec, is a widely used digital sensor for measuring barometric pressure and temperature with precision.

This sensor is ideal for applications that require accurate atmospheric pressure and temperature data, including altitude tracking and health monitoring.

Using advanced MEMS (Micro-Electro-Mechanical Systems) technology, the BMP280 delivers reliable pressure and temperature readings.

The sensor incorporates a built-in temperature sensor to compensate for any temperature variations, ensuring both high accuracy and stability across a broad temperature range.

For interfacing with a microcontroller, the BMP280 supports both I2C and SPI communication protocols.

BMP280 Module Specification



Pressure Range: Measures atmospheric pressure from 300 hPa to 1100 hPa.
Altitude Range: Capable of calculating altitude within a range of -500 meters to 9000 meters.
Pressure Accuracy: High accuracy of ±1 hPa (0.01 bar) at a standard temperature of 25°C.
Temperature Range: Operates within a temperature range of -40°C to +85°C.
Temperature Accuracy: Provides an accuracy of ±1°C at 25°C.
Resolution: Fine resolution of 0.01 hPa for pressure and 0.01°C for temperature.
Interface: Supports I2C and SPI communication, with speeds up to 3.4 MHz.
Supply Voltage: Operates with a supply voltage between 1.8 V and 5.5 V.

BMP280 Pinouts

  • VCC: Connects to the positive power supply, typically 3.3V.
  • GND: Ground pin for establishing a common ground.
  • SCL: Serial Clock Line pin used for I2C communication.
  • SDA: Serial Data pin for I2C communication.
  • CSB: Chip Select pin used when communicating via SPI.
  • SDO: Serial Data Output pin for SPI communication.

BMP280 Hardware Connection with Arduino

Ardunio interfacing diagram with BMP280 Barometer Sensor

Extracting Data from BMP280 using Arduino

Reading values from the BMP280 sensor and displaying them on the Serial Monitor in the Arduino IDE.

To accomplish this, we’ll use Adafruit’s BMP280 library, Adafruit_BMP280.h.

This library can be easily downloaded from the Arduino IDE’s Library Manager.

Next, open an example from the Adafruit BMP280 library to get started. To access it, go to File -> Examples -> Adafruit BMP280 Library -> bmp280test in the Arduino IDE.

We’ll use the provided example code below. Before uploading it, edit the code in the setup function to specify the module’s address when initializing the bmp280.

status = bmp.begin(0x76);

Code for reading values and display it on the Serial Monitor

/***************************************************************************
  This is a library for the BMP280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BMP280 Breakout
  ----> http://www.adafruit.com/products/2651

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ***************************************************************************/

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Serial.begin(9600);
  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
//  status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  status = bmp.begin(0x76);
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }

  /* Default settings from datasheet. */
 bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                 Adafruit_BMP280::SAMPLING_X2,    /* Temp. oversampling */
                 Adafruit_BMP280::SAMPLING_X16,   /* Pressure oversampling */
                 Adafruit_BMP280::FILTER_X16,     /* Filtering. */
                 Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure()/100.0F);
    Serial.println(" Pa");

    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
    Serial.println(" m");

    Serial.println();
    delay(2000);
}

Now, upload the code to your Arduino.

Once the upload is complete, open the Serial Monitor in the Arduino IDE and set the baud rate to 9600 to view the output data from the BMP280 sensor.

Output on the serial monitor

Let’s understand the code

First, initialize the necessary libraries. The Wire.h library is required for I2C communication with the microcontroller.

The Adafruit_BMP280.h library provides all the functions and classes needed to retrieve data from the BMP280 sensor module.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

Now we created an object for Adafruit_BMP280 class as bmp

Adafruit_BMP280 bmp; // I2C

Setup Function

In the setup function, we’ve set the baud rate to 9600 for serial communication.

Next, we initialized the BMP280 sensor object with the I2C address 0x76. If the sensor is not detected, the code enters an indefinite while loop, pausing with a 10-millisecond delay.

The if statement checks whether the BMP280 sensor was successfully initialized. If the initialization fails, an error message is printed to the Serial Monitor.

while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
//  status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  status = bmp.begin(0x76);
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or 
                      try a different address!"));

Loop Function

In the loop function, the bmp.readTemperature() function is used to read the temperature data from the BMP280 sensor. The temperature is then displayed on the Serial Monitor using the Serial.println() function.

Similarly, the readPressure() and readAltitude() functions are used to retrieve the pressure and altitude data, which are also displayed on the Serial Monitor.

    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
      
    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25));

Leave a Comment

Your email address will not be published. Required fields are marked *