Guide for BME280 Sensor with Arduino (Pressure, Temperature, Humidity)
Содержание
- Introducing BME280 Sensor Module
- Parts Required
- Schematic
- Installing the BME280 library
- Installing the Adafruit_Sensor library
- Reading Pressure, Temperature, and Humidity
- How the Code Works
- Demonstration
- Wrapping Up
This guide shows how to use the BME280 sensor module with Arduino to read pressure, temperature, humidity and estimate altitude. We’ll show you how to wire the sensor, install the required libraries, and write a simple sketch to display the sensor readings.
You might also like reading other BME280 guides:
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. The BME280 sensor uses I2C or SPI communication protocol to exchange data with a microcontroller.
We’re using the module illustrated in the figure below.
This sensor communicates using I2C communication protocol, so the wiring is very simple. You connect the BME280 sensor to the Arduino Uno I2C pins as shown in the table below:
BME280 | Arduino |
Vin | 5V |
GND | GND |
SCL | A5 |
SDA | A4 |
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 | Arduino |
SCK (SCL Pin) | A5 |
SDI (SDA pin) | A4 |
If you use SPI communication protocol, you need to use the following pins:
BME280 | Arduino |
SCK (SPI Clock) | Pin 13 |
SDO (MISO) | Pin 12 |
SDI (MOSI) | Pin 11 |
CS (Chip Select) | Pin 10 |
Parts Required
To complete this tutorial you need the following parts:
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
Wire the BME280 sensor to your Arduino board as shown in the following schematic diagram.
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.
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.
SPI communication
As we’re going to use I2C communication, the following lines that define the SPI pins are commented:
Sea level pressure
A variable called SEALEVELPRESSURE_HPA is created.
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.
This example uses I2C communication protocol by default. As you can see, you just need to create an Adafruit_BME280 object called bme .
To use SPI, you need to comment this previous line and uncomment one of the following lines.
setup()
In the setup() , start a serial communication:
And the sensor is initialized:
Note: when testing the sensor, if you can’t get any sensor readings, you may need to find your BME280 sensor I2C address. With the BME280 wired to your Arduino, run this I2C scanner sketch to check the address of your sensor. Then, pass the address to the begin() method.
Printing values
In the loop() , the printValues() function reads the values from the BME280 and prints the results in the Serial Monitor.
Reading temperature, humidity, pressure, and estimate altitude is as simple as using the following methods on the bme object:
- bme.readTemperature() – reads temperature in Celsius;
- bme.readHumidity() – reads absolute humidity;
- bme.readPressure() – reads pressure in hPa (hectoPascal = millibar);
- bme.readAltitude(SEALEVELPRESSURE_HPA) – estimates altitude in meters based on the pressure at the sea level.
Demonstration
Upload the code to your Arduino Board.
Open the Serial Monitor at a baud rate of 9600.
You should see the readings displayed on the Serial Monitor.
Wrapping Up
The BME280 provides an easy and inexpensive way to get pressure, temperature and humidity readings. The sensor communicates via I2C communication protocol, which means that wiring is very simple, you just need to connect the sensor to the Arduino I2C pins.
Writing the code to get the sensor readings is also very straightforward thanks to the BME280_Adafruit library. You just need to use the readTemperature() , readHumidity() and readPressure() methods. You can also estimate altitude using the readAltitude() method.
We have guides for other sensors and modules with the Arduino that you may find useful:
Источник: