LoginSignup
5
4

More than 5 years have passed since last update.

40: Raspberry Pi3 で BME280 温度、湿度、気圧計センサー

Last updated at Posted at 2018-05-10

はじめに

こちらは
Arduino と Raspberry Pi の違い
の個別記事で、BME280 で 温度、湿度と気圧を取得する。

目的

Projectが動作する環境を把握するため、温度湿度を取得したい。気圧はおそらく不要であるが、取得できるのでしてみた。

考え方

Raspberry Pi3はI2C接続ができる。I2C接続ができるセンサーのBME280を持ってくる。
I2C は、BUSなのでそれぞれのセンサーはAddressを持っている。それを見つけて、そこにアクセスする。
データシートを見て、データを取得する。データ取得にはいろいろと細かい作法があるが、 Libraryがあるのでそれを利用する。

接続


- I2C (SCL, SDA)
- VCC 3.3V
- GND

Hardware

Address 0x76 が表示されている。SCL, SDA の接続を逆にしていたら、Address は表示されない。

$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- 76 --   

software

I2Cは有効にしておく。

I2C package installation

$ sudo apt-get install i2c-tools

検出

sudo i2cdetect -y 1

data読み出し

$ i2cdump -y 1 0x76
No size specified (using byte-data access)
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f    0123456789abcdef
00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
10: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
20: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
80: 7f 70 89 6f a4 7f 9d 06 34 6f 98 68 32 00 b3 93    ?p?o????4o?h2.??
90: 2e d6 d0 0b 0e 1b e9 ff f9 ff ac 26 0a d8 bd 10    .??????.?.?&????
a0: 00 4b b3 00 00 00 00 00 00 00 00 00 33 00 00 c0    .K?.........3..?
b0: 00 54 00 00 00 00 60 02 00 01 ff ff 1f 60 03 00    .T....`?.?..?`?.
c0: 00 00 00 ff 00 00 00 00 00 00 00 00 00 00 00 00    ................
d0: 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    `...............
e0: 00 6d 01 00 13 07 00 1e 50 41 ff ff ff ff ff ff    .m?.??.?PA......
f0: ff 00 00 00 00 00 00 80 00 00 80 00 00 80 00 80    .......?..?..?.?

library

Switch Science のものは、MIT Licenseで配布されている。
(https://github.com/SWITCHSCIENCE/samplecodes/tree/master/BME280)


$ wget https://github.com/SWITCHSCIENCE/samplecodes/tree/master/BME280/Python27/bme280_sample.py

実行してみる。


$ python bme280_sample.py 
Traceback (most recent call last):
  File "bme280_sample.py", line 3, in <module>
    from smbus2 import SMBus
ImportError: No module named smbus2

実行すると、smbus2が必要となるので、別途インストールする。

$ sudo pip install smbus2

動作テスト

$ python bme280_sample.py 
temp : 21.29  ℃
pressure : 1010.82 hPa
hum :  58.51 %

連続表示

今度は、連続で1秒おきにデータを表示するように変更する。while True:でloopにして、time.sleep(1)を追加しただけである。

bme280_sample.pyの最後の方

if __name__ == '__main__':
        try:
            while True:
                readData()
                time.sleep(1)
        except KeyboardInterrupt:
                pass

差分はこれだけ。


$ diff bme280_sample.py BME280.py 143a144
>             while True:
144a146
>                 time.sleep(1)

実行のログ

$ python BME280.py 
temp : 21.39  ℃
pressure : 1010.92 hPa
hum :  58.64 %
temp : 21.38  ℃
pressure : 1010.91 hPa
hum :  58.64 %
temp : 21.39  ℃
pressure : 1010.90 hPa
hum :  58.65 %
temp : 21.38  ℃
pressure : 1010.91 hPa
hum :  58.65 %

このあと、繰り返し

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4