-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMP388example3.py
74 lines (58 loc) · 2.64 KB
/
BMP388example3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""BMP388example3, example for using the BMP388 python
module. Example using the sensor data ready interrupt and
the sensor interrupt pin to read the pressure and
temperature output registers.
This requires the RPi.GPIO python module
created Jan 30, 2022 OM
modified Jan 30, 2022 OM"""
"""
Copyright 2022 Owain Martin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import time, smbus, spidev
import BMP388
import RPi.GPIO as GPIO
# sensor data ready interrupt and interrupt pin set up and output reading example
# using a Pi GPIO to monitor the sensor interrupt pin
# uncomment line below if using SPI
#bmp388 = BMP388.BMP388('spi', spiPort = 0, spiCS = 1)
# uncomment the 2 lines below if using i2c
i2cAddress = 0x76
bmp388 = BMP388.BMP388('i2c', i2cAddress)
# Pi interrupt pin set up
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # uses numbering outside circles
#initialize input pin
interruptPin = 20
GPIO.setup(interruptPin,GPIO.IN)
GPIO.add_event_detect(interruptPin,GPIO.RISING)
# read chip id register - should return 0x50
print(f'Reading Chip ID register (should output 0x50): {hex(bmp388.single_access_read(0x00))}')
bmp388.set_sensor_enables(t = 1, p = 1) # enable T & P sensors
bmp388.set_power_mode('normal') # set power mode to normal - set after enabling sensors
bmp388.set_odr(0x07) # set ODR to 25/18 Hz (640ms)
bmp388.set_osr(t_osr = 1, p_osr = 4) # set t_OSR x1, p_OSR x4
bmp388.set_iir_filter(0) # set iiR filter coeff to 0
bmp388.config_int_pin(outputMode = 'pushpull', level = 'high', latch = False)
bmp388.set_interrupts(drdy = 1, fifoFull = 0, fifoWtm = 0)
print('Ctrl-C to stop readings')
try:
while True:
# check status register to see if data is ready to be read
if GPIO.event_detected(interruptPin):
temperature, pressure = bmp388.get_output()
print(f'Temperature: {temperature:.2f}C Pressure: {pressure:.2f}hPa')
time.sleep(1)
except:
GPIO.cleanup([interruptPin])
bmp388.set_interrupts(drdy = 0, fifoFull = 0, fifoWtm = 0)