M5Stack Core2 で Grove の温度湿度センサーを使う方法です。
Port A に Grove を挿します。
temperature_humidity/temperature_humidity.ino
// ---------------------------------------------------------------
/*
temperature_humidity.ino
Sep/01/2021
*/
// ---------------------------------------------------------------
# include <M5Core2.h>
# include "DHT.h"
# define DHTTYPE DHT22
# define DHTPIN 33
DHT dht(DHTPIN, DHTTYPE);
int count = 0;
// ---------------------------------------------------------------
void setup()
{
M5.begin(true, true, true, false);
M5.Lcd.setTextSize(4);
M5.Lcd.println("Temperature");
M5.Lcd.println("Humidity");
while (!Serial);
M5.lcd.setBrightness(80);
Serial.println("Sep/01/2021 AM 10:00 ***");
}
// ---------------------------------------------------------------
void display_proc(float temp_hum[])
{
Serial.print("temperature = ");
Serial.print(temp_hum[1]);
Serial.print(" C ");
Serial.print("humidity = ");
Serial.print(temp_hum[0]);
Serial.println(" %");
M5.Lcd.setTextSize(4);
M5.Lcd.setCursor(50,100);
M5.Lcd.print(temp_hum[1]);
M5.Lcd.println(" C");
M5.Lcd.setCursor(50,160);
M5.Lcd.print(temp_hum[0]);
M5.Lcd.println(" %");
}
// ---------------------------------------------------------------
void loop()
{
float temp_hum[2] = {0,0};
Serial.print("count = " + String(count) + " ");
if (!dht.readTempAndHumidity(temp_hum))
{
display_proc(temp_hum);
}
else
{
Serial.println("ERROR! *** readTempAndHumidity ***");
}
delay(5000);
count++;
}
// ---------------------------------------------------------------