3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

M5StackとBME280センサーで、環境データを取得する

Last updated at Posted at 2020-06-03

はじめに

M5StackというESP32基板のマイコンボードに、環境センサーを接続し、温度、湿度、圧力の情報をM5StackのLCD画面に表示してみました。

作業内容

  1. BME280ボードを準備します。センサーライブラリーが充実している理由にAdafruit社の製品を購入します。
  2. M5Stack本体拡張ボードを準備します。
  3. BME280を拡張ボードに設置します。配線は、I2Cに合わせて行います。
  4. Arduino IDEを利用して、プログラムをアップロードします。

写真1. M5Stack本体の内部 ESP32が見えます。
image.png

写真2.M5Stack用拡張ボードにBME280を設置しました。中央部にMPU-9250という9軸センサーも設置しましたが、今回は説明対象外です。
image.png

実行結果

プログラムをアップロードします。
上手く起動しました。やった!
image.png

まとめ

1.M5StackというマイコンボードにBME280環境センサーを設置し、動作を確認しました。

お気づき

 マイコンボードにLCD画面が搭載されていると、活用度が上がりますね。デザインもかわいいので、M5Stackにしてよかったと思います。
 
 ESP32と近い位置に温度センサーを設置したため、室温より3-5℃高い数値を示します。

アップデート

  M5Stack+BME280から取得したデータをクラウドアップロードし、可視化した内容を掲載しました。
M5StackとBME280センサーで取得した環境データをクラウドで可視化する

参考資料

  1. みんなのM5Stack入門

IoT可視化サービスAmbinet社の下村さんが書いた本を参考にしました。
M5Stack,ESP32全般に関する情報をわかりやすく整理したので、M5Stackユーザーには絶対おすすめします。

image.png

プログラムコード

M5Stack_BME280.ino
/

# include <Wire.h> //IC2 Library
# include <Adafruit_Sensor.h> 
# include <Adafruit_BME280.h>
# include <M5Stack.h>

# define SEALEVELPRESSURE_HPA (1013.25) //Ambient Pressure

Adafruit_BME280 bme; // I2C Instance

unsigned long delayTime;

void setup() {

    //M5Stack Setup
    M5.begin();
    M5.lcd.setTextSize(3);
    
    Serial.begin(115200);
    while(!Serial);    // time to get serial running
    Serial.println(F("BME280 test"));

    unsigned status;
    
    // default settings
    status = bme.begin();  
    // You can also pass in a Wire library object like &Wire2
    // status = bme.begin(0x76, &Wire2)
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
        Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
        Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
        Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
        Serial.print("        ID of 0x60 represents a BME 280.\n");
        Serial.print("        ID of 0x61 represents a BME 680.\n");
        while (1) delay(10);
    }
    
    Serial.println("-- Default Test --");
    delayTime = 1000;

    Serial.println();
}


void loop() { 
    M5printValues();
    printValues();
    delay(delayTime);
}


void printValues() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

void M5printValues() {
    
    float temp = bme.readTemperature();
    float pres = bme.readPressure()/1000.0F; //[kPa]
    float humid = bme.readHumidity();

    M5.Lcd.setCursor(20,40);
    M5.Lcd.print("Temp: ");
    M5.Lcd.print(temp,1);
    M5.Lcd.print("degC");
    
    M5.Lcd.setCursor(20,100);
    M5.Lcd.print("Pressure: ");
    M5.Lcd.print(pres,1);
    M5.Lcd.print("kPa");

    M5.Lcd.setCursor(20,160);
    M5.Lcd.print("Humidity: ");
    M5.Lcd.print(humid,1);
    M5.Lcd.print("%RH");
    
}
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?