DHT11を使用してみる

更新日 2022-09-08 (木) 13:09:10

Arduinoを使用してDHT11で温度、湿度を測定してみる

必要ファイルのDownload

adafruitのオープンソースのDHT-sensor-library(ダウンロード)をダウンロードする。

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

Add_Lib1.png

Add_Lib2.png

以下のように「DHT sensor library」があることを確認

Add_Lib3.png

Add_Lib4.png

Add_Lib5.png

Add_Lib6.png

サンプルファイル

DHT11からのデータをシリアルモニタに表示する

接続図

配線図.png

プログラム

#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("℃");
}

シリアルモニタ

シリアルモニタ.png

Arduino基板図

Pin配置.png

参考


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS