用于ms5803-14ba的python库用于覆盆子pi over i2c的压力传感器
ms5803p的Python项目详细描述
MS5803年
用于MS5803-14BA的Python 3库,用于I2C上的覆盆子PI压力传感器。
基于Adafruit Arduino Library和Control Everything Python Library。当把原始读数修正为实际的温度和压力时,有些数学问题很复杂,可以从MS5803-14BA datasheet中验证。
支持在任何支持的过采样率(OSR)下从传感器读取压力和温度值。更高的osr导致更高的分辨率/精度,但需要更长的转换时间。支持的osr速率是[256、512、1024、2048、4096],也可以在MS5803.OSRs
处获得。
安装
在pypi上提供,因此您可以使用
pip3 install ms5803py
或者克隆此存储库并运行
python3 setup.py install
用法
ms5803和rpi使用i2c协议进行通信,因此需要在pi上设置i2c,如this Adafruit tutorial中所述。之后,ms5803需要连接到覆盆子pi,如this Adafruit tutorial所述。
完成后,您必须找到ms5803的i2c地址。为此,请在插入ms5803之前和之后运行以下命令。无论显示什么地址,都是您想要的地址:
sudo i2cdetect -y 1
它应该是0X76
或0x77
,如MS5803-14BA datasheet中所述,这取决于ms5803上的csb(chip select)管脚是高还是低。在Sparkfun breakout board上,i2c地址是0x76
,因此如果在初始化传感器时没有指定地址,我将其设置为默认值。
有关用法示例,请参见python3 example.py
:
import ms5803py
import time
s = ms5803py.MS5803()
while True:
# Do the batteries-included version, optionally specifying an OSR.
press, temp = s.read(pressure_osr=512)
print("quick'n'easy pressure={} mBar, temperature={} C".format(press, temp))
# Use the raw reads for more control, e.g. you need a faster sample
# rate for pressure than for temperature. Use a high OverSampling Rate (osr)
# value for a slow but accurate temperature read, and a low osr value
# for quick and inaccurate pressure readings.
raw_temperature = s.read_raw_temperature(osr=4096)
for i in range(5):
raw_pressure = s.read_raw_pressure(osr=256)
press, temp = s.convert_raw_readings(raw_pressure, raw_temperature)
print("advanced pressure={} mBar, temperature={} C".format(press, temp))
time.sleep(1)
结果:
quick'n'easy pressure=835.7 mBar, temperature=26.64 C
advanced pressure=835.3 mBar, temperature=26.64 C
advanced pressure=834.2 mBar, temperature=26.64 C
advanced pressure=835.3 mBar, temperature=26.64 C
advanced pressure=834.2 mBar, temperature=26.64 C
advanced pressure=834.2 mBar, temperature=26.64 C
quick'n'easy pressure=835.1 mBar, temperature=26.65 C
advanced pressure=835.3 mBar, temperature=26.63 C
advanced pressure=837.7 mBar, temperature=26.63 C
advanced pressure=834.2 mBar, temperature=26.63 C
advanced pressure=831.8 mBar, temperature=26.63 C
advanced pressure=835.3 mBar, temperature=26.63 C
...