This guide shows you how to use the BMP180 barometric sensor with the ESP32 to read pressure, temperature and estimate altitude. We’ll show you how to wire the sensor to the ESP32, install the needed library, and how to write the sketch in the Arduino IDE.
Introducing the BMP180 Barometric Sensor
The BMP180 is a digital pressure sensor and it measures the absolute pressure of the air around it.
It features a measuring range from 300 to 1100hPa with an accuracy down to 0.02 hPa.
Because temperature affects the pressure, the sensor comes with a temperature sensor to give temperature compensated pressure readings.
Additionally, because the pressure changes with altitude, you can also estimate the altitude based on the current pressure measurement.
Wiring BMP180 Sensor to the ESP32
The BMP180 barometric sensor uses I2C communication protocol. So, you need to use the SDA and SCL pins of the ESP32.
The following table shows how to wire the sensor.
BMP180 | Wiring to ESP32 |
Vin | 3.3V |
GND | GND |
SCL | GPIO 22 (SCL) |
SDA | GPIO 21 (SDA) |
Reading Temperature, Pressure, And Altitude
In this section we’ll show you how to read pressure and temperature from the BMP180 barometric sensor using the ESP32. We’ll also show you how to estimate altitude.
Parts required
For this example, you need the following parts:
- ESP32 Module (ESP32 DOIT DEVKIT V1 Board) – read ESP32 development boards comparison
- BMP180 barometric sensor
- Jumper wires
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
Wire the BMP180 barometric sensor to the ESP32 as shown in the following schematic diagram.
Preparing the ESP32 board in Arduino IDE
In order to upload code to your ESP32 using Arduino IDE, you should install an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow one of the next tutorials to prepare your Arduino IDE:
- Windows instructions – Installing the ESP32 Board in Arduino IDE
- Mac and Linux instructions – Installing the ESP32 Board in Arduino IDE
Installing the BMP_085 Library
One of the easiest ways to read pressure, temperature and altitude with the BMP180 sensor is using the BMP_085 library by Adafruit. This library is compatible with the BMP085 and the BMP180 sensors. 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 “BMP085” on the Search box and install the BMP085 library from Adafruit.
After installing, restart your Arduino IDE.
Code
The library provides an example showing how to get temperature, pressure, and altitude. Go to File > Examples > Adafruit BMP085 Library > BMP085test.
/*
* Rui Santos
* Complete Project Details https://randomnerdtutorials.com
*/
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
}
void loop() {
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
Serial.print("Pressure at sealevel (calculated) = ");
Serial.print(bmp.readSealevelPressure());
Serial.println(" Pa");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(102000));
Serial.println(" meters");
Serial.println();
delay(500);
}
The code starts by importing the needed libraries:
#include <Wire.h>
#include <Adafruit_BMP085.h>
You create an Adafruit_BMP085 object called bmp.
Adafruit_BMP085 bmp;
In the setup() the sensor is initialized:
void setup() {
Serial.begin(9600);
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
}
Reading Temperature
To read the temperature you just need to use the readTemperature() method on the bmp object:
bmp.readTemperature()
Reading Pressure
Reading the pressure is also straighforward. You use the readPressure() method.
bmp.readPressure()
The pressure readings are given in Pascal units.
Reading Altitude
Because the pressure changes with altitude, you can estimate your current altitude by comparing it with the pressure at the sea level.
The example gives you two different ways to estimate altitude.
1. The first assumes a standard barometric pressure of 10132 Pascal at the sea level. You get the altitude as follows:
bmp.readAltitude()
2. The second method assumes the current pressure at the sea level. For example, if at the moment the pressure at the sea level is 101500 Pa, you just need to pass 101500 as an argument to the readAltitude() method as follows:
bmp.readAltitude(101500)
Demonstration
Upload the code to your ESP32. Make sure you have the right board and COM port selected.
Then, open the Serial Monitor at a baud rate of 9600. You should get the sensor readings, as shown in the following figure.
Wrapping Up
In this guide we’ve shown you how to use the BMP180 barometric sensor with the ESP32 to read pressure, temperature and estimate altitude.
Now, you can take this project further and display the latest sensor readings on a web server. We have several examples you can modify to display the readings:
- ESP32 Web Server with BME280 – Mini Weather Station
- ESP32 DHT11/DHT22 Web Server – Temperature and Humidity using Arduino IDE
We hope you’ve found this guide useful. If you like ESP32, make sure you take a look at the following resourceS:
- Learn ESP32 with Arduino IDE (course)
- MicroPython Programming with the ESP32 (eBook)
- Getting Started with ESP32
- ESP32 Pinout Reference: Which GPIO pins should you use?
- ESP32 Web Server – Arduino IDE
Thanks for reading.
hello there.. may i know what software you are using to draw that esp32 schematic ?
Hi.
We use fritzing 🙂
Hi, for the DOIT ESP32 V1 board it was necessary put Wire.begin (21, 22); after the line serial.begin (9600);. The are many problens with this GPIOs of this board, then it is necessary .
Hi Rodrigo.
For us, it worked just fine the way it is.
But if someone has problems with the pins, that is a good tip.
Thanks for letting us know.
Thank you for the tutorial.
I used an ESP32 based M5Stickc.
I put Wire.begin(0, 26, 10000); just before bmp.begin().
It works very good!
Thanks for the suggestion!
Hello Rui would this example work with esp8266?
Hi everybody! THanks a lot for the comments and for sharing your knoledgment!. It’s great!!
I’m using a Esp32 Lora Wifi V2 and, the only way to run the BMP180 was using the old version (version 1.0.0) of the Adafruit_BMP085.h. I used exactly the code BMP085test above. THe last version of this library (1.2.1) run very nice in Arduino IDE but only when using, as hardware, an Arduino (I tested using Arduino UNO). But, when using the Esp32 Lora Wifi V2, the Adafruit_BMP085.h must be the version 1.0.0. I hope to help !
Alternatively, to know the hPa for your static location, you must do:
// 114 m is my location altitude
bmp.readSealevelPressure(114);
This return Pa of my location and divided by 100 give you hPa
Hello
Can 2 sensors be connected to esp32?
Thank you.
Yes.
Check this tutorial to learn how to use multiple I2C connections: https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
Regards,
Sara
Hello Sarah
Thank you for the excellent information you have provided. I hope I am not wrong. I made the changes you mentioned.
Thank you very much.
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-i2c-communication-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#define I2C_SDA 21 // first bmp180
#define I2C_SCL 22 // first bmp180
#define I2C_SDA_2 33 // 33 second bmp180
#define I2C_SCL_2 32 // 32 second bmp180
#define SEALEVELPRESSURE_HPA (1013.25)
TwoWire I2CBME = TwoWire(0); // first bmp180
Adafruit_BMP085 bme; // first bmp180
TwoWire I2CBME_2 = TwoWire(1); // TwoWire(1); // second bmp180
Adafruit_BMP085 bme_2; // second bmp180
unsigned long delayTime;
void setup() {
Serial.begin(115200);
Serial.println(F(“BMP085 test”));
I2CBME.begin(I2C_SDA, I2C_SCL, 100000);
Serial.println(F(“BMP085_2 test”));
I2CBME_2.begin(I2C_SDA_2, I2C_SCL_2, 200000); // , 200000);
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin(0x77, &I2CBME);
if (!status) {
Serial.println(“Could not find a valid BMP085 sensor, check wiring!”);
while (1)
;
}
status = bme_2.begin(0x77, &I2CBME_2);
if (!status) {
Serial.println(“Could not find a valid BMP085_2 sensor, check wiring!”);
while (1)
;
}
Serial.println(“– Default Test –“);
delayTime = 1000;
Serial.println();
}
void loop() {
printValues();
delay(delayTime);
}
void printValues() {
Serial.print(“Temperature = “);
Serial.print(bme.readTemperature());
Serial.println(” *C”);
Serial.print(“Pressure = “);
Serial.print(bme.readPressure() / 100.0F);
Serial.println(” hPa”);
Serial.print(“Approx. Altitude = “);
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(” m”);
Serial.print(“Temperature_2 = “);
Serial.print(bme_2.readTemperature());
Serial.println(” *C”);
Serial.print(“Pressure_2 = “);
Serial.print(bme_2.readPressure() / 100.0F);
Serial.println(” hPa”);
Serial.print(“Approx. Altitude_2 = “);
Serial.print(bme_2.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(” m”);
Serial.println();
}
Try it out and see if it works.
I tried and it worked. You are great.
Thank you very much
Don’t forget to set the serial monitor to 9600 bauds, otherwise you´ll see nothing but squares.