Interfacing BME280 Sensor with ESP32
Introduction
The BME280 sensor is a versatile environmental sensor that can measure temperature, pressure, and humidity. It communicates with the ESP32 microcontroller using the I2C protocol. This guide will walk you through the process of interfacing the BME280 sensor with the ESP32 using the provided code.
Problem Statement
The objective of this project is to interface the BME280 sensor with ESP32. The BME280 sensor communicates with the ESP32 using the I2C protocol. We will read and display temperature, pressure, and humidity values from the sensor.
Requirements
To successfully complete this project, you will need the following components:
- ESP32 development board
- BME280 sensor
- Jumper wires
- Jumper wires
- Breadboard
Circuit Diagram
Connect the BME280 sensor to the ESP32 development board as follows:
- VCC of the BME280 to the 3.3V pin of the ESP32
- GND of the BME280 to the GND pin of the ESP32
- SDA of the BME280 to the SDA pin (GPIO 21 pin) of the ESP32
- SCL of the BME280 to the SCL pin (GPIO 22 pin) of the ESP32

Working Principle
The BME280 sensor measures temperature, pressure, and humidity by utilizing internal sensors and calibration data. It communicates with the ESP32 using the I2C bus. The provided code initializes the I2C parameters, reads the sensor data, and prints the temperature, pressure, and humidity values on the serial monitor.
Code:
Now let's understand and write the C code to measure temperature, pressure and humidity. Follow the steps below:
Open VS Code and create a new sample project with the name bmp280 (Follow previous guides to see how to do it).
Now navigate to the "Main" folder of the project and then open the Main.c file.
After that, let’s add the necessary header files and source files required for this project. Download them from here into the Main folder of your project.
Your folder structure in the project should look like this after downloading the files:

The CMakeLists.txt file here is modified for specifying the source and header files required for interfacing the sensor. All the i2c initialization and reading of the data is done with the help of bme280.c and helper.c file.
The main.c contains the following code :
(To change the SDA and SCL used from default GPIO 21 and 22 go to the helper.c file)
#include <stdio.h>
#include "helper.c" // Import the helper file
#include "bme280.h"
// SensorData is a struct defined in helper.c
// It contains the temperature, pressure, and humidity
// of the BME280 sensor
void app_main(void)
{
// Initialize I2C parameters on GPIO 21 and 22
i2c_master_init();
while (1)
{
SensorData sensorData = readSensorData(); // read sensor data
printf("Temperature: %.2f degC\n", sensorData.temperature);
printf("Pressure: %.2f hPa\n", sensorData.pressure);
printf("Humidity: %.2f %%\n", sensorData.humidity);
vTaskDelay(2000 / portTICK_PERIOD_MS); // wait for 2 seconds
}
}
- Now at the bottom navbar of VS Code select the port, flash method and click on the Build, flash and monitor icon.
Note: During the flashing process of your board, you may encounter an error. If this happens, there's no need to worry. Simply flash the board again and when you see the message "connecting..........." appear, press and hold the boot button on your board for 1 or 2 seconds. Keep holding it until the dots on the screen start disappearing or the further flashing process begins. This action will help resolve any issues and ensure a successful flashing of your board.
Output
When you run the program and successfully interface the BME280 sensor with the ESP32 using the provided code, you will see the temperature, pressure, and humidity readings displayed on the serial monitor. The readings will be updated at regular intervals as specified in the program.
The output on the serial monitor will look similar to the following:
Temperature: 25.50 degC
Pressure: 1012.20 hPa
Humidity: 55.80 %
This indicates that the temperature is 25.50 degrees Celsius, the pressure is 1012.20 hPa (hectopascals), and the humidity is 55.80%.
Conclusion
By following this guide, you have successfully interfaced the BME280 sensor with the ESP32 microcontroller and have learned to include other source files and header files. You can now measure temperature, pressure, and humidity using the BME280 sensor and use the sensor data in your projects or applications as required.