検索コード

DHT22温湿度センサー用のArduinoコードとビデオ

DHT22温湿度センサー用のArduinoコードとビデオ

このチュートリアルでは、DHT22温度・湿度センサーをArduinoで使う方法を学びます。DHT22(別名AM2302)は、摂氏-40度から80度までの温度と、0%から99%までの湿度を測定できます。このガイドに従えば、温度を摂氏・華氏・ケルビンで表示し、湿度も表示できるようになります。

DHT22 sensor with PCB-1

DHTセンサーライブラリを利用して、センサーからのデータを簡単に読み取ります。このライブラリはDHT22との通信を簡素化し、わずか数行のコードで温度と湿度の値にアクセスできるようにします。より詳しい説明は、関連する動画(動画の00:00)をご覧ください。

ハードウェア解説

このプロジェクトの主要なコンポーネントはDHT22センサーモジュールで、デジタル温湿度センサーです。静電容量式の湿度検出素子とサーミスタを用いて周囲の空気を測定します。出力はデジタル信号で、Arduinoで読み取ることができます。

DHT22は3.3〜5ボルトの電圧で動作し、1線式インターフェースで通信するため、プロジェクトへの組み込みが容易です。最大20メートルの長距離伝送に対応しており、センサー配置の柔軟性が高まります。

データシートの詳細

製造元アオソン
部品番号DHT22(AM2302)
ロジック/入出力電圧3.3〜5V
電源電圧3.3~5V
測定範囲(温度)-40〜+80°C
測定範囲(湿度)0〜99%
精度(温度)±0.5°C
精度(湿度)25℃で±2%
解像度0.1°C / 0.1%
伝送距離最大20 m
パッケージ4ピンモジュール

  • 電源は3.3V〜5Vの範囲で適切に供給してください。
  • データピンと電源の間に10kΩのプルアップ抵抗を使用してください。
  • 正確な測定のため、センサーの配線は短く保ってください。
  • 短時間での連続ポーリングは避け、読み取り間に遅延を設けてください。
  • 測定値に影響を与える環境要因に注意してください。

配線手順

Arduino wiring for DHT22 sensor
Arduino wiring for DHT22 sensor

DHT22センサーを配線するには、まずセンサーの電源ピン(ピン1)をArduinoの5V出力に接続します。次に、グラウンドピン(ピン4)をArduinoのGNDピンのいずれかに接続します。データピン(ピン2)は通信のためにArduinoのデジタルピン2に接続してください。さらに、安定した読み取りを得るためにデータピンと電源ピンの間に10kΩの抵抗を入れてください。

データ用に別のピンを使用している場合は、それに合わせてコード内のピン指定を変更して更新することを忘れないでください#define DHTPIN選択したピンに合わせる値。設定は簡単なはずです。これらの接続に従えば、センサーは正しく動作します。

コード例と解説

次のコードはDHT22センサーを初期化し、温度と湿度の値を読み取ります。まず、DHTライブラリをインクルードし、センサーが接続されているピンを定義します:

#include "DHT.h"
#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

ここで、変数DHTPINは2に設定されており、これはセンサーのデータピンがArduinoのデジタルピン2に接続されていることを示します。そのDHTTYPE使用されるセンサーの種類を定義しており、この場合はDHT22です。

次に、その中でsetup()関数内で、センサーとシリアルモニタを初期化します:

void setup() {
  Serial.begin(9600);
  dht.begin();
}

このコードスニペットはシリアル通信を初期化し、DHTセンサーの読み取りの準備を行います。そのSerial.begin(9600)シリアル通信のボーレートを設定します。

その中でloop()この関数で、温度と湿度の値を読み取り、表示できます:

Serial.print("Temperature: ");
Serial.print(getTemp("c")); // Celsius
Serial.print(" *C ");
Serial.print(getTemp("h")); // Humidity
Serial.println(" % ");

このコードのこの部分は摂氏の温度と湿度(%)をシリアルモニタに出力します。関数getTemp()渡されたパラメータに基づいて要求されたデータを取得するために使用されます。

デモンストレーション/当日の内容

すべてが設定され、コードがアップロードされると、シリアルモニタに温度と湿度の測定値が表示されるはずです。DHT22は安定するまで少し時間がかかることがあるので、測定間に数秒の間隔をあけてください(ビデオでは15:00)。測定値に問題がある場合は配線を確認し、接続が確実かどうかを確認してください。よくある落とし穴はピン割り当ての誤りや電源の問題です。

動画のタイムスタンプ

  • 00:00- DHT22センサーの紹介
  • 01:30- センサーの配線
  • 03:00- コードの説明
  • 04:30- コードデモ
  • 06:00- 結論

画像

Arduino wiring for DHT22 sensor
Arduino wiring for DHT22 sensor
DHT22 sensor with PCB-1
DHT22 sensor with PCB-1
DHT22 with PCB red
DHT22 with PCB red
DHT22 sensor no PCB
DHT22 sensor no PCB
54-Arduion code DHT22 Temperature and Humidity Sensor Module
言語: C++
++
/*
 * This is the Arduino code for the DHT22 module to read temperature and humidity.
 * This code can display temperature in:
 * getTemp("c") is used to get Celsius.
 * getTemp("f") is used to get Fahrenheit.
 * getTemp("k") is used for Kelvin.
 * getTemp("hif") is used to get Fahrenheit.
 * getTemp("hic") is used to get Celsius.
 * getTemp("h") is used to get humidity.
 * Watch the video https://youtu.be/oi_GPSLjgBY
 * Other Arduino library and videos https://robojax.com
 * 
 * Written/updated by Ahmad Shamshiri for Robojax.com Video
 * Date: Jan 08, 2018, in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * 
 * original source code : https://github.com/adafruit/DHT-sensor-library
 */

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


#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

// 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 4 (on the right) of the sensor to GROUND
// 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 processors.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx Robojax test!");

  dht.begin();
}

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


  // Robojax.com test video
  Serial.print("Temperature: ");
  
  Serial.print(getTemp("c"));
  
  Serial.print(" *C ");
  Serial.print(getTemp("f"));
  Serial.println (" *F");
  Serial.println("-----------------");  
  Serial.print("Heat index: ");
  Serial.print(getTemp("hic"));
  Serial.print(" *C ");
  Serial.print(getTemp("hif"));
  Serial.println(" *F");
  Serial.print(getTemp("k"));
  Serial.println(" *K");
  Serial.println("-----------------");    
  Serial.print("Humidity: ");
  Serial.print(getTemp("h"));
  Serial.println(" % ");
  Serial.println("===========================");
}


/*
 * getTemp(String req)
 * returns the temperature related parameters
 * req is string request
 * This code can display temperature in:
 * getTemp("c") is used to get Celsius.
 * getTemp("f") is used to get Fahrenheit.
 * getTemp("k") is used for Kelvin.
 * getTemp("hif") is used to get Fahrenheit.
 * getTemp("hic") is used to get Celsius.
 * getTemp("h") is used to get humidity.
 */
float getTemp(String req)
{

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (it's 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);

  // 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);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return 0.0;
  }
  // Compute heat index in Kelvin 
  float k = t + 273.15;
  if(req =="c"){
    return t;//return Celsius
  }else if(req =="f"){
    return f;// return Fahrenheit
  }else if(req =="h"){
    return h;// return humidity
  }else if(req =="hif"){
    return hif;// return heat index in Fahrenheit
  }else if(req =="hic"){
    return hic;// return heat index in Celsius
  }else if(req =="k"){
    return k;// return temperature in Kelvin
  }else{
    return 0.000;// if no request found, return 0.000
  }
 
}

ファイル📁

Arduinoライブラリ(zip)

フリッツィングファイル

ユーザーマニュアル