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?

ESP32からBME280の温度を読み出してみる

Posted at

やりたいこと

ESP32で室温などを収集して、データベース化して遊んでみたい。
そのための準備として、まずはESP32でBME280というセンサから温度データを読み出してみた。
以下の記事からの続きです。

使ったもの

  • ESP32 devkitC

  • BME280使用 温湿度・気圧センサーモジュールキット

パスコン等、周辺部品が実装されているから便利。

  • その他ブレッドボード等
    • ブレッドボードは、以下のものを使用。穴数によってはESP32が載らないので注意。

準備

BME280キットのはんだ付け。

  • 端子をはんだ付け
  • J3をショート

ブレッドボードに実装・配線

BME280モジュール
ピン番号
ESP32ピン名 意味
1 3V3 3.3V電源
2 GND GND
3 - 3.3V接続するとI2Cモード
J3ショートすると接続不要
4 GPIO 21 I2C SDA
5 GND GND接続すると0x76アドレスに設定
6 GPIO 22 I2C SCL

ESP32の電源はひとまずPCのUSBから供給。
以下の写真のような感じになります。

image.png

必要なライブラリのインストール

Arduino IDEで以下の手順でインストール。

  1. Arduino IDEのメニューから「Sketch」→「Include Library」→「Manage Libraries」を選択
    image.png

  2. 検索窓に「Adafruit BME280」と入力し、検索結果から「Adafruit BME280」を選択
    image.png

  3. 「Install」ボタンをクリックして、ライブラリをインストール

コーディング

#include <Wire.h>
#include <Adafruit_BME280.h>

Adafruit_BME280 bme;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  if (!bme.begin(0x76)) {
    Serial.println("センサが見つかりません");
    while (1);
  }
}

void loop() {
  // センサーからデータを読み込む
  float temperature = bme.readTemperature();
  float pressure = bme.readPressure() / 100.0F;
  float humidity = bme.readHumidity();

  // 表示
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" degrees C");

  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" hPa");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  delay(1000);
}

動作確認

以下のような感じに、温度、湿度、気圧を出力できました。
image.png

感想

  • ICのデータシートをよく読んで、I2C通信のコードを組もうかと思っていたが、ちょっと面倒そうだと思って、AI(Gemini)に聞いたら一発で答え(専用ライブラリあるよー)を教えてくれた
    • ただし、一応githubを斜め読みするとか、自分には必要ない処理をしていたのでその部分は削除するとかのアレンジはした
  • スクラッチで作ろうとせず、まず調べるのが大事
  • AIの出力を鵜呑みにしないのも大事
  • 次は取得したデータをPC上のサーバWifi送信できるようにする
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?