LoginSignup
4
3

More than 3 years have passed since last update.

気温・湿度・気圧センサー(BME280)を使って室内環境を測定

Last updated at Posted at 2019-10-15

きっかけ

・CO2に引き続き何か測定してみたいと思った
 どっかのサイト「BME280あるで」
 僕「温度・気圧・湿度全部測れるのお得やん」

使ったもの

・Raspberry Pi 3 model B+
・BME280 (スイッチサイエンスで売ってるもの)
・ブレットボード
・ジャンパワイヤ(オス-メス)

BME280はこんな感じ(背景に余計なものが写ってることに関してはあしからず)
↓これも千石電商で買ったけどここに詳細が載っている
https://www.switch-science.com/catalog/2236/

IMG_3097.jpg

結構小さい
小指の第一関節ぐらいの大きさ
BME280をやるのはn番煎じだと思いますがやっていきます

手順

はんだ付け

ピンソケットと基板ははんだ付けされていない状態で売られていたのではんだ付けをする
IMG_3097_handa.jpg
赤線の部分をはんだ付けする

接続

接続は以下の通り
BME280はI2Cと呼ばれる通信方式らしい

BME側 ラズパイ側 ジャンパワイヤ色
Vin 3.3v(1 or 17)
GND GND
SDO GND
SCK I2C/SDA(3)
SDI I2C/SCL(5)

bme280.png

諸々のセットアップ

raspi-configでI2Cを有効化する

root@raspberrypi:/home/pi# raspi-config

5 Interfacing Options  Configure connections to peripherals ←これを選択
↓
P5 I2C         Enable/Disable automatic loading of I2C kernel module ←これを選択
      ・
          ・
          ・
"Would you like the ARM I2C interface to be enabled?"と表示されるので<yes>を選択



コマンドラインからI2Cにアクセスするためのi2c-toolsをインストール

root@raspberrypi:/home/pi# apt update
root@raspberrypi:/home/pi# apt install -y i2c-tools



コマンドライン上でセンサーと通信できているか確認(BME280の場合はSDO端子をGNDにつなぐとアドレスは0x76)

root@raspberrypi:/home/pi# 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 --                         



必要なPythonライブラリをインストールする

root@raspberrypi:/home/pi# apt install -y python-smbus
root@raspberrypi:/home/pi# pip install smbus2

プログラムの実行

スイッチサイエンスのサンプルプログラムを落とす

root@raspberrypi:/home/pi# git clone https://github.com/SWITCHSCIENCE/BME280.git



BME280/Python27/に移動し、サンプルプログラムを実行する

root@raspberrypi:/home/pi# cd BME280/Python27/
root@raspberrypi:/home/pi/BME280/Python27/# python bme280_sample.py
temp : 24.87  ℃
pressure : 1008.77 hPa
hum :  49.74 %

気温・湿度・気圧を取得することができた
ここから先のプログラムの改良はよしなに

参考にしたサイト

・BME280関係
https://qiita.com/hawk777/items/2b910a81df480268e07e

・I2C関係
https://qiita.com/kamujun/items/51f85339bfd582b27752

有用な情報ありがとうございました

4
3
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
4
3