|Since||Origin / Contributor||Maintainer||Source|
This module communicates with BME280/BMP280 temperature/air presssure/humidity sensors (Bosch Sensortec) through I2C interface.
The module works only with the bme280_math module.
The BMP280 only supports temperature and air pressure measurements. It will give wrong readings for humidity but no warnings or errors. Sometimes sellers of breakout boards for these sensors confuse the two and sell one as the other.
To easily check if you have a BMP280 or a BME280 look at the shape of the sensor: The BMP280 has rectangular shape, whereas the BME280 has a square one.
Creates bme280sensor object and initializes the module. Initialization is mandatory before reading values. Note that there has to be a delay between some tens to hundreds of milliseconds between calling
setup() and reading measurements.
Functions supported by bme280sensor object: - setup() - read() - startreadout() - qfe2qnh - altitude - dewpoint
bme280.setup(id, [address, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter])
id- I2C bus number
address- BME280 sensor address.
BME280_I2C_ADDRESS1 = 0x76,
BME280_I2C_ADDRESS2 = 0x77. Default sensor address is
temp_oss- Controls oversampling of temperature data. Default oversampling is 16x.
press_oss- Controls oversampling of pressure data. Default oversampling is 16x.
humi_oss- Controls oversampling of humidity data. Default oversampling is 16x
sensor_mode- Controls the sensor mode of the device. Default sensor more is normal.
inactive_duration- Controls inactive duration in normal mode. Default inactive duration is 20ms.
IIR_filter- Controls the time constant of the IIR filter. Default filter coefficient is 16.
cold_start- If 0 then the BME280 chip is not initialised. Useful in a battery operated setup when the ESP deep sleeps and on wakeup needs to initialise the driver (the module) but not the chip itself. The chip was kept powered (sleeping too) and is holding the latest reading that should be fetched quickly before another reading starts (
bme280sensor:startreadout()). By default the chip is initialised.
|0||Skipped (output set to 0x80000)|
|1 and 2||Forced mode|
Using forced mode is recommended for applications which require low sampling rate or hostbased synchronization. The sensor enters into sleep mode after a forced readout. Please refer to BME280 Final Datasheet for more details.
||t standby (ms)|
sobj - BME280 Sensor Object (
nil if initialization has failed)
BME280 Sensor Object Methods¶
Re-initializes the sensor.
Parameters are the same as for the bme280.setup function.
Returned values are the same as for the bme280.setup function.
For given air pressure (called QFE in aviation - see wiki QNH article) and sea level air pressure returns the altitude in meters, i.e. altimeter function.
QNHcurrent sea level pressure
altitude in meters of measurement point
For given temperature and relative humidity returns the dew point in celsius.
Hrelative humidity in percent (100 means 100%)
Ttemperate in celsius
dew point in celsisus
For given altitude converts the air pressure to sea level air pressure (QNH).
altitudealtitude in meters of measurement point
sea level pressure
Reads the sensor and returns the temperature, the air pressure, the air relative humidity and see level pressure when
altitude is specified.
altitude- altitude in meters of measurement point. If provided also the air pressure converted to sea level air pressure is returned.
Ttemperature in celsius
Pair pressure in hectopascals
Hrelative humidity in percent
QNHair pressure in hectopascals (when
nil if the readout is not successful.
Starts readout (turns the sensor into forced mode). After the readout the sensor turns to sleep mode. Callback function is called with readout results.
callbackif provided it will be invoked after given
delay. Callback parameters are identical to
altitudein meters of measurement point (QNH is returned when specified)
delaysets sensor to forced mode and calls the
callback(if provided) after given number of milliseconds. For 0 the default delay is set to 113ms (sufficient time to perform reading for oversampling settings 16x). For different oversampling setting please refer to BME280 Final Datasheet - Appendix B: Measurement time and current calculation.
alt=320 -- altitude of the measurement place sda, scl = 3, 4 i2c.setup(0, sda, scl, i2c.SLOW) s = require('bme280').setup(0) tmr.create():alarm(500, tmr.ALARM_AUTO, function() local T, P, H, QNH = s:read(alt) local D = s:dewpoint(H, T) print(("T=%.2f, QFE=%.3f, QNH=%.3f, humidity=%.3f, dewpoint=%.2f"):format(T, P, QNH, H, D)) end)
Example with sensor in sleep mode between readouts (asynchronous readouts)
alt=320 -- altitude of the measurement place sda, scl = 3, 4 i2c.setup(0, sda, scl, i2c.SLOW) s = require('bme280').setup(0, nil, nil, nil, nil, 0) -- initialize to sleep mode tmr.create():alarm(1000, tmr.ALARM_AUTO, function() s:startreadout(function(T, P, H, QNH) local D = s:dewpoint(H, T) print(("T=%.2f, QFE=%.3f, QNH=%.3f, humidity=%.3f, dewpoint=%.2f"):format(T, P, QNH, H, D)) end, alt) end)
Altimeter function - calculate altitude based on current sea level pressure (QNH) and measure pressure
alt = 0 -- initial altitude sda, scl = 3, 4 i2c.setup(0, sda, scl, i2c.SLOW) s = require('bme280').setup(0) tmr.create():alarm(100, tmr.ALARM_AUTO, function() local _, P, _, lQNH = s:read(alt) if not QNH then QNH = lQNH end local altitude = s:altitude(P, QNH) print(("altitude=%.3f m"):format(altitude)) end)