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.
In this tutorial, we cover:
- BMP388 Pinout
- Wiring BMP388 with Arduino
- Getting BMP388 Pressure, Altitude and Temperature with Arduino
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.
The following picture shows the other side of the sensor.
BMP388 Technical Data
The following table shows the key features of the BMP388 sensor. For more information, consult the datasheet.
Operation range | 300 to 1250 hPa (pressure) -40 to +85ºC (temperature) |
Interface | I2C and SPI |
Average typical current consumption | 3.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 rate | 200 Hz |
BMP388 Pinout
Here’s the pinout of the BMP388 module we’re using—it might be slightly different for other modules.
VIN | Powers the sensor (5V) |
3V3 | Powers the sensor (3V3) |
GND | Common GND |
SCK | SCL pin for I2C communication SCK pin for SPI communication |
SDO | SDO (MISO) pin for SPI communication |
SDI | SDI (MOSI) pin for SPI communication SDA pin for I2C communication |
CS | Chip select pin for SPI communication |
INT | Interrupt pin |
BMP388 Interface
As mentioned previously, the BMP388 sensor supports I2C and SPI interfaces.
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):
BMP388 | Arduino |
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):
BMP388 | Arduino |
SCK | Pin 13 |
SDI (MOSI) | Pin 11 |
SDO (MISO) | Pin 12 |
CS (Chip Select) | Pin 10 |
Parts Required
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 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.
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.
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.
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);
}
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.
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:
- Guide for DHT11/DHT22 Temperature and Humidity Sensor with Arduino
- Guide for BME280 Sensor with Arduino (Pressure, Temperature, Humidity)
- Guide for BMP180 Barometric Sensor with Arduino
- Guide for DS18B20 Temperature Sensor with Arduino
Learn more about the Arduino board with our resources:
- Arduino Step-by-step Projects
- Arduino Mini-Course for Absolute Beginners
- More Arduino Projects and Tutorials …
Thanks for reading.