Skip to content

Latest commit

 

History

History
72 lines (42 loc) · 4.03 KB

bme280.md

File metadata and controls

72 lines (42 loc) · 4.03 KB

BME280 driver

The Bosch Sensortec BME280 is a small sensor that can read temperature, humidity, and atmospheric pressure. The chipset supports I2C and SPI interfaces. This driver uses the AtomVM I2C interface for communicating with the BME280. This means you can take temperature, barometric pressure, and humidity readings using two GPIO pins on your ESP32.

Developers interact with this driver by starting an instance, specifying pins for the I2C data and clock pins. Starting an instance of the driver yeilds a reference that can be used in subsequent calls.

The primary operation in this module is the take_reading/1 function, which takes a reference to a BME280 driver, and returns a reading expressed as a tuple containing the temperature (in degrees celsius), atmospheric pressure (in hecto-pascals) and relative humidity (as a percentage).

Functions for reading the BME280 chip ide and version, as well as doing a soft reset of the device, are also supported.

Note. The BME280 sensor is a fairly dynamic sensor and can be used for many different applications (e.g., weather collection, gaming, drones, etc). The primary use-case for this driver is weather collection, which is assumed to be a low frequency operation. Some of the BME280 applications may require additional support in this driver, which would be relatively straightforward to support in future versions.

Further information about the Bosch Sensortec BME280 can be found in the reference documentation:

https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf

Programming Manual

The BME280 Driver is designed to provide a simple, easy to use interface for taking temperature, humidity, and atmospheric pressure measurements for your AtomVM application. This section describes the BME280 driver API, as encapsulated in the bme280 Erlang module.

Lifecycle

An instance of the BME280 driver is created using an instance of the I2CBus service. Start by initializing an I2Cbus using the i2c_bus module, specifying the sda and scl pins in a map structure:

{ok, I2CBus} = i2c_bus:start(#{
    sda => 21,
    scl => 22
}),

You can then use the bme280:start/1 and bme280:start/2 functions to start an instance of the BME280 driver:

{ok, BME} = bme280:start(I2CBus),
...

The return value includes a reference to the driver, which is used in subsequent operations.

Note. The I2CBus instance may be used to initialize other I2C devices that are multiplexed on the same I2C bus.

The arity-2 version of the start function allows additional parameters to be specified, in a manner described in more detail below.

To delete an instance of the driver, use the bme280:stop/1 function.

Note. This function is not well tested and its use may result in a memory leak.

Readings

Readings are obtained via a call to the bme280:take_reading/0 function.

Readings are represented as a tuple containing the temperature (in degrees celcius), atmospheric pressure (in hectopascals), and relative humidity, as a percentage. Each value contains an integral and fractional part, to two digits of precision.

 {{20, 88}, {1018, 30}, {23, 31}}

Oversampling

You may specify an oversampling rate for each data point (temperature, atmospheric pressure, or humidity) to reduce noise by adding the tuple {*_oversampling, Value)} to the Options properties list to the bme280:start/2 function, where *_oversampling can range over:

  • temp_oversampling
  • pressure_oversampling
  • humidity_oversampling

and where Value can take the following atomic values:

  • ignore -- skip measurement of this data point
  • x1 -- x1 oversampling
  • x2 -- x2 oversampling
  • x4 (default) -- x4 oversampling
  • x8 -- x8 oversampling
  • any other value -- x16 oversampling

BME280 Example

The BME280 driver includes an example program illustrating use of the BME280 driver. See the README for information about how to build, flash, and run this exmaple program.