33# SPDX-License-Identifier: MIT
44
55"""
6- `adafruit_bme280` - Adafruit BME280 - Temperature, Humidity & Barometic Pressure Sensor
6+ `adafruit_bme280`
77=========================================================================================
88
9- CircuitPython driver from BME280 Temperature, Humidity and Barometic Pressure sensor
9+ CircuitPython driver from BME280 Temperature, Humidity and Barometric
10+ Pressure sensor
1011
1112* Author(s): ladyada
13+
14+ Implementation Notes
15+ --------------------
16+
17+ **Hardware:**
18+
19+ * Adafruit `BME280 Temperature, Humidity and Barometric Pressure sensor
20+ <https://www.adafruit.com/product/2652>`_ (Product ID: 2652)
21+
22+
23+ **Software and Dependencies:**
24+
25+ * Adafruit CircuitPython firmware for the supported boards:
26+ https://github.com/adafruit/circuitpython/releases
27+ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
1228"""
1329import math
1430from time import sleep
113129
114130
115131class Adafruit_BME280 :
116- """Driver from BME280 Temperature, Humidity and Barometic Pressure sensor"""
132+ """Driver from BME280 Temperature, Humidity and Barometric Pressure sensor
133+
134+ .. note::
135+ The operational range of the BMP280 is 300-1100 hPa.
136+ Pressure measurements outside this range may not be as accurate.
137+
138+ """
117139
118140 # pylint: disable=too-many-instance-attributes
119141 def __init__ (self ):
@@ -138,9 +160,6 @@ def __init__(self):
138160 self ._t_fine = None
139161
140162 def _read_temperature (self ):
141- """Private function to read the temperature
142- :return: None
143- """
144163 # perform one measurement
145164 if self .mode != MODE_NORMAL :
146165 self .mode = MODE_FORCE
@@ -172,8 +191,8 @@ def _reset(self):
172191 def _write_ctrl_meas (self ):
173192 """
174193 Write the values to the ctrl_meas and ctrl_hum registers in the device
175- ctrl_meas sets the pressure and temperature data acquistion options
176- ctrl_hum sets the humidty oversampling and must be written to first
194+ ctrl_meas sets the pressure and temperature data acquisition options
195+ ctrl_hum sets the humidity oversampling and must be written to first
177196 """
178197 self ._write_register_byte (_BME280_REGISTER_CTRL_HUM , self .overscan_humidity )
179198 self ._write_register_byte (_BME280_REGISTER_CTRL_MEAS , self ._ctrl_meas )
@@ -333,7 +352,7 @@ def measurement_time_max(self):
333352
334353 @property
335354 def temperature (self ):
336- """The compensated temperature in degrees celsius ."""
355+ """The compensated temperature in degrees Celsius ."""
337356 self ._read_temperature ()
338357 return self ._t_fine / 5120.0
339358
@@ -359,8 +378,7 @@ def pressure(self):
359378 var1 = (1.0 + var1 / 32768.0 ) * self ._pressure_calib [0 ]
360379 if not var1 : # avoid exception caused by division by zero
361380 raise ArithmeticError (
362- "Invalid result possibly related to error while \
363- reading the calibration registers"
381+ "Invalid result possibly related to error while reading the calibration registers"
364382 )
365383 pressure = 1048576.0 - adc
366384 pressure = ((pressure - var2 / 4096.0 ) * 6250.0 ) / var1
@@ -464,26 +482,56 @@ def _write_register_byte(self, register, value):
464482class Adafruit_BME280_I2C (Adafruit_BME280 ):
465483 """Driver for BME280 connected over I2C
466484
467- :param i2c: i2c object created with to use with BME280 sensor
468- :param int address: address of the BME280 sensor. Defaults to 0x77
485+ :param ~busio.I2C i2c: The I2C bus the BME280 is connected to.
486+ :param int address: I2C device address. Defaults to :const:`0x77`.
487+ but another address can be passed in as an argument
488+
489+ .. note::
490+ The operational range of the BMP280 is 300-1100 hPa.
491+ Pressure measurements outside this range may not be as accurate.
492+
493+ **Quickstart: Importing and using the BME280**
494+
495+ Here is an example of using the :class:`Adafruit_BME280_I2C`.
496+ First you will need to import the libraries to use the sensor
497+
498+ .. code-block:: python
499+
500+ import board
501+ import adafruit_bme280
502+
503+ Once this is done you can define your `board.I2C` object and define your sensor object
504+
505+ .. code-block:: python
506+
507+ i2c = board.I2C() # uses board.SCL and board.SDA
508+ bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
509+
510+ You need to setup the pressure at sea level
511+
512+ .. code-block:: python
513+
514+ bme280.sea_level_pressure = 1013.25
515+
516+ Now you have access to the :attr:`temperature`, :attr:`relative_humidity`
517+ :attr:`pressure` and :attr:`altitude` attributes
518+
519+ .. code-block:: python
520+
521+ temperature = bme280.temperature
522+ relative_humidity = bme280.relative_humidity
523+ pressure = bme280.pressure
524+ altitude = bme280.altitude
469525
470526 """
471527
472- def __init__ (self , i2c , address : int = _BME280_ADDRESS ) -> None :
528+ def __init__ (self , i2c , address = _BME280_ADDRESS ):
473529 import adafruit_bus_device .i2c_device as i2c_device # pylint: disable=import-outside-toplevel
474530
475531 self ._i2c = i2c_device .I2CDevice (i2c , address )
476532 super ().__init__ ()
477533
478534 def _read_register (self , register , length ):
479- """Private function to read a register with a provided length
480-
481- :param register: register to read from
482- :param length: length in bytes to read
483- :return: bytearray with register information
484- :rtype: bytearray
485-
486- """
487535 with self ._i2c as i2c :
488536 i2c .write (bytes ([register & 0xFF ]))
489537 result = bytearray (length )
@@ -492,13 +540,6 @@ def _read_register(self, register, length):
492540 return result
493541
494542 def _write_register_byte (self , register , value ):
495- """Private function to write on a register with a provided value
496-
497- :param register: register to write to
498- :param value: value to write on the selected register
499- :return: None
500-
501- """
502543 with self ._i2c as i2c :
503544 i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
504545 # print("$%02X <= 0x%02X" % (register, value))
@@ -507,26 +548,58 @@ def _write_register_byte(self, register, value):
507548class Adafruit_BME280_SPI (Adafruit_BME280 ):
508549 """Driver for BME280 connected over SPI
509550
510- :param spi: spi object created with to use with BME280 sensor
511- :param ~microcontroller.Pin cs: pin used for cs
512- :param int baudrate: the desired clock rate in Hertz of the spi. Defaults to 100000
551+ :param ~busio.SPI spi: SPI device
552+ :param ~digitalio.DigitalInOut cs: Chip Select
553+ :param int baudrate: Clock rate, default is 100000. Can be changed with :meth:`baudrate`
554+
555+ .. note::
556+ The operational range of the BMP280 is 300-1100 hPa.
557+ Pressure measurements outside this range may not be as accurate.
558+
559+ **Quickstart: Importing and using the BME280**
560+
561+ Here is an example of using the :class:`Adafruit_BME280_SPI` class.
562+ First you will need to import the libraries to use the sensor
563+
564+ .. code-block:: python
565+
566+ import board
567+ from digitalio import DigitalInOut, Direction
568+ import adafruit_bme280
569+
570+ Once this is done you can define your `board.SPI` object and define your sensor object
571+
572+ .. code-block:: python
573+
574+ cs = digitalio.DigitalInOut(board.D10)
575+ spi = board.SPI()
576+ bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)
577+
578+ You need to setup the pressure at sea level
579+
580+ .. code-block:: python
581+
582+ bme280.sea_level_pressure = 1013.25
583+
584+ Now you have access to the :attr:`temperature`, :attr:`relative_humidity`
585+ :attr:`pressure` and :attr:`altitude` attributes
586+
587+ .. code-block:: python
588+
589+ temperature = bme280.temperature
590+ relative_humidity = bme280.relative_humidity
591+ pressure = bme280.pressure
592+ altitude = bme280.altitude
513593
514594 """
515595
516- def __init__ (self , spi , cs , baudrate : int = 100000 ) -> None :
596+ def __init__ (self , spi , cs , baudrate = 100000 ):
517597 import adafruit_bus_device .spi_device as spi_device # pylint: disable=import-outside-toplevel
518598
519599 self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
520600 super ().__init__ ()
521601
522- def _read_register (self , register : int , length : int ) -> bytearray :
523- """Private function to read a register with a provided length
524-
525- :param int register: register to read from
526- :param int length: length in bytes to read
527- :return bytearray: bytearray with register information
528-
529- """
602+ def _read_register (self , register , length ):
530603 register = (register | 0x80 ) & 0xFF # Read single, bit 7 high.
531604 with self ._spi as spi :
532605 spi .write (bytearray ([register ])) # pylint: disable=no-member
@@ -535,14 +608,7 @@ def _read_register(self, register: int, length: int) -> bytearray:
535608 # print("$%02X => %s" % (register, [hex(i) for i in result]))
536609 return result
537610
538- def _write_register_byte (self , register : int , value : int ) -> None :
539- """Private function to write on a register with a provided value
540-
541- :param register: register to write to
542- :param value: value to write on the selected register
543- :return: None
544-
545- """
611+ def _write_register_byte (self , register , value ):
546612 register &= 0x7F # Write, bit 7 low.
547613 with self ._spi as spi :
548614 spi .write (bytes ([register , value & 0xFF ])) # pylint: disable=no-member
0 commit comments