Skip to content

Commit f97f7d4

Browse files
Merge pull request #47 from jposada202020/changing_busio_definition
Adding Note regarding the pressure capabilities of the sensor. Changing the use of busio. Including learning guide.
2 parents da8d5ed + 0a404e7 commit f97f7d4

File tree

5 files changed

+129
-66
lines changed

5 files changed

+129
-66
lines changed

README.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,17 @@ Usage Example
6161
6262
import board
6363
import digitalio
64-
import busio
6564
import time
6665
import adafruit_bme280
6766
68-
# Create library object using our Bus I2C port
69-
i2c = busio.I2C(board.SCL, board.SDA)
67+
# Create sensor object, using the board's default I2C bus.
68+
i2c = board.I2C() # uses board.SCL and board.SDA
7069
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
7170
#or with other sensor address
7271
#bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)
7372
74-
# OR create library object using our Bus SPI port
75-
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
73+
# OR create sensor object, using the board's default SPI bus.
74+
#spi = board.SPI()
7675
#bme_cs = digitalio.DigitalInOut(board.D10)
7776
#bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)
7877

adafruit_bme280.py

Lines changed: 115 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@
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
"""
1329
import math
1430
from time import sleep
@@ -113,7 +129,13 @@
113129

114130

115131
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+
"""
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):
464482
class 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):
507548
class 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

docs/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Table of Contents
2323
.. toctree::
2424
:caption: Tutorials
2525

26+
Adafruit BME280 I2C or SPI Temperature Humidity Pressure Sensor Learnng Guide <https://learn.adafruit.com/adafruit-bme280-humidity-barometric-pressure-temperature-sensor-breakout>
27+
2628
.. toctree::
2729
:caption: Related Products
2830

examples/bme280_normal_mode.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@
77
Refer to the BME280 datasheet to understand what these parameters do
88
"""
99
import time
10-
1110
import board
12-
import busio
1311
import adafruit_bme280
1412

15-
# Create library object using our Bus I2C port
16-
i2c = busio.I2C(board.SCL, board.SDA)
13+
# Create sensor object, using the board's default I2C bus.
14+
i2c = board.I2C() # uses board.SCL and board.SDA
1715
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
1816

19-
# OR create library object using our Bus SPI port
20-
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
17+
# OR create sensor object, using the board's default SPI bus.
18+
# spi = board.SPI()
2119
# bme_cs = digitalio.DigitalInOut(board.D10)
2220
# bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)
2321

examples/bme280_simpletest.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
# SPDX-License-Identifier: MIT
33

44
import time
5-
65
import board
7-
import busio
86
import adafruit_bme280
97

10-
# Create library object using our Bus I2C port
11-
i2c = busio.I2C(board.SCL, board.SDA)
8+
# Create sensor object, using the board's default I2C bus.
9+
i2c = board.I2C() # uses board.SCL and board.SDA
1210
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
1311

14-
# OR create library object using our Bus SPI port
15-
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
12+
# OR create sensor object, using the board's default SPI bus.
13+
# spi = board.SPI()
1614
# bme_cs = digitalio.DigitalInOut(board.D10)
1715
# bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)
1816

0 commit comments

Comments
 (0)