3
3
# SPDX-License-Identifier: MIT
4
4
5
5
"""
6
- `adafruit_bme280` - Adafruit BME280 - Temperature, Humidity & Barometic Pressure Sensor
6
+ `adafruit_bme280`
7
7
=========================================================================================
8
8
9
- CircuitPython driver from BME280 Temperature, Humidity and Barometic Pressure sensor
9
+ CircuitPython driver from BME280 Temperature, Humidity and Barometric
10
+ Pressure sensor
10
11
11
12
* 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
12
28
"""
13
29
import math
14
30
from time import sleep
113
129
114
130
115
131
class 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
+ """
117
139
118
140
# pylint: disable=too-many-instance-attributes
119
141
def __init__ (self ):
@@ -138,9 +160,6 @@ def __init__(self):
138
160
self ._t_fine = None
139
161
140
162
def _read_temperature (self ):
141
- """Private function to read the temperature
142
- :return: None
143
- """
144
163
# perform one measurement
145
164
if self .mode != MODE_NORMAL :
146
165
self .mode = MODE_FORCE
@@ -172,8 +191,8 @@ def _reset(self):
172
191
def _write_ctrl_meas (self ):
173
192
"""
174
193
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
177
196
"""
178
197
self ._write_register_byte (_BME280_REGISTER_CTRL_HUM , self .overscan_humidity )
179
198
self ._write_register_byte (_BME280_REGISTER_CTRL_MEAS , self ._ctrl_meas )
@@ -333,7 +352,7 @@ def measurement_time_max(self):
333
352
334
353
@property
335
354
def temperature (self ):
336
- """The compensated temperature in degrees celsius ."""
355
+ """The compensated temperature in degrees Celsius ."""
337
356
self ._read_temperature ()
338
357
return self ._t_fine / 5120.0
339
358
@@ -359,8 +378,7 @@ def pressure(self):
359
378
var1 = (1.0 + var1 / 32768.0 ) * self ._pressure_calib [0 ]
360
379
if not var1 : # avoid exception caused by division by zero
361
380
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"
364
382
)
365
383
pressure = 1048576.0 - adc
366
384
pressure = ((pressure - var2 / 4096.0 ) * 6250.0 ) / var1
@@ -464,26 +482,56 @@ def _write_register_byte(self, register, value):
464
482
class Adafruit_BME280_I2C (Adafruit_BME280 ):
465
483
"""Driver for BME280 connected over I2C
466
484
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
469
525
470
526
"""
471
527
472
- def __init__ (self , i2c , address : int = _BME280_ADDRESS ) -> None :
528
+ def __init__ (self , i2c , address = _BME280_ADDRESS ):
473
529
import adafruit_bus_device .i2c_device as i2c_device # pylint: disable=import-outside-toplevel
474
530
475
531
self ._i2c = i2c_device .I2CDevice (i2c , address )
476
532
super ().__init__ ()
477
533
478
534
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
- """
487
535
with self ._i2c as i2c :
488
536
i2c .write (bytes ([register & 0xFF ]))
489
537
result = bytearray (length )
@@ -492,13 +540,6 @@ def _read_register(self, register, length):
492
540
return result
493
541
494
542
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
- """
502
543
with self ._i2c as i2c :
503
544
i2c .write (bytes ([register & 0xFF , value & 0xFF ]))
504
545
# print("$%02X <= 0x%02X" % (register, value))
@@ -507,26 +548,58 @@ def _write_register_byte(self, register, value):
507
548
class Adafruit_BME280_SPI (Adafruit_BME280 ):
508
549
"""Driver for BME280 connected over SPI
509
550
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
513
593
514
594
"""
515
595
516
- def __init__ (self , spi , cs , baudrate : int = 100000 ) -> None :
596
+ def __init__ (self , spi , cs , baudrate = 100000 ):
517
597
import adafruit_bus_device .spi_device as spi_device # pylint: disable=import-outside-toplevel
518
598
519
599
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
520
600
super ().__init__ ()
521
601
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 ):
530
603
register = (register | 0x80 ) & 0xFF # Read single, bit 7 high.
531
604
with self ._spi as spi :
532
605
spi .write (bytearray ([register ])) # pylint: disable=no-member
@@ -535,14 +608,7 @@ def _read_register(self, register: int, length: int) -> bytearray:
535
608
# print("$%02X => %s" % (register, [hex(i) for i in result]))
536
609
return result
537
610
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 ):
546
612
register &= 0x7F # Write, bit 7 low.
547
613
with self ._spi as spi :
548
614
spi .write (bytes ([register , value & 0xFF ])) # pylint: disable=no-member
0 commit comments