Das elfte Türchen

GY-BME280 Barometric sensor for temperature, humidity and air pressure

Behind today's advent calendar door number 11 you will find the BME280 sensor in the 5 pack. With it, the temperature (-40 to +85 ° C), the humidity (0 to 100% RH) and the air pressure (300 to 1100 hPa) can be measured. It can be operated with 3.3V and 5V. This is the variant with an I²C interface. The component needs an address for this. This is by default 0x76. The address can be opened using the three soldering pads below the small metal box (which represents the actual sensor) 0x77 to be changed:

 

There should be a (poorly visible) connection between the left and middle pad. This must be cut with a sharp, fine blade. Then the middle pad must be connected to the right pad.

If you are not sure which address is set, you may use an I2C scanner sketch in the Arduino IDE. The wire library (which is responsible for the I²C interface) provides such a sketch. Upload it to your micro controller and connect the sensor as follows:

 

or:


The sensors are supplied with header pins, which, however, still have to be soldered on.

The I²C pins of the individual microcontrollers are as follows:

µC

SDA

SCL

Nano

A4

A5

U.N

A4 or SDA (GPIO19)

A5 or SCL (GPIO18)

Mega

20

21

Leonardo / Micro

2

3

ESP32

GPIO 21

GPIO 22

ESP8266

GPIO 4 (D2)

GPIO 5 (D1)

 

You already know from past calendar doors how to put a Nano, Uno, ESP8266 or ESP32 into operation and execute a sketch.

The following should then be displayed in the serial monitor:

 


The sensor was recognized with the I²C address 0x76. So we can communicate with him.

In order to get the sensor data for temperature, humidity and air pressure, we use one of the libraries that can be installed via the library manager. It makes sense to use the Adafruit BME280 library:

 


Note: You also need the Adafruit Unified Sensor Library, which is usually installed at the same time. At least one should get a hint about it.

After the installation you will find the sketch under File-> Examples-> Adafruit BME280 Library bme280test.

Adjust the baud rate for the serial monitor in the set up() at:

Serial.begin (115200);

It is possible that the sensor is not yet recognized:

 


Then please complete the following line:

status = bme.begin ();

in:

status = bme.begin (0x76, & Wire);

Enter the address that you determined with the I²C scanner.

You should get the following output after the re-upload:

 


Temperature, air pressure and humidity can now be determined. As you can see, another value is output. This is the height above sea level (approx. Altitude). At my location, however, approx. 40 m should be displayed there. To correct this, you need to find out what air pressure is calculated for Mean Sea Level. This information is provided by the weather stations in your area. To do this, visit e.g. the website of the Weather service and search for your location using the postcode. Then click on the "Measured values" tab. In the table you will find the column "Air pressure". Copy this value and paste it into the source code in the following line (above):

#define SEALEVELPRESSURE_HPA (1013.25)

My current weather data shows an air pressure of 1017.5 hPa. I add this value to the line:

#define SEALEVELPRESSURE_HPA (1017.5)

Do I get the correct height where I am:

 


To find out whether this value is correct, you can e.g. this website visit and enter your location there. In addition to the coordinates, you will also be given the height above sea level. The air pressure changes constantly. The value should therefore be updated regularly.

If you want to deal more intensively with barometric altitude measurement, you could, among others, this article be interesting for you.

To give your creativity a little boost, I have a few more project ideas in connection with the BME280 sensor. Of course as a thermometer, e.g. with a connected display. Maybe an e-paper display and a slow refresh rate. That saves energy. You can also call up the values ​​online with a WLAN-enabled micro controller. Perhaps you own a garden shed and want to monitor the air conditioning from home. You can also carry out experiments with negative or positive pressure, e.g. in physics class. To use the altitude measurement, you could attach the components to a drone or a weather balloon. Pay attention to the correct height calculations. I once built a circuit for a rocket that recorded the telemetry data on an SD card during flight.

If you now want to monitor several experiments at the same time with only one micro controller, it becomes a bit problematic. As you have already read above, only two I²C addresses can be set. So you could only use two sensors. It is possible to use other pins for the I²C connection with the Softwire and SoftwareWire libraries. Unfortunately these are not compatible with the BME libraries.

Another option is to get one I²C multiplexer to use. Eight devices can be connected there. In my Halloween blog post I had already introduced it.

Note: Remember that the cable lengths for the I²C connection are restricted.

An HTTP server sketch is included in the examples for the ESP8266. For this case I modified it so that the data of the BME280 are displayed. I took portions of the source code from the one shown above bme280test-Sketch inserted there. I use an ESP8266 D1 Mini as a server:

 


You can use the updated sketch here download.

Enter your WLAN data for SSID and PASSWORD there. You should also update the air pressure for the correct altitude reading. When you have uploaded the sketch, you will see the IP address in the serial monitor. You can then enter this into a web browser on a device that is in the same WiFi network. The weather data are then output:

 

The function handleRoot () composed of a formatted string. For the output of the degree symbol and the umlauts you have to use the extended ASCII table .

In order not to exclude the Raspberry Pi users, I would like to briefly show how the BME280 sensor can be used there. The Raspi also has an I²C interface, which, however, has to be switched on in the raspi-config:

sudo raspi-config

then activate I2C via the interface options and restart the Raspi.

Connect the sensor as follows:

 

 

Adafruit offers a Python library for the Raspi that you can access Github finds. There is also a description of how to install it:

pip3 install adafruit-circuitpython-bme280

Python 3 and Pip must be installed for this.

You can then create a Python script and submit the sample code. Comment out the following line for the I²C address 0x76 by removing the hash at the beginning of the line:

bme280 = adafruit_bme280.Adafruit_BME280_I2C (i2c, address=0x76)

The other line uses the address 0x77 and has to be commented out:

# bme280 = adafruit_bme280.Adafruit_BME280_I2C (i2c)

In this line you enter the current air pressure again:

bme280.sea_level_pressure = 1013.25

Save the script e.B. as bme280test.py and start it from the terminal:

python3 bme280test.py

In the current Raspbian Buster desktop, there is a Python development environment called Thonny Python. In it you can also very easily write and execute scripts.

An alternative solution for using the BME280 on the Raspberry Pi can be found Here.

 

We wish you a happy Advent season.

Andreas Wolter

for AZ-Delivery Blog

Specials

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN