Video Tutorial (Optional)
Watch first if you want to follow the full ESP32 + BMP280 setup in real time.
Project Overview
ESP32 + BMP280: In this project, you connect a BMP280 sensor to an ESP32 and read pressure, temperature, and altitude values in the Serial Monitor using an Arduino example sketch.
The BMP280 is a beginner-friendly sensor that measures pressure and temperature accurately. Pressure can be converted to altitude with the right calibration, making it useful for things like drones and weather stations.
- Time: A few minutes
- Skill level: Beginner
- What you will build: An ESP32 sensor readout that prints BMP280 pressure, temperature, and altitude to the Serial Monitor
If you want to follow the channel and support the content, here are the original links:
- Subscribe: https://www.youtube.com/@mmshilleh
- Support: https://www.buymeacoffee.com/mmshilleh
Parts List
From ShillehTek
- None linked in the original article.
External
- BMP280 sensor module (pre-soldered breakout) - example link: https://www.amazon.com/dp/B0CD4PQZGQ
- ESP32 development board (use ESP32 Dev Module in Arduino IDE)
- Arduino IDE
- Adafruit BMP280 library (installed via Library Manager in Arduino IDE)
- Jumper wires and a way to connect the sensor to the ESP32 (breadboard or direct wiring)
Note: The example code in this tutorial uses I2C mode and initializes the sensor with the I2C address 0x76.
Step-by-Step Guide
Step 1 - Setup the physical connection
Goal: Wire the BMP280 sensor to the ESP32 so the board can communicate with it.
What to do: Connect the BMP280 breakout to the ESP32 using the appropriate wiring for your module and board. Use the image below as the reference from the original tutorial.
Expected result: The BMP280 is physically connected to the ESP32 and ready for software setup.
Step 2 - Install the ESP32 board package and BMP280 library
Goal: Prepare Arduino IDE so you can compile and upload BMP280 example code to your ESP32.
What to do: In the Arduino IDE Boards Manager, download the esp32 boards by Espressif Systems.
Then download the BMP280 library by Adafruit.
Connect to the ESP32 on the correct serial port and select ESP32 Dev Module under Tools > Board.
Expected result: Arduino IDE can compile for ESP32, and the Adafruit BMP280 library is installed.
Step 3 - Open and run the Adafruit example code
Goal: Upload a known-good sketch and start printing sensor values to the Serial Monitor.
What to do: Open the example sketch at File > Examples > Adafruit BMP280 Library > bmp280test.
Use the code below for reference, and make sure the I2C address is set to 0x76:
/***************************************************************************
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());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1011.9)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
delay(2000);
}
If you do not set the correct I2C address, you may see a device not found error.
For more accurate altitude values, look up the local sea-level pressure for your area. The original example location used this page: https://w1.weather.gov/data/obhistory/KAUS.html.
Upload the code to your ESP32, then open the Serial Monitor with the correct baud rate to start seeing values.
Expected result: The Serial Monitor prints temperature (C), pressure (Pa), and approximate altitude (m) every 2 seconds.
Conclusion
You connected a BMP280 sensor to an ESP32 and used the Adafruit example sketch to read pressure and temperature, plus approximate altitude using a sea-level pressure value. Once you see values in the Serial Monitor, you can adjust the example code to fit your own project.
Want the exact parts used in this build? Grab parts and modules from ShillehTek.com. If you want help customizing this project or building something similar for your product, check out our IoT consulting services.