TOPに戻る

ラズパイ5 pythonの仮想環境 ⑫ 気圧センサDPS310

 InfineonのDPS310は、温度と気圧が出力されるMEMSセンサです。最高±0.002 hPa(または±0.02m)の高精度モードをもつので高度を扱う用途に適しています。

Adafruitのブレークアウト・ボード

 STEMMA QT(JST SH 4ピン)コネクタは2か所に装着されていて、どちらにつないでもかまいません。このコネクタを使ってI2Cで制御する場合、特に、ジャンパ線をつなぐなどは不要です。


   Adafruit DPS310 Precision Barometric Pressure and Altitude Sensor

気圧センサDPS310のおもなスペック

  データシート

  • 動作電圧 3.3V(デバイスは1.7~3.6 V)
  • 測定範囲 300~1200hPa
  • 確度 ±0.06hPa(or ±0.5m)
  • 分解能  ±0.002hPa(or ±0.02 m)(high precision mode)
  • インターフェース SPI(最大10MHz)、I2C(最大3.4MHz)
  • スレーブ・アドレス 0x77。SDOピン(裏面のAddr)の設定で0x76も設定できる

 気圧センサを比較します。

型名 会社名 電源電圧
[V]

測定範囲
[hPa]

絶対確度圧力
[hPa]
相対確度圧力
[hPa]
その他の機能
BMP280 Bosch 1.71~3.6 300~1100 ±1 ±0.12 温度
BME280 Bosch 1.71~3.6 300~1100 ±1 ±0.12 温度、湿度
BMP388 Bosch 1.65~3.6 300~1250 ±0.40 ±0.08 温度
BMP390 Bosch 1.65~3.6 300~1250 ±0.50 ±0.03 温度
BME680 Bosch 1.71~3.6 300~1100 ±0.6 ±0.12 温度、湿度、汚れ
BME688 Bosch 1.71~3.6 300~1100 ±0.6 ±0.12 温度、湿度、汚れ
DPS310 Infineon 1.7~3.6  3001200 ±1 ±0.06 温度
LPS25HB STMicroelectronics 1.7~3.6 260~1260 ±0.2 ±0.1 温度
LPS35HW
STMicroelectronics 1.7~3.6 260~1260 ±1 ±0.1 温度

環境

  • ハードウェア Raspberry Pi 5(4GBモデル)
  • OS Raspberry Pi OS (64ビット)、リリース日December 5th 2023
  • Windows10 22H2にて、ssh(OpenSSH_9.2p1 Debian-2+deb12u2, OpenSSL 3.0.11 19 Sep 2023)および、VNC Viewerを動作させている

接続

ライブラリを用意

yoshi@ras05:~ $ python -m venv envdps


yoshi@ras05:~ $ source envdps/bin/activate


(envdps) yoshi@ras05:~ $ pip list
Package    Version
---------- -------
pip        23.0.1
setuptools 66.1.1

  Infineon/RaspberryPi_DPS


(envdps) yoshi@ras05:~ $ pip install smbus
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting smbus
  Using cached smbus-1.1.post2.tar.gz (104 kB)
  Preparing metadata (setup.py) ... done
Installing collected packages: smbus
  DEPRECATION: smbus is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
  Running setup.py install for smbus ... done
Successfully installed smbus-1.1.post2


(envdps) yoshi@ras05:~ $ pip install DigitalPressureSensor
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting DigitalPressureSensor
  Using cached https://www.piwheels.org/simple/digitalpressuresensor/DigitalPressureSensor-1.0.0-py3-none-any.whl (4.5 kB)
Installing collected packages: DigitalPressureSensor
Successfully installed DigitalPressureSensor-1.0.0


(envdps) yoshi@ras05:~ $ pip list
Package               Version
--------------------- ---------
DigitalPressureSensor 1.0.0
pip                   23.0.1
setuptools            66.1.1
smbus                 1.1.post2

 サンプルの実行ファイルです。

  RaspberryPi_DPS/Examples/TempPressValues_310.py

  hPaの単位に変更し、dps310.pyで保存しました。

import DPS
from time import sleep

dps310 = DPS.DPS()
try:

        while True:
            scaled_p = dps310.calcScaledPressure()
            scaled_t = dps310.calcScaledTemperature()
            p = dps310.calcCompPressure(scaled_p, scaled_t) / 100.0
            t = dps310.calcCompTemperature(scaled_t)
            print(f'{p:5.4f}hPa {t:4.1f}`C')
            sleep(1)

except KeyboardInterrupt:
        pass

 実行している様子です。

 関数化するほどのではないので、このまま利用します。

グラフィック・ディスプレイに測定結果を表示

(envdps) yoshi@ras05:~ $ pip install st7789

(envdps) yoshi@ras05:~ $ pip install pillow

(envdps) yoshi@ras05:~ $ pip install numpy

(envdps) yoshi@ras05:~ $ pip install spidev

 /home/yoshi/envadc/lib/python3.11/site-packages/ST7789のなかにある__init__.pyを、 /home/yoshi/envdps/lib/python3.11/site-packages/ST7789のなかにある__init__.pyにコピペします。

 測定値をグラフィック・ディスプレイに表示するプログラムです。

import time
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import ST7789
import DPS

dps310 = DPS.DPS()

MESSAGE = "DPS310 Pressure Sensor"
print(MESSAGE)

disp = ST7789.ST7789( height=240,width=320,rotation=0,
    port=0,cs=0,dc=22,rst=25,backlight=None,spi_speed_hz=80 * 1000 * 1000,
    )

disp.begin()
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
COLOR_ORANGE = (255, 167, 24)
COLOR_RED    = (255, 0, 50)
COLOR_WHITE  = (255, 255, 255)
COLOR_BLACK  = (5, 5, 5)
img = Image.new('RGB', (320, 240), color=COLOR_WHITE)
draw = ImageDraw.Draw(img)
draw.text((10,20),MESSAGE, font=font, fill=COLOR_RED)
disp.display(img)

while 1:
    scaled_p = dps310.calcScaledPressure()
    scaled_t = dps310.calcScaledTemperature()
    p = dps310.calcCompPressure(scaled_p, scaled_t) / 100.0
    t = dps310.calcCompTemperature(scaled_t)
    print(f'{p:5.4f}hPa {t:4.1f}`C')
    MESSAGE0 = 'Pressure= '+str(round(p,4))+'hPa'
    MESSAGE1 = 'Temparature= '+str(round(t,1))+'`C'
    draw.rectangle((10, 74, 310,155), COLOR_BLACK)
    draw.text((20,90), MESSAGE0, font=font, fill=COLOR_ORANGE)
    draw.text((20,120),MESSAGE1, font=font, fill=COLOR_ORANGE)
    disp.display(img)
    time.sleep(3)

 実行中の様子です。

 

ソース

 RaspberryPi_DPS/DPS.pyを読むと、

  #  64 times (High Precision) | 1040384  <- Configured

と書かれているので、分解能  ±0.002hPa(or ±0.02 m)で測定しているものと思われます。

python仮想環境 備忘録

仮想環境の作成

python -m venv envtest

有効化

source envtest/bin/activate

無効化

deactivate

初期状態に(いったん deactivate してから)

python -m venv --clear envtest

 

-