Qwiic Atmospheric Sensor (BME280) Hookup Guide

Pages
Favorited Favorite 2

Arduino Library Overview

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

We've written a library to easily get setup and take readings from the Qwiic Atmospheric Sensor. However, before we jump into getting data from the sensor, let's take a closer look at the available functions in the library. You can install this library through the Arduino Library Manager. Search for SparkFun BME280 Arduino Library and you should be able to install the latest version. If you prefer manually downloading the libraries from the GitHub repository, you can grab them here:

Let's get started by looking at the functions that set up the Qwiic Atmospheric Sensor:

Class

In the global scope, construct your sensor object (such as mySensor or pressureSensorA) without arguments.

BME280 mySensor;

Object Parameters and setup()

Rather that passing a bunch of data to the constructor, configuration is accomplished by setting the values of the BME280 type in the setup() function. They are exposed by being public: so use the myName.aVariable = someValue; syntax.

Settable variables of the class BME280:

language:c
//Main Interface and mode settings
uint8_t commInterface;
uint8_t I2CAddress;
uint8_t chipSelectPin;

uint8_t runMode;
uint8_t tStandby;
uint8_t filter;
uint8_t tempOverSample;
uint8_t pressOverSample;
uint8_t humidOverSample;

Functions

.begin();
Initialize the operation of the BME280 module with the following steps:

  • Starts up the wiring library for I2C by default
  • Checks/Validates BME280 chip ID
  • Reads compensation data
  • Sets default settings from table
  • Sets operational mode to Normal Mode
Output: uint8_t

Returns the BME280 chip ID stored in the ID register.

.begin() Needs to be run once during the setup, or after any settings have been modified. In order to let the sensor's configuration take place, the BME280 requires a minimum time of about 2 ms in the sketch before you take data.

.beginSPI(uint8_t csPin);
Begins communication with the BME280 over an SPI connection.

Input: uint8_t

csPin: Digital pin used for the CS.

Output: Boolean

True: Connected to sensor.
False: Unable to establish connection.

.beginI2C(TwoWire &wirePort); or .beginI2C(SoftwareWire &wirePort);
Begins communication with the BME280 over an I2C connection. If #ifdef SoftwareWire_h is defined, then a software I2C connection is used.

Input: &wirePort

&wirePort: Port for the I2C connection.

Output: Boolean

True: Connected to sensor.
False: Unable to establish connection.

.setMode(uint8_t mode);
Sets the operational mode of the sensor. (For more details, see section 3.3 of the datasheet.)

Input: uint8_t

0: Sleep Mode
1: Forced Mode
2: Normal Mode

.getMode();
Returns the operational mode of the sensor.

Output: uint8_t

0: Sleep Mode
1: Forced Mode
2: Normal Mode

.setStandbyTime(uint8_t timeSetting);
Sets the standby time of the cycle time. (For more details, see section 3.3 and Table 27 of the datasheet.)

Input: uint8_t

0: 0.5ms
1: 62.5ms
2: 125ms
3: 250ms
4: 500ms
5: 1000ms
6: 10ms
7: 20ms

.setFilter(uint8_t filterSetting)
Sets the time constant of the IIR filter, which slows down the response time of the sensor inputs based on the number of samples required. (For more details, see section 3.4.4, Table 6, and Figure 7 of the datasheet.)

Input: uint8_t

0: filter off
1: coefficient of 2
2: coefficient of 4
3: coefficient of 8
4: coefficient of 16

.setTempOverSample(uint8_t overSampleAmount);
Sets the oversampling option (osrs_t) for the temperature measurements. (Directly influences the noise and resolution of the data.)

Input: uint8_t

0: turns off temperature sensing
1: oversampling ×1
2: oversampling ×2
4: oversampling ×4
8: oversampling ×8
16: oversampling ×16
Other: Bad Entry, sets to oversampling ×1 by default.

Note: Yes, we do know there is a spelling error in the name of the method. It will get corrected in the next library update.

.setPressureOverSample(uint8_t overSampleAmount);
Sets the oversampling option (osrs_p) for the pressure measurements. (Directly influences the noise and resolution of the data.)

Input: uint8_t

0: turns off pressure sensing
1: oversampling ×1
2: oversampling ×2
4: oversampling ×4
8: oversampling ×8
16: oversampling ×16
Other: Bad Entry, sets to oversampling ×1 by default.

.setHumidityOverSample(uint8_t overSampleAmount);
Sets the oversampling option (osrs_h) for the humidity measurements. (Directly influences the noise of the data.)

Input: uint8_t

0: turns off humidity sensing
1: oversampling ×1
2: oversampling ×2
4: oversampling ×4
8: oversampling ×8
16: oversampling ×16
Other: Bad Entry, sets to oversampling ×1 by default.

.setI2CAddress(uint8_t address);
Changes the I2C address stored in the library to access the sensor.

Input: uint8_t

address: The new I2C address.

.isMeasuring();
Checks the measuring bit of the status register for if the device is taking measurement.

Output: Boolean

True: A conversion is running.
False: The results have been transferred to the data registers.

.reset();
Soft resets the sensor. (If called, the begin function must be called before using the sensor again.)

.readFloatPressure();
Reads raw pressure data stored in register and applies output compensation (For more details on the data compensation, see section 4.2 of the datasheet.)

Output: float

Returns pressure in kPa.

.readFloatHumidity();
Reads raw humidity data stored in register and applies output compensation (For more details on the data compensation, see section 4.2 of the datasheet.)

Output: float

Returns humidity in %RH.

.readTempC();
Reads raw temperature data stored in register and applies output compensation (For more details on the data compensation, see section 4.2 of the datasheet.)

Output: float

Returns temperature in Celsius.

.readTempF();
Reads raw temperature data stored in register and applies output compensation (For more details on the data compensation, see section 4.2 of the datasheet.)

Output: float

Returns temperature in Fahrenheit.