Guide for BMP388 Altimeter with Arduino (Pressure, Altitude, Temperature)

The BMP388 is a tiny and precise absolute barometric pressure sensor. Because of its precision, it is often used to estimate altitude in drone applications. It can also be used in indoor/outdoor navigation, GPS applications, and others. In this tutorial, you’ll learn how to use the BMP388 altimeter/pressure sensor with the Arduino board—wiring diagram and example sketch.

Guide for BMP388 Altimeter with Arduino Pressure Altitude Temperature

In this tutorial, we cover:

Introducing BMP388 Barometric Sensor

The BMP388 is a precise, low-power, low-noise absolute barometric pressure sensor that measures absolute pressure and temperature. Because pressure changes with altitude, we can also estimate altitude with great accuracy. For this reason, this sensor is handy for drone and navigation applications. You can also use it for other applications:

  • vertical velocity calculation;
  • internet of things;
  • weather forecast and weather stations;
  • health care applications;
  • fitness applications;
  • others…

We’re using the BMP388 sensor as a module, as shown in the figure below. It is also available in other different formats.

Front Pins BMP388 Sensor Module Altitude Pressure Temperature

The following picture shows the other side of the sensor.

Part Back Pins BMP388 Sensor Module Altitude Pressure Temperature

BMP388 Technical Data

The following table shows the key features of the BMP388 sensor. For more information, consult the datasheet.

Operation range300 to 1250 hPa (pressure)
-40 to +85ºC (temperature)
InterfaceI2C and SPI
Average typical current consumption3.4 µA @ 1Hz
Absolute accuracy pressure (typ.)
P=900 …1100 hPa (T=25 … 40°C)
±0.5 hPa
Relative accuracy pressure (typ.)
P=900…1100 hPa (T=25 … 40°C)
±0.08 hPa
Noise in pressure (lowest bandwidth, highest resolution)0.03 Pa
Maximum sampling rate200 Hz

BMP388 Pinout

Here’s the pinout of the BMP388 module we’re using—it might be slightly different for other modules.

VINPowers the sensor (5V)
3V3Powers the sensor (3V3)
GNDCommon GND
SCKSCL pin for I2C communication
SCK pin for SPI communication
SDOSDO (MISO) pin for SPI communication
SDISDI (MOSI) pin for SPI communication
SDA pin for I2C communication
CSChip select pin for SPI communication
INTInterrupt pin

BMP388 Interface

As mentioned previously, the BMP388 sensor supports I2C and SPI interfaces.

Arduino UNO Board BMP388 Sensor Module Altitude Pressure Temperature Parts Required

BMP388 I2C

To use I2C communication protocol, use the following pins (for the Arduino Uno—if you’re using another model, check its I2C pins):

BMP388Arduino
SDI (SDA)A4
SCK (SCL)A5

BMP388 SPI

To use SPI communication protocol, use the following pins (for the Arduino Uno—if you’re using another model, check its SPI pins):

BMP388Arduino
SCKPin 13
SDI (MOSI)Pin 11
SDO (MISO)Pin 12
CS (Chip Select)Pin 10

Parts Required

Arduino UNO Board BMP388 Sensor Module Altitude Pressure Temperature Parts Demonstration

To complete this tutorial you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic – Arduino with BMP388

The BMP388 can communicate using I2C or SPI communication protocols.

Arduino with BMP388 using I2C

Follow the next schematic diagram to wire the BMP388 to the Arduino Uno using the I2C pins.

Arduino with BMP388 Altimeter Pressure Sensor Wiring Circuit Diagram I2C

Arduino with BMP388 using SPI

Alternatively, if you want to use the SPI communication protocol, follow the next schematic diagram to wire the BMP388 to the Arduino Uno using the SPI pins.

Arduino with BMP388 Altimeter Pressure Sensor Wiring Circuit Diagram SPI

Installing the Adafruit BMP3XX Library

There are different libraries compatible with the BMP388 sensor and the Arduino. In this tutorial, we’ll use the Adafruit BMP3XX library.

Follow the next steps to install the library in your Arduino IDE:

Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open.

Search for “adafruit bmp3xx ” in the search box and install the library.

Install Adafruit bmp3xx library Arduino IDE

Installing the Adafruit_Sensor Library

To use the BMP3XX library, you also need to install the Adafruit_Sensor library. Follow the next steps to install the library in your Arduino IDE:

Go to Sketch Include Library > Manage Libraries and type “Adafruit Unified Sensor” in the search box. Scroll all the way down to find the library and install it.

Installing Adafruit Unified Sensor Driver library

After installing the libraries, restart your Arduino IDE.

Code – Reading BMP388 Pressure, Altitude and Temperature

The best way to get familiar with a new sensor is to start with a basic example provided by the library.

After installing the BMP3XX library, open the Arduino IDE and go to File > Examples > Adafruit BMP3XX Library > bmp3XX_simpletest. The following code should load.

/***************************************************************************
  This is a library for the BMP3XX temperature & pressure sensor. Designed specifically to work with the Adafruit BMP388 Breakout ----> http://www.adafruit.com/products/3966
  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 and open-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
 ***************************************************************************/

// Complete project details: https://RandomNerdTutorials.com/arduino-bmp388/


#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"

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

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Adafruit BMP388 / BMP390 test");

  if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire
  //if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode  
  //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode
    Serial.println("Could not find a valid BMP3 sensor, check wiring!");
    while (1);
  }

  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);
}

void loop() {
  if (! bmp.performReading()) {
    Serial.println("Failed to perform reading :(");
    return;
  }
  Serial.print("Temperature = ");
  Serial.print(bmp.temperature);
  Serial.println(" *C");

  Serial.print("Pressure = ");
  Serial.print(bmp.pressure / 100.0);
  Serial.println(" hPa");

  Serial.print("Approx. Altitude = ");
  Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");

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

View raw code

Sea Level Pressure

To get more accurate results for pressure and altitude, we recommend that you adjust the sea level pressure for your location in the SEALEVELPRESSURE_HPA variable:

#define SEALEVELPRESSURE_HPA (1013.25)

The standard value is 1013.25 hPa. For more accurate results, check the sea level pressure at your location. You can use this website to check sea level pressure.

How the Code Works

Continue reading this section to learn how the code works, or skip to the Demonstration section.

Libraries

The code starts by including the needed libraries: the Wire library to use I2C, the SPI library (if you want to use SPI instead of I2C), the Adafruit_Sensor, and Adafruit_BMP3XX libraries to interface with the BMP388 sensor.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"

SPI communication

We prefer to use the I2C communication protocol with the sensor. However, the code is prepared if you want to use SPI. The following lines of code define the SPI pins.

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

Sea level pressure

A variable called SEALEVELPRESSURE_HPA is created.

#define SEALEVELPRESSURE_HPA (1013.25)

This variable saves the pressure at the sea level in hectopascal (is equivalent to milibar). This variable is used to estimate the altitude for a given pressure by comparing it with the sea level pressure. This example uses the default value, but for accurate results, replace the value with the current sea level pressure at your location. You can use this website to check sea level pressure.

setup()

In the setup() start a serial communication.

Serial.begin(115200);

Init BMP388 Sensor I2C

This example uses I2C communication protocol by default. The following line starts an Adafruit_BMP3XX object called bmp on the I2C pins: A5 (SCL), A4 (SDA).

if (!bmp.begin_I2C()) {   // hardware I2C mode, can pass in address & alt Wire

To use SPI, you need to comment this previous line and uncomment one of the following lines for hardware SPI (use the default SPI pins and choose the CS pin) or software SPI (use any pins).

//if (! bmp.begin_SPI(BMP_CS)) {  // hardware SPI mode  
//if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) {  // software SPI mode

Set up the following parameters (oversampling and filter) for the sensor.

// Set up oversampling and filter initialization
bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
bmp.setOutputDataRate(BMP3_ODR_50_HZ);

To increase the resolution of the raw sensor data, it supports oversampling. We’ll use the default oversampling parameters, but you can change them.

  • setTemperatureOversampling(): set temperature oversampling.
  • setPressureOversampling(): set pressure oversampling.

These methods can accepts one of the following parameters:

  • BMP3_NO_OVERSAMPLING
  • BMP3_OVERSAMPLING_2X
  • BMP3_OVERSAMPLING_4X
  • BMP3_OVERSAMPLING_8X
  • BMP3_OVERSAMPLING_16X
  • BMP3_OVERSAMPLING_32X

The setIIRFilterCoeff() function sets the coefficient of the filter (in samples). It can be:

  • BMP3_IIR_FILTER_DISABLE (no filtering)
  • BMP3_IIR_FILTER_COEFF_1
  • BMP3_IIR_FILTER_COEFF_3
  • BMP3_IIR_FILTER_COEFF_7
  • BMP3_IIR_FILTER_COEFF_15
  • BMP3_IIR_FILTER_COEFF_31
  • BMP3_IIR_FILTER_COEFF_63
  • BMP3_IIR_FILTER_COEFF_127

Set the output data rate with the setOutputDataRate() function. It can accept one of the following options:

BMP3_ODR_200_HZ, BMP3_ODR_100_HZ, BMP3_ODR_50_HZ, BMP3_ODR_25_HZ,BMP3_ODR_12_5_HZ, BMP3_ODR_6_25_HZ, BMP3_ODR_3_1_HZ, BMP3_ODR_1_5_HZ, BMP3_ODR_0_78_HZ, BMP3_ODR_0_39_HZ,BMP3_ODR_0_2_HZ, BMP3_ODR_0_1_HZ, BMP3_ODR_0_05_HZ, BMP3_ODR_0_02_HZ, BMP3_ODR_0_01_HZ, BMP3_ODR_0_006_HZ, BMP3_ODR_0_003_HZ, or BMP3_ODR_0_001_HZ

loop()

In the loop(), we’ll get measurements from the BMP388 sensor.

First, tell the sensor to get new readings with bmp.performReading().

if (! bmp.performReading()) {
  Serial.println("Failed to perform reading :(");
  return;
}

Then, get and print the temperature, pressure and altitude readings as follows:

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

Serial.print("Pressure = ");
Serial.print(bmp.pressure / 100.0);
Serial.println(" hPa");

Serial.print("Approx. Altitude = ");
Serial.print(bmp.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");

You get each specific reading as follows:

  • bmp.temperature: returns temperature reading
  • bmp.pressure: returns pressure reading
  • bmp.readAltitude (SEALEVELPRESSURE_HPA): returns altitude estimation

Demonstration

After inserting the sea level pressure for your location, you can upload the code to your board. In your Arduino IDE, go to Tools > Boards and select the board you’re using. Then, in Tools > Port, select the COM port.

After uploading, open the Serial Monitor at a baud rate of 115200. The readings will be printed in the Serial Monitor.

BMP388 Altitude Pressure Display Serial Monitor

Notice that if you increase the sensor’s altitude, it will be reflected in the altitude reading. The altitude estimation is pretty accurate. It can detect small changes in the centimeters or inches range. You can check it by comparing the altitude you’re getting with the altitude of your location. To get your location’s altitude, you can use this website.


Wrapping Up

The BMP388 is a small and very precise pressure sensor that allows you to estimate altitude with great precision. The sensor also measures temperature. It is great for outdoor/indoor navigation, drones, weather stations, and other applications.

You’ve learned how to use the sensor with the Arduino board in this tutorial. We hope you found this getting started guide useful. In addition, we have guides for other popular sensors:

Learn more about the Arduino board with our resources:

Thanks for reading.



Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.