Skip to content

Commit 161d961

Browse files
committed
ok done!
1 parent e918d66 commit 161d961

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

README.rst

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ Introduction
1010
:target: https://discord.gg/nBQh6qu
1111
:alt: Discord
1212
13-
TODO
13+
CircuitPython driver from BMP280 Temperature and Barometic Pressure sensor
1414

1515
Dependencies
1616
=============
1717
This driver depends on:
1818

1919
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
2020
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
21-
* `Register <https://github.com/adafruit/Adafruit_CircuitPython_Register>`_
2221

2322
Please ensure all dependencies are available on the CircuitPython filesystem.
2423
This is easily achieved by downloading
@@ -27,7 +26,32 @@ This is easily achieved by downloading
2726
Usage Example
2827
=============
2928

30-
TODO
29+
30+
.. code-block:: python
31+
32+
import board
33+
import digitalio
34+
import busio
35+
import time
36+
from adafruit_bmp280 import adafruit_bmp280
37+
38+
# Create library object using our Bus I2C port
39+
i2c = busio.I2C(board.SCL, board.SDA)
40+
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
41+
42+
# OR create library object using our Bus SPI port
43+
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
44+
#bmp_cs = digitalio.DigitalInOut(board.D10)
45+
#bmp280 = adafruit_bmp280.Adafruit_BMP280_SPI(spi, bmp_cs)
46+
47+
# change this to match the location's pressure (hPa) at sea level
48+
bmp280.seaLevelhPa = 1013.25
49+
50+
while True:
51+
print("\nTemperature: %0.1f C" % bmp280.temperature)
52+
print("Pressure: %0.1f hPa" % bmp280.pressure)
53+
print("Altitude = %0.2f meters" % bmp280.altitude)
54+
time.sleep(2)
3155
3256
Contributing
3357
============

adafruit_bmp280.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
`adafruit_bmp280`
2424
====================================================
2525
26-
CircuitPython driver from BMP280 sensor
26+
CircuitPython driver from BMP280 Temperature and Barometic Pressure sensor
2727
2828
* Author(s): ladyada
2929
"""
@@ -40,6 +40,7 @@
4040

4141
BMP280_REGISTER_CHIPID = const(0xD0)
4242
BMP280_REGISTER_DIG_T1 = const(0x88)
43+
"""
4344
BMP280_REGISTER_DIG_T2 = const(0x8A)
4445
BMP280_REGISTER_DIG_T3 = const(0x8C)
4546
BMP280_REGISTER_DIG_P1 = const(0x8E)
@@ -51,9 +52,8 @@
5152
BMP280_REGISTER_DIG_P7 = const(0x9A)
5253
BMP280_REGISTER_DIG_P8 = const(0x9C)
5354
BMP280_REGISTER_DIG_P9 = const(0x9E)
54-
BMP280_REGISTER_VERSION = const(0xD1)
55+
"""
5556
BMP280_REGISTER_SOFTRESET = const(0xE0)
56-
BMP280_REGISTER_CAL26 = const(0xE1) # R calibration stored in 0xE1-0xF0
5757
BMP280_REGISTER_STATUS = const(0xF3)
5858
BMP280_REGISTER_CONTROL = const(0xF4)
5959
BMP280_REGISTER_CONFIG = const(0xF5)
@@ -62,16 +62,20 @@
6262

6363
class Adafruit_BMP280:
6464
def __init__(self):
65+
"""Check the BMP280 was found, read the coefficients and enable the sensor for continuous reads"""
6566
# Check device ID.
66-
if BMP280_CHIPID != self._read_byte(BMP280_REGISTER_CHIPID):
67-
raise RuntimeError('Failed to find BMP280!')
67+
id = self._read_byte(BMP280_REGISTER_CHIPID)
68+
if BMP280_CHIPID != id:
69+
raise RuntimeError('Failed to find BMP280! Chip ID 0x%x' % id)
6870
self._read_coefficients()
69-
self._write_byte(BMP280_REGISTER_CONTROL, 0x3F);
7071
self.seaLevelhPa = 1013.25
7172

7273
@property
7374
def temperature(self):
7475
"""Gets the compensated temperature in degrees celsius."""
76+
# perform one measurement
77+
self._write_register_byte(BMP280_REGISTER_CONTROL, 0xFE); # high res, forced mode
78+
7579
# Wait for conversion to complete
7680
while (self._read_byte(BMP280_REGISTER_STATUS) & 0x08):
7781
time.sleep(0.002)
@@ -89,7 +93,7 @@ def temperature(self):
8993

9094
@property
9195
def pressure(self):
92-
"""Gets the compensated pressure in Pascals."""
96+
"""Gets the compensated pressure in hectoPascals."""
9397
self.temperature # force read
9498

9599
adc = self._read24(BMP280_REGISTER_PRESSUREDATA) / 16 # lowest 4 bits get dropped
@@ -106,10 +110,11 @@ def pressure(self):
106110
var1 = self.dig_P9 * p * p / 2147483648.0
107111
var2 = p * self.dig_P8 / 32768.0
108112
p = p + (var1 + var2 + self.dig_P7) / 16.0
109-
return p
113+
return p / 100
110114

111115
@property
112116
def altitude(self):
117+
"""calculate the altitude based on the sea level pressure (seaLevelPa) - which you must enter ahead of time)"""
113118
p = self.pressure / 100.0 # in Si units for Pascal
114119
return 44330 * (1.0 - math.pow(p / self.seaLevelhPa, 0.1903));
115120

@@ -123,17 +128,13 @@ def _read_coefficients(self):
123128
#print("%d %d %d" % (self.dig_P1, self.dig_P2, self.dig_P3))
124129
#print("%d %d %d" % (self.dig_P4, self.dig_P5, self.dig_P6))
125130
#print("%d %d %d" % (self.dig_P7, self.dig_P8, self.dig_P9))
126-
127-
def _write_byte(self, register, value):
128-
# Write a byte register value
129-
return self._write_register_byte(register, value)
130131

131132
def _read_byte(self, register):
132-
# Read a byte register value and return it.
133+
"""Read a byte register value and return it"""
133134
return self._read_register(register, 1)[0]
134135

135136
def _read24(self, register):
136-
# Read an unsigned 24-bit value as a floating point and return it.
137+
"""Read an unsigned 24-bit value as a floating point and return it."""
137138
ret = 0.0
138139
for b in self._read_register(register, 3):
139140
ret *= 256.0
@@ -144,7 +145,6 @@ class Adafruit_BMP280_I2C(Adafruit_BMP280):
144145
def __init__(self, i2c, address=BMP280_ADDRESS):
145146
import adafruit_bus_device.i2c_device as i2c_device
146147
self._i2c = i2c_device.I2CDevice(i2c, address)
147-
self._buffer = bytearray(3)
148148
super().__init__()
149149

150150
def _read_register(self, register, length):
@@ -159,3 +159,23 @@ def _write_register_byte(self, register, value):
159159
with self._i2c as i2c:
160160
i2c.write(bytes([register & 0xFF, value & 0xFF]))
161161
#print("$%02X <= 0x%02X" % (register, value))
162+
163+
class Adafruit_BMP280_SPI(Adafruit_BMP280):
164+
def __init__(self, spi, cs, baudrate=100000):
165+
import adafruit_bus_device.spi_device as spi_device
166+
self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate)
167+
super().__init__()
168+
169+
def _read_register(self, register, length):
170+
register = (register | 0x80) & 0xFF # Read single, bit 7 high.
171+
with self._spi as spi:
172+
spi.write(bytearray([register]))
173+
result = bytearray(length)
174+
spi.readinto(result)
175+
#print("$%02X => %s" % (register, [hex(i) for i in result]))
176+
return result
177+
178+
def _write_register_byte(self, register, value):
179+
register &= 0x7F # Write, bit 7 low.
180+
with self._spi as spi:
181+
spi.write(bytes([register, value & 0xFF]))

0 commit comments

Comments
 (0)