[ESP32]DHT22(温湿度センサー)

ESP32

DHT22(温湿度センサー)

はじめに

DHT22とは、DHTシリーズの温度と湿度を同時に測定する複合センサーモジュールです。
AM2302高精度温度湿度センサーにより、DHT11より高い精度で取得可能。

DHT11DHT22SHT31SHT41
電源電圧3.3V~5.5V3V~5.5V2.4V~5.5V3.3V~5V
測定温度
(精度)
-20℃~60℃
(±2℃)
-40℃~80℃
(±0.5℃)
-40℃~+125℃
(±0.3℃)
-40℃~+125
(±0.2℃)
測定湿度
(精度)
5%~95%
(±5%)
0〜100%
(±2%)
0%~100%
(±2%)
0%~100%
(±1.8%)
通信方式1-Wire1-WireI2CI2C
価格約300円約600円約700円約800円
温湿度センサの比較

↑の表は、温湿度センサーで私がよく見るものを比較してみました。
価格は、時期や販売元により変動がありますので、参考程度です。
SHTシリーズの方が精度は良さそうですが、価格が少し高く、信号線が1本多いです。精度をそこまで求めない場合は、DHTシリーズの方が手頃な感じがします。

開発環境

OS : Windows 11 Pro
ESP32:ESP-WROOM-32
統合開発環境 : Arduino IDE 2.3.2
Arduino core for the ESP32:1.0.6
使用ライブラリ:DHT sensor library(1.4.6)

使用パーツ

ESP32開発ボード(38Pin)

電子工作ステーション https://electronicwork.shop/items/64134541d91711003035a5a6

温湿度センサー (DHT22)

電子工作ステーション https://electronicwork.shop/items/649bedc1675488002ff8f075

ジャンパーワイヤー(メスーメス)

センサーについている場合は、必要ありません。

電子工作ステーション https://electronicwork.shop/items/637f92415988dc5175f8a9ec

作業内容

配線図

DHT22 とは GPIO 18 を使用して接続します。

DHT22ESP32開発ボード(38Pin)
GNDGND
VCC3.3V
信号線GPIO 18

ライブラリインストール

DHT sensor library

DHT22 と 通信するためのライブラリである「DHT sensor library」をインストールします。

依存関係のインストールを聞かれた場合は、「全てをインストール」を選びます。

スケッチ作成

「DHT sensor library」のスケッチ例を使用します。
「ファイル」→「スケッチ例」→「DHT sensor library」→「DHTtester」を選択します。

「DHTtester.ino」が開きます。

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

#define DHTPIN 18     // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 3 (on the right) of the sensor to GROUND (if your sensor has 3 pins)
// Connect pin 4 (on the right) of the sensor to GROUND and leave the pin 3 EMPTY (if your sensor has 4 pins)
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println(F("DHTxx test!"));

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

10行目と34行目を修正します。

#define DHTPIN 18     // Digital pin connected to the DHT sensor

👈DHTPIN 2 → 18 に変更。

  Serial.begin(115200);

👈9600 → 115200 に変更。

コード説明

使用するGPIOピンとDHT番号で初期化しています。

DHT dht(DHTPIN, DHTTYPE);

湿度を取得しています。

  float h = dht.readHumidity();

温度を取得しています。

  float t = dht.readTemperature();

取得した湿度/温度から体感温度(熱指数)を計算しています。

  float hic = dht.computeHeatIndex(t, h, false);

第3引数は、華氏(°F)の場合は true 摂氏(°C)の場合は false を指定します。

動作確認

動作させてみると、シリアルモニタに取得した値が表示されます。

華氏(°F)表示もされていますが、馴染みのない場合は摂氏(°C)のみ使用してください。

HeatIndexは、体感温度(熱指数)です。熱中症対策に使用できそうですね。

おわりに

今回はサンプルコードを使用したので、コード量が多く見えますが、実際に温湿度を取得するコードは簡単です。
初期化をした後は、温湿度共に1行で取得できるので簡単ですね。体感温度(熱指数)も簡単に求めることができるため、面白そうです。

コメント

タイトルとURLをコピーしました