Using the BME280 Pressure and Temp sensor with ESP32

The BME280 based pressure and temperature sensors have an amazing accuracy for their price point (around 5USD, 5Euro, 5GBP or cheaper!) and they are incredibly easy to connect up and use. Both I²C or SPI busses are usually supported. Below is the one similar to those that are readily available.

BME280 capable of I2C or SPI connection

They usually come with header pins that need soldering, although local eBay sellers may offer soldered ones for a premium. For this tutorial I’m going to connect to the  I²C bus.

In this article we’ll connect one up to an ESP32. If you want a video guide as well as this page then click below;

Click to view

Circuit Diagram
Just connect your device to the hardware I²C of the ESP32, these are pins 21 and 22.

The code
To make this we need a library.  In the Arduino IDE open up the Library Manager (“Tools->Manage Libraries…”). In the search box type in “Adafruit bme280” and only one result should return (at time of writing). Install this library..

Example Code
From the menu “File->Examples->Adafruit BME280 Library->bme280 test”.  Compile, upload and then open your serial monitor.  if you get the following output:

BMP280 test
Could not find a valid BME280 sensor, check wiring,address…!

The the most likely culprit (presuming wiring is correct) is that the  I²C address of your sensor is different than the one supplied by Adafruit. If you don’t get this issue and have something like this:

BME280 test
Temperature = 25.60 *C
Pressure = 100585.72 Pa
Approx altitude = 61.73 m
Humidity = 61.73 %

then your sensor is being read fine and you can move onto the Height and Pressure section below.

Trouble-shooting the  I²C address
If you have wired everything correctly then the culprit is probably the I²C address of your sensor. Copy this code below (and upload) to scan the I²C addresses of all your devices attached to your Arduino.

This will result in a output on the serial monitor similar to that below:

I2C Scanner
Scanning…
I2C device found at address 0x76 !
done

So the BMP280 is address 0x76.  Yours may be different, follow the advice above this scanner code to enter this address into the example code.

Re-compile and upload the BMP280 demo code from above again. Hopefully this time you should have  a result like this:

BMP280 test
Temperature = 25.60 *C
Pressure = 100585.72 Pa
Approx altitude = 61.73 m
Humidity = 61.73 %

Height and Pressure
Pressure is returned in Pascals (Pascal is one of the many units of pressure) with the standard pressure  being 100,000 Pascals. The altitude is derived by knowing the air pressure at ground level. Air pressure is the result of all the air above it pressing down (just like the deeper you go in the sea the pressure increases due to the weight of water above this depth). The further you go up the less air there is above you. Knowing this a simple calculation can be made to calculate height above sea level based on the pressure measured. However air pressure varies around the world so the routine that calculates the height takes a Barometric Pressure reading for your local area, this is the line:

Serial.print(bmp.readAltitude(1013.25)); 

Just Google air pressure for where you live.

For an exercise, instead of adding the local area pressure you could pass in the current pressure (will need to convert from raw Pascals) and then it will show 0 meters when switched on and then measure height relative to the devices starting position and not relative to sea level.

Affiliate links
Affiliate links for the items shown: Clicking these costs you the same price but gives me a small commission, please consider supporting this channel by using them 🙂

BME280 ( identical to the video) : https://amzn.to/34mVCRl
BME280 (I2C only) : https://amzn.to/2JMc1qq
ESP32 : https://amzn.to/363XTSZ
Breadboards : https://amzn.to/2qiC0ik

2 Comments

  1. I have ordered BME280 half a dozen times, and they always send a BMP280 instead. From both ebay and aliexpress. Very disappointing. Maybe Amazon will be better and send the right one.

    • Hopefully!, that’s ridiculous though, the BMP280 is quite a bit cheaper, makes you wonder if it’s on purpose. The BMP280 I got from AliEspress was labelled as both BMP280 and BME280. I’ve also bought a BME280 from BangGood and that came correctly.

Comments are closed.