Arduinoを使用してDHT11で温度、湿度を測定してみる
adafruitのオープンソースのDHT-sensor-library(ダウンロード)をダウンロードする。
以下のように「DHT sensor library」があることを確認
DHT11からのデータをシリアルモニタに表示する
接続図
プログラム
#include <DHT.h>
#include <DHT_U.h>
/*
* DHT11センサー
* Arduinoのデジタル2ピンでDHT11センサーを接続する
*/
#define DHT11_PIN 2
DHT dht(DHT11_PIN, DHT11);
unsigned long ti;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
ti = millis();
// 湿度
float humidity = dht.readHumidity();
// 温度
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("センサーから読み取りに失敗しました。");
return;
}
// 体感温度
Serial.print(ti);
float heatIndex = dht.computeHeatIndex(temperature, humidity, false);
Serial.print("温度:");
Serial.print(temperature);
Serial.print("℃ 湿度:");
Serial.print(humidity);
Serial.print("% 体感温度:");
Serial.print(heatIndex);
Serial.println("℃");
}