The bmx280_spi
python3 library uses SPI to communicate with a BME280 or BMP280 (BMx280) sensor. After testing a few different SPI modules that I wasn’t able to get working I decided to write a new module to configure and query the sensor.
This python class will query the sensor for the model (currently supports BME280 and BP280) and will facilitate programming and reading from the sensor.
- Wiring Diagram
- How It Works
- bmx280_spi – Install from PyPi
- Usage Example – CLI
- Usage Example – Importing into Code
- class BMX280_Exception(Exception)
- class Bmx280Readings
- class Bmx280Spi
- __init__(spiBus:int, cs_pin:int, cs_chip=None, set_spi_hz=True, logger=None,temp_enable=True, pressure_enable=True, humidity_enable=True, filter=0,temp_oversample=1, pressure_oversample=1, humidity_oversample=1,mode=MODE_FORCED, sleep_duration=0)
- init(temp_enable=True, pressure_enable=True, humidity_enable=True, filter=0,temp_oversample=1, pressure_oversample=1, humidity_oversample=1,mode=MODE_FORCED, sleep_duration=0)
- get_filter() -> int
- set_filter(vaule:int) -> bool
- get_sleep_duration_value() -> int
- get_sleep_duration_ms() -> int
- set_sleep_duration_value(vault:int) -> bool
- set_sleep_duration_ms(vaule:int) -> bool
- get_temp_oversample() -> int
- set_temp_oversample(value:int) -> bool
- get_pressure_oversample() -> int
- set_pressure_oversample(value:int) -> bool
- get_humidity_oversample() -> int
- set_humidity_oversample(value:int) -> bool
- get_power_mode() -> int
- set_power_mode(value:int) -> bool
- (property) measuring() -> bool
- (property) updating() -> bool
- reset_device()
- update_readings(timeout:int) -> Bmx280Readings
Related LearningToPi Posts | |
Git: https://github.com/LearningToPi/dht11_spi | |
PyPi: https://pypi.org/project/dht11-spi/ |
Wiring Diagram
Diagram Notes:
How It Works
- SPI bus is configured for a speed compatible with the sensor (1kHz to 1MHz).
- The sensor is configured with no filter and no oversampling and set to run in FORCED mode
- Alternate values can be passed during initialization or later using the
.init(...)
function to reprogram all settings, or individual configuration functions to update a single setting. - Readings can be pulled from the sensor using the
update_readings()
function. This will return an instance of theBmx280Readings
class containing all the returned values as well as unit converion functions.
bmx280_spi
– Install from PyPi
pip3 install -U bmx280_spi
Usage Example – CLI
The dht11_spi library can be called from the command line using some basic command line options to query the DHT11 or DHT22 sensor and print the temperature and humidity data to the console.
$ python3 -m bmx280_spi --help
usage: __main__.py [-h] [--gpio-chip GPIO_CHIP] [--time TIME] [--interval INTERVAL] [--spi SPI_BUS] [--spi-freq SPI_FREQ] [--temp-f] [--pressure PRES_FORMAT] gpio
Start a bme280/bmp280 test run using the bmx_spi library or the default RPi based library. Exact model is polled from the SPI device.
positional arguments:
gpio GPIO to use for signaling the DHT sensor. If using GPIO_CHIP other than 0, set the "--gpio-chip x" option.
optional arguments:
-h, --help show this help message and exit
--gpio-chip GPIO_CHIP
(default 0) GPIO chip for the GPIO provided. (0 typical for Pi4)
--time TIME (default 120) Time in seconds to run the test
--interval INTERVAL (default 1) Interval between reads. 1sec min for DHT11, 2sec min for DHT22 (per spec)
--spi SPI_BUS (default 0) SPI Bus number to use (assumes kernel driver loaded and accessible by spidev)
--spi-freq SPI_FREQ (default 500000Hz) Frequence to run on the SPI Bus.
--temp-f (default False) Print temps in F rather than C
--pressure PRES_FORMAT
(default psi) Prints the pressure reading in psi|bar|pa
(venv) $ python3 -m bmx280_spi --spi 1 13 --time 10 --pressure psi --temp-f
BME280:
2023-03-03 23:10:15,263 - root - INFO - 10/10 (100.0%): Temps (min/avg/max): 75.74/75.77/75.79 deg F, Humidity (min/avg/max): 24.94/24.95/24.98 %, Pressure (min/avg/max): 14.05/14.06/14.06 psi
BMP280:
Usage Example – Importing into Code
The dht11_spi library can be imported into your code and executed using the following example:
from ammeter_logger import AmmeterRecvSerial
# Import DHT SPI class
from dht11_spi import DHT11_Spi, DHT22_Spi
# initialize device, use (DHT11_Spi or DHT22_Spi)
# cs_chip and cs_pin from "gpioinfo". gpiod used for platform compat
dht = DHT22_Spi(spiBus=0, cs_chip=0, cs_pin=26)
reading = dht22.read() # returns instance of DhtReadings
print(reading)
# --or--
print(reading.temp_c, reading.temp_f, reading.humidity)
class BMX280_Exception(Exception)
Represents an error with the DHT11 class.
class Bmx280Readings
Represents a reading returend from the dht11_spi class. An object of this class will be returned when a read is called.
(property) temp_c()
Returns the temperature reading in Celcius
(property) temp_f()
Returns the temperature reading in Fahrenheit
(property) humidity()
Returns the humidity reading as a percentage (%)
(property) pressure_pa()
Returns the pressure reading in Pascal (Pa)
(property) pressure_psi()
Returns the pressure in pounds per square inch (psi)
(property) pressure_bar()
Returns the pressure reading in Bar
class Bmx280Spi
__init__(spiBus:int, cs_pin:int, cs_chip=None, set_spi_hz=True, logger=None,temp_enable=True, pressure_enable=True, humidity_enable=True, filter=0,temp_oversample=1, pressure_oversample=1, humidity_oversample=1,mode=MODE_FORCED, sleep_duration=0)
Initalizes BME280 or BMP280 sensor.
- spiBus (int) – SPI bus number the class should listen on.
- NOTE: For RPI4B systems, only SPI Bus 0 is operable
- cs_pin (int) – GPIO pin used as a CS for the BMx280
- cs_chip (int) – GPIO chip number for the
cs_pin
. Use “gpioinfo” to check for the proper chip number. If “None” uses chip 0- NOTE: For RPI4B systems, all GPIO’s are on chip 0
- set_spi_hz (bool) – Updates the SPI bus speed to match the BMx280 sensor. If the current SPI bus speed is higher than supported, the bus speed is lowered to the maximum allowed, if the current SPI bus speed is lower than supported, the bus speed is raised to the minimum allowed.
- NOTE: If you disable setting the SPI bus speed, be sure to set the speed to a supported frequency
- logger (Python logger) – Python logger to use for messages. See Python logging_handler for a function to create loggers.
- temp_enable (bool: True) – Enables temperature monitor
- pressure_enable (bool:True) – Enables the pressure monitor
- humidity_enable (bool:True) – Enables the humidity monitor
- filter (int:0) – Enables the filter, see
set_filter()
function below for more details - temp_oversample (int: 0) – Sets oversampling for the temp sensor. See
set_temp_oversample()
below for more details - pressure_oversample – Sets oversampling for the pressure sensor. See
set_pressure_oversample()
below for more details - humidity_oversample – Sets oversampling for the humidity sensor. See
set_humidity_oversample()
below for more details - mode (int:1) – Sets the mode of the sensor. See
set_power_mode()
for more details - sleep_duration (int:0) – Sets the sleep duration when running in NORMAL mode. See
set_sleep_duration_value()
for more details.
init(temp_enable=True, pressure_enable=True, humidity_enable=True, filter=0,temp_oversample=1, pressure_oversample=1, humidity_oversample=1,mode=MODE_FORCED, sleep_duration=0)
Initializes the sensor with the provided values. See the __init__()
function for details.
get_filter() -> int
Get the current filter value. See set_filter()
for details
set_filter(vaule:int) -> bool
Set the filter value. The filter is used limit transient changes in the temperature and pressure readings. Readings from one time period are carried forward and averaged with subsequent readings. The BMx280 datasheet refers to this as Infinite Impulse Response (IIR) filtering.
The BMx280 sensor supports values of 0, 2, 4, 8, and 16. At each reading the value stored in the data register is updated using the following formula:
See the Bosch datasheet for more details.
Returns True if the change was successful.
get_sleep_duration_value() -> int
Get the current sleep duration value. See set_sleep_duration_value()
for details
get_sleep_duration_ms() -> int
Get the current sleep duration in milliseconds. See set_sleep_duration_value()
for details
set_sleep_duration_value(vault:int) -> bool
Set the sleep duration between readings when running in NORMAL mode. (see set_power_mode()
for details on setting the mode). The integer values match up to the following times (in milliseconds):
Sleep Value | Time (ms) |
---|---|
0 | 0.5 |
1 | 62.5 |
2 | 125 |
3 | 250 |
4 | 500 |
5 | 1000 |
6 | 2000 |
7 | 4000 |
Returns True if the change was successful.
set_sleep_duration_ms(vaule:int) -> bool
Sets the sleep duration to a number of milliseconds. The value provided is rounded DOWN to the nearest matching value. See set_sleep_duration_value()
for detailed values supported by the BMx280 sensor.
Returns True if the change was successful.
get_temp_oversample() -> int
Get the current temperature sensor oversampling value. See set_temp_oversample()
for details.
set_temp_oversample(value:int) -> bool
Set the temperature sensor oversampling value. Oversampling utilizes the digital signal processor on the BMx280 to take multiple samples in rapid succession. The returned value is averaged to remove random noise. The BMx280 supports the following oversampling values: 1, 2, 4, 8, 16
Returns True if the change was successful.
get_pressure_oversample() -> int
Get the current pressure sensor oversampling value. See set_pressure_oversample()
for details.
set_pressure_oversample(value:int) -> bool
Set the pressure sensor oversampling value. Oversampling utilizes the digital signal processor on the BMx280 to take multiple samples in rapid succession. The returned value is averaged to remove random noise. The BMx280 supports the following oversampling values: 1, 2, 4, 8, 16
Returns True if the change was successful.
get_humidity_oversample() -> int
Get the current humidity sensor oversampling value. See set_temp_oversample()
for details.
set_humidity_oversample(value:int) -> bool
Set the humidity sensor oversampling value. Oversampling utilizes the digital signal processor on the BMx280 to take multiple samples in rapid succession. The returned value is averaged to remove random noise. The BMx280 supports the following oversampling values: 1, 2, 4, 8, 16
Returns True if the change was successful.
get_power_mode() -> int
Get the current power mode. 0 = Standby, 1 or 2 = Forced, 3 = Normal. Forced mode is only applicable while a forced read is in progress and is changed to 0 (Standby) once the read is complete.
set_power_mode(value:int) -> bool
Sets the power mode. 0 = Standby, 1 or 2 = Forced, 3 = Normal. Setting the power mode to Forced will trigger a reading then the mode will change to Standby.
Returns True if the change was successful.
(property) measuring() -> bool
Returns True if a measurement is in progress.
(property) updating() -> bool
Returns True if the sensor is in process of udating radings in NVRAM
reset_device()
Resets the sensor. Be sure to update settings after a reset.
update_readings(timeout:int) -> Bmx280Readings
Triggers a read. If the device is in Forced mode, this will trigger a read and return the results. If the device is in Normal mode, this will read the latest available data from the sensor. Be sure that reads are far enough apart based on the sleep timout set in set_sleep_timeout_value()