0

$USD

$USD
TUTORIALS ESP32DFRobotEnglish

ESP32: Getting pressure measurements from a BMP388 sensor

DFRobot May 25 2020 2100

In this tutorial we will learn how to obtain barometric pressure measurements from a BMP388 sensor, using the ESP32 and the Arduino core. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this tutorial we will learn how to obtain barometric pressure measurements from a BMP388 sensor, using the ESP32 and the Arduino core. You can check the datasheet of the BMP388 here.

We will be using this module from DFRobot, which already has all the additional electronic components necessary to interact with the sensor from a microcontroller such as the ESP32.

The BMP388 is a digital sensor that allows to measure both pressure and temperature [1]. It can be accessed via I2C and SPI [1]. For this tutorial we are going to use the I2C interface.

For pressure measurements, the device has a relative accuracy of ±8 Pa. For temperature measurements, it has an absolute accuracy of ±0.5 ℃ [1].

The sensor can operate in a temperature range of -40 ℃ to 85 ℃ and in a pressure range of 300 to 1250 hPa.

The module we are using can operate in a voltage range of 3.3 V to 5.5 V and has a current consumption of 0.5 mA [2].

We will be using this library, which offers higher level functions to interact with the sensor. That way we don’t need to worry about the lower leve protocol details.

To install the library, simply download it as a .zip file from the GitHub repository, extract it and place it on your Arduino libraries folder.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The electric schematic

The connection diagram between the ESP32 and the module can be seen on figure 1.

Figure 1 – Connection diagram between the ESP32 and the sensor module.

The wiring is very simple: we only need to power the sensor module with 3.3 V and connect the SDA and SCL pins of the ESP32 to the module.

The code

We will start by including the DFRobot_BMP388_I2C.h library, so we have access to the methods needed to read the pressure from the sensor.

#include "DFRobot_BMP388_I2C.h"

Then we will define an object of class DFRobot_BMP388_I2C. We will interact with this object to obtain the readings.

DFRobot_BMP388_I2C bmp388;

Moving on to the Arduino setup, we will start by opening a serial connection, to be able to output the results of our program.

Serial.begin(115200);

Then we will call the begin method on our DFRobot_BMP388_I2C object. This method takes no arguments and takes care of initializing the sensor.

This method returns 0 on success. So, we will wrap this method call on a while loop until we can successfully initialize the sensor.

while(bmp388.begin() != 0){
    Serial.println("Error initializing. Trying again..");
    delay(1000);
}

The full Arduino setup function can be seen below,

void setup(){
 
  Serial.begin(115200);
   
  while(bmp388.begin() != 0){
    Serial.println("Error initializing. Trying again..");
    delay(1000);
  }
}

We will then get the sensor readings on the Arduino main loop. To obtain a pressure reading, we simply need to call the readPressure method. This method takes no arguments and returns as output the measurement, as a float, in Pascals.

The complete Arduino main loop can be seen below. We have added a 1 second delay per each iteration of the loop.

void loop(){
   
  float pressure = bmp388.readPressure();
   
  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" Pa");
   
  delay(1000);
}

The final code can be seen below.

Testing the code

After the wiring from figure 1 is done, compile and upload the code to your device using the Arduino IDE.

Once the procedure finishes, open the serial monitor. You should see the measurements getting printed, like illustrated below in figure 2.

Figure 2 – Output of the program, showing the pressure measurements obtained from the sensor.


Tutorial originally shared by techtutorialsx


References

[1] https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf

[2] https://www.dfrobot.com/product-1792.html