BME280 Module
This Lua module provides a simple, high-level interface to the Bosch BME280 sensor family. It is a convenient starting point if you want to read temperature, humidity, and pressure from Lua without writing low-level I2C register code yourself.
The module communicates with the sensor through the I2C API. If you want to understand the lower-level interaction, see the BME280 Lua source code.
Note
The module is currently included in the Xedge32 firmware, but in future versions it may be shipped separately.
Typical Workflow
Using the module normally looks like this:
Create a BME280 object.
Call
bme:read()whenever you want a new sensor reading.Reuse the object for repeated measurements.
Creating a BME280 Object
Function signature:
bme = require"bme280".create(port, address, sda, scl [, settings])
Parameters
port: I2C port number, for example0.address: I2C address of the sensor, typically0x76.sda: GPIO used for I2C data.scl: GPIO used for I2C clock.settings: Optional table with driver-specific configuration values.
The optional settings table is useful when you want to match a particular
oversampling or measurement setup. See the Weather Station demo
for a more advanced example.
Return Value
The function returns a new BME280 object.
BME280 Object Methods
bme:read()
Function signature:
local temperature, humidity, pressure = bme:read()
This method reads the current measurement values from the sensor.
Return values:
temperaturein degrees Celsius,humidityin percent relative humidity, andpressurein Pascals.
Basic Example
local bmeModule = require"bme280"
local port = 0
local address = 0x76
local sda = 5
local scl = 6
local bme = bmeModule.create(port, address, sda, scl)
local temp, hum, pres = bme:read()
trace("Temperature:", temp)
trace("Humidity:", hum)
trace("Pressure:", pres)
This example creates the sensor object once, reads one sample, and stores the three returned values in local variables.
Practical Notes
The BME280 and BMP280 are closely related devices, but only BME280 sensors provide humidity readings.
If communication fails, first verify the I2C address and the selected SDA/SCL pins.
Many breakout boards use
0x76or0x77depending on how the address pin is wired.