0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

BME280 温度・湿度・気圧センサーの環境構築と実行

Posted at

BME280 温度・湿度・気圧センサーの環境構築と実行

概要

BME280は、温度・湿度・気圧の3つを計測できるセンサーで、I2C通信によりRaspberry Piなどと接続できます。


必要なもの

  • BME280 センサーモジュール(I2C対応)
  • Raspberry Pi(または互換マイコン)
  • ジャンパーワイヤー
  • ブレッドボード(任意)

配線(I2C接続)

BME280 Raspberry Pi
VCC 3.3V (Pin 1)
GND GND (Pin 6)
SDA SDA (Pin 3)
SCL SCL (Pin 5)

注意: BME280 は通常 3.3V 動作です。5V に接続しないよう注意してください。


I2C の有効化と確認

1. I2C を有効にする

sudo raspi-config
# → [5 Interfacing Options] → [P5 I2C] → Enable
  1. I2Cツールのインストール
sudo apt update
sudo apt install -y i2c-tools python3-smbus
  1. 接続確認
i2cdetect -y 1

結果に 0x76 や 0x77 が表示されれば正常に認識されています。


Pythonでの読み取り

  1. ライブラリのインストール
pip3 install adafruit-circuitpython-bme280
  1. サンプルコード(Python)
import board
import busio
import adafruit_bme280

# I2C通信の初期化
i2c = busio.I2C(board.SCL, board.SDA)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

# 計測結果を表示
print(f"Temperature: {bme280.temperature:.2f} °C")
print(f"Humidity: {bme280.humidity:.2f} %")
print(f"Pressure: {bme280.pressure:.2f} hPa")

よくあるエラーと対策

エラー内容 原因と対策

i2cdetect に何も表示されない 配線ミス、I2Cが無効、電源不足などを確認
ModuleNotFoundError ライブラリのインストールができていない。再度 pip3 install を実行


参考リンク

BME280 Datasheet (Bosch)

Adafruit BME280 Pythonライブラリ


0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?