IoT/ESP32 B.Tech/Diploma Beginner

ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)

Learn how to use the BME280 sensor module with ESP32 to read pressure, temperature, humidity and estimate altitude using Arduino IDE. It uses I2C or SPI communication protocol.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

This guide shows how to use the BME280 sensor module with the ESP32 to read pressure, temperature, humidity and estimate altitude using Arduino IDE. The BME280 sensor uses I2C or SPI communication protocol to exchange data with a microcontroller.
We’ll show you how to wire the sensor to the ESP32, install the required libraries, and write a simple sketch that displays the sensor readings.
Recommended reading:
ESP32 Web Server with BME280 – Weather Station
Before proceeding with this tutorial you should have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven’t already.
Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)
You might also like reading other BME280 guides:
ESP32 Web Server with BME280 – Weather Station
ESP8266 with BME280 using Arduino IDE
ESP32/ESP8266 with BME280 using MicroPython
Arduino Board with BME280
Introducing BME280 Sensor Module
The
BME280 sensor
module reads barometric pressure, temperature, and humidity. Because pressure changes with altitude, you can also estimate altitude. There are several versions of this sensor module. We’re using the module illustrated in the figure below.
This sensor communicates using I2C communication protocol, so the wiring is very simple. You can use the default ESP32 I2C pins as shown in the following table:
BME280
ESP32
Vin
3.3V
GND
GND
SCL
GPIO 22
SDA
GPIO 21
There are other versions of this sensor that can use either SPI or I2C communication protocols, like the module shown in the next figure:
If you’re using one of these sensors, to use I2C communication protocol, use the following pins:
BME280
ESP32
SCK (SCL Pin)
GPIO 22
SDI (SDA pin)
GPIO 21
If you use SPI communication protocol, you need to use the following pins:
BME280
ESP32
SCK (SPI Clock)
GPIO 18
SDO (MISO)
GPIO 19
SDI (MOSI)
GPIO 23
CS (Chip Select)
GPIO 5
Parts Required
To complete this tutorial you need the following parts:
BME280 sensor module
ESP32
(read
Best ESP32 development boards
)
Breadboard
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 – ESP32 with BME280 using I2C
We’re going to use I2C communication with the BME280 sensor module. For that, wire the sensor to the ESP32
SDA
and
SCL
pins, as shown in the following schematic diagram.
Recommended reading:
ESP32 Pinout Reference Guide
Installing the BME280 library
To get readings from the BME280 sensor module you need to use the
Adafruit_BME280 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 bme280
” on the Search box and install the library.
Installing the Adafruit_Sensor library
To use the BME280 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.
Reading Pressure, Temperature, and Humidity
To read pressure, temperature, and humidity we’ll use a sketch example from the library.
After installing the BME280 library, and the Adafruit_Sensor library, open the Arduino IDE and, go to
File
>
Examples
>
Adafruit BME280 library
>
bme280 test

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
Serial.begin(9600);
Serial.println(F("BME280 test"));

bool status;

// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 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");

// Convert temperature to Fahrenheit
/*Serial.print("Temperature = ");
Serial.print(1.8 * bme.readTemperature() + 32);
Serial.println(" *F");*/

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("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");

Serial.println();
}
We’ve made a few modifications to the sketch to make it fully compatible with the ESP32.
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, and the
Adafruit_Sensor
and
Adafruit_BME280
libraries to interface with the BME280 sensor.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
SPI communication
As we’re going to use I2C communication, the following lines that define the SPI pins are commented:
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
Note:
if you’re using SPI communication, you use the ESP32 SPI pins. For SPI communication on the ESP32 you can use either the HSPI or VSPI pins, as shown in the following table.
SPI
MOSI
MISO
CLK
CS
HSPI
GPIO 13
GPIO 12
GPIO 14
GPIO 15
VSPI
GPIO 23
GPIO 19
GPIO 18
GPIO 5
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 more accurate results, replace the value with the current sea level pressure at your location.
I2C
This example uses I2C communication protocol by default. As you can see, you just need to create an
Adafruit_BME280
object called
bme
Adafruit_BME280 bme; // I2C
To use SPI, you need to comment this previous line and uncomment one of the following lines depending on whether you’re using hardware or software SPI (hardware SPI uses the ESP32 default HSPI pins; software SPI uses the pins defined on the code).
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
setup()
In the
setup()
, start a serial communication:
Serial.begin(9600);
And initialize the sensor:
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
We initialize the sensor with the
0x76
address. In case you’re not getting sensor readings, check the I2C address of your sensor. With the BME280 sensor wired to your ESP32, run
this I2C scanner sketch
to check the address of your sensor. Then, change the address if needed.
Printing values
In the
loop()
, the
printValues()
function reads the values from the BME280 and prints the results in the Serial Monitor.
void loop() {
printValues();
delay(delayTime);
}
Reading temperature, humidity, pressure, and estimate altitude is as simple as using the following methods on the
bme
object:
bme.readTemperature()
– reads temperature in C

Technologies used

ESP32ESP8266ArduinoBLEBME280PythonI2CSPIMicroPythonArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • BME280

Software & tools

  • Arduino IDE
  • MicroPython

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.