This library provides a compact option to read out data from a BME280 sensor via I2C. It is designed to be used for weather monitoring in Arduino projects. To optimize size and compatibility, internal arithmetics is based entirely on 32bit integers. If floating point values are required, they can easily be calculated as shown in the IntMath example. The sensor settings and compensation formulae follow the recommendations from Bosch given in the BME280 datasheet section 3.5.1 for the settings and 4.2.3 as well as 8.2 for the compensation algorithms.
The library implements an on-demand measurement using the "forced mode" (section 3.3 in the datasheet). This means:
- by default the sensor is in sleep mode using very little power
- if needed a measurement cycle is started
- after about 10ms, the measurement is finished and the sensor goes back in sleep mode automatically
- until next the next measurement is finished, the data can be read
To calculate temperature, pressure and humidity from the raw sensor data the 32bit algorithm given by Bosch in the data sheet is implemented. For energy saving, no oversampling ("single") is used as recommended for weather monitoring.
The library provides the functions
bool begin()
initialize the communication and sensor. Returnstrue
if a BME280 or BMP280 sensor is found. Optionally, you can provide your ownTwoWire
instance to be used:begin(TwoWire &wirePort)
. The latter makes most sense if you do not use standard I2C ports on the ESP-Plattform.void setAddress(unit8_t address)
optionally set the I2C address of the sensor (default is0x76
, you might trysetAddress(0x77)
if the sensor does not respondvoid reset()
reset sensor. Need to callbegin()
afterwardsvoid startMeasurement()
kick off a measurement cycle. The sensor goes back to sleep automatically thereafter.bool isMeasuring()
returnstrue
while a measurement is being performed.int32_t getTemperature()
returns the temperature in units of 0.01 DegC. An output value of "5123" equals to 51.23 DegCuint32_t getPressure()
returns the pressure in Pa. An output value of "96386" equals to 96386 Pa (or 963.86 hPa)uint32_t getHumidity()
returns the humidity in Q22.10 format (22 integer and 10 fractional bits). An output value of “47445” equals to 47445/1024 = 46. 333 %RH
The values for temperature, pressure and humidity are only read from the sensor once per measurement. Thus, subsequent calls to getTemperature()
or the equivalents will return the identical values until a new measurement is started. As for pressure and humidity calculations the temperature value is needed, getTemperature()
should be called first (however, the library internally keeps track and calculates the corresponding temperature in case you call the get*()
-functions in a different order).
A typical measurement in the loop()
would look like this (the complete sketch is in the example folder):
void loop() {
mySensor.startMeasurement();
while (!mySensor.isMeasuring()) {
Serial.println("Waiting for Measurement to start");
delay(1);
}
while (mySensor.isMeasuring()) {
Serial.println("Measurement in progress");
delay(1);
}
Serial.print("Temperature: ");
Serial.print(mySensor.getTemperature() / 100.);
Serial.println("°C");
Serial.print("Pressure: ");
Serial.print(mySensor.getPressure());
Serial.println(" Pa");
Serial.print("Humidity: "));
Serial.print(mySensor.getHumidity() / 1024.);
Serial.println("%rH");
delay(5000);
}
Important:
- The sensor will actually need a few moments before
isMeasuring()
returnstrue
. In the above example, the meassage is printed once on an Arduino Pro Mini. On other boards your mileage may vary. - If you call the
get*()
-functions before the measurement is finished, the sensor (and the thus theget*()
-functions) will return the data from the last previously finished measurement. This may or may not be important, depending on the time between subsequent measurements. get*()
will return the same value until the next timestartMeasurement()
is called (and the measurement is finished!).