Makato-kan が 2021年05月22日22時24分24秒 に編集
初版
タイトルの変更
M5StickCで 温度と湿度と二酸化炭素とVOCの測定してみた
タグの変更
M5StickC
DHT11
SGP30
二酸化炭素
温度センサー
湿度センサー
本文の変更
![キャプションを入力できます](https://camo.elchika.com/86cb88baa7ce4f509f6503cbf2dc0a29b0ab7cce/687474703a2f2f73746f726167652e676f6f676c65617069732e636f6d2f656c6368696b612f76312f757365722f66373831653764372d303336352d343131662d613663362d3663623238333931346464342f62643334353930392d626463622d346261632d623265352d303733636233353930323334/) **はじめに** 前回投稿した [ お礼 いただいたM5StickCを使って二酸化炭素濃度測定](https://elchika.com/article/77f5ef6a-063a-4ac3-bd5a-50bc7f2488c7/) に DHT11をつけてSGP30の温度湿度補正と温度湿度表示も加えてみました。 **ハードウェア** M5StickC SGP30 DHT11はデータとVCCの間に10kΩのプルアップ抵抗を入れています。 接続はVCC3.3V データはG26 GNDの3本です。 **プログラム** ```#include <M5StickC.h> #include <Wire.h> #include "Adafruit_SGP30.h" #include <DHT.h> #define DHTPIN 26 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); #define DISP_BRIGHTNESS_MIN 9 uint8_t disp_brightness = DISP_BRIGHTNESS_MIN; Adafruit_SGP30 sgp; /* return absolute humidity [mg/m^3] with approximation formula * @param temperature [°C] * @param humidity [%RH] */ uint32_t getAbsoluteHumidity(float temperature, float humidity) { //approximation formula from Sensirion SGP30 Driver Integration chapter 3.15 const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3] const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3] return absoluteHumidityScaled; } void setup() { M5.Axp.ScreenBreath(disp_brightness); M5.begin(); M5.Lcd.setRotation(1); M5.Lcd.setTextFont(2); M5.Lcd.setTextColor(TFT_WHITE,TFT_BLACK); M5.Lcd.fillScreen(BLACK); Serial.begin(115200); while (!Serial) { delay(10); } // Wait for serial console to open! Serial.println("SGP30 test"); Wire.begin(32, 33);//I2C dht.begin(); if (! sgp.begin()){ Serial.println("Sensor not found :("); while (1); } Serial.print("Found SGP30 serial #"); Serial.print(sgp.serialnumber[0], HEX); Serial.print(sgp.serialnumber[1], HEX); Serial.println(sgp.serialnumber[2], HEX); // If you have a baseline measurement from before you can assign it to start, to 'self-calibrate' //sgp.setIAQBaseline(0x8E68, 0x8F41); // Will vary for each sensor! M5.Lcd.fillScreen(WHITE); delay(500); // M5.Lcd.fillScreen(RED); // delay(500); // M5.Lcd.fillScreen(GREEN); // delay(500); // M5.Lcd.fillScreen(BLUE); // delay(500); M5.Lcd.fillScreen(BLACK); delay(500); // text print M5.Axp.ScreenBreath(disp_brightness); M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(0, 10); M5.Lcd.setTextColor(WHITE); M5.Lcd.setTextSize(1); M5.Lcd.printf("Display Test!"); delay(1000); } int counter = 0; void loop() { //delay(3000); float DHThumidity = dht.readHumidity(); float DHTtemperature = dht.readTemperature(); String datath = ""+String(DHTtemperature)+" "+String(DHThumidity); // If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals //float temperature = 22.1; // [°C] float temperature = dht.readTemperature(); //float humidity = 45.2; // [%RH] float humidity = dht.readHumidity(); sgp.setHumidity(getAbsoluteHumidity(temperature, humidity)); if (! sgp.IAQmeasure()) { Serial.println("Measurement failed"); return; } Serial.print(datath); Serial.print(" ");Serial.print(sgp.TVOC); Serial.print(" "); Serial.println(sgp.eCO2); //Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t"); //Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm"); if (! sgp.IAQmeasureRaw()) { Serial.println("Raw Measurement failed"); return; } // Serial.print("Raw H2 "); Serial.print(sgp.rawH2); Serial.print(" \t"); // Serial.print("Raw Ethanol "); Serial.print(sgp.rawEthanol); Serial.println(""); delay(1000); counter++; if (counter == 30) { counter = 0; uint16_t TVOC_base, eCO2_base; if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) { Serial.println("Failed to get baseline readings"); return; } Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX); Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX); } //Add M5stick OLED M5.Lcd.fillScreen(BLACK); String str = " " + (String)sgp.TVOC + "[ppb] "; M5.Lcd.drawRightString(str,250,0,2); M5.Lcd.setCursor(0, 0, 2); M5.Lcd.println(" TVOC"); str = " " + (String)sgp.eCO2 + "[ppm] "; M5.Lcd.drawRightString(str,250,16,2); M5.Lcd.setCursor(0, 16, 2); M5.Lcd.println(" eCO2"); M5.Lcd.setCursor(0, 32, 2); M5.Lcd.println(" Temp"); str = " " + (String)DHTtemperature + "[C] "; M5.Lcd.drawRightString(str,250,32,2); M5.Lcd.setCursor(0, 32+16, 2); M5.Lcd.println(" Humi"); str = " " + (String)DHThumidity + "[%] "; M5.Lcd.drawRightString(str,250,32+16,2); delay(3000); } ``` **最後に** ナンパーセント充電できたか調べていないのですが 30分程度でバッテリーなくなるようです。