Other Arduino Codes and Videos by Robojax

How to use Two or more DHT11 or DHT22 Temperature and Humidity Sensor

This is the Arduino code and video for 2 or more DHT22 Temprerature and Humidity Sensors Module

The actual sensor is AM2302 from AOSONG Chine. This video shows you how to use DHT22 to measure Temperature, humidity and disply in serial monitor or use for other perpose.
  1. DHT22 Library on GetHub
  2. DHT22 Manual (PDF)
  3. DHT22 Library from Robojax.com
  4. AM2302 Manufacturer Website

 /*
 * This is the Arduino code for two or more DHT22 (or DHT11) module to read temprature and humidity

 * Watch the video https://youtu.be/oi_GPSLjgBY
  * Other Arduino libarary and videos http://robojax.com/learn/arduino/
 *  * 
 * Written/updated by Ahmad Nejrabi for Robojax.com Video
 * Date: Nov 21, in 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 purpose 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 DHT1_PIN 2     // for first DHT module
#define DHT2_PIN 3     // for 2nd DHT module and do the same for 3rd and 4th etc.

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




// 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 dht1(DHT1_PIN, DHTTYPE);//for first DHT module
DHT dht2(DHT2_PIN, DHTTYPE);// for 2nd DHT module and do the same for 3rd and 4th etc.

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

  dht1.begin();//for first DHT module
  dht2.begin();//for 2nd DHT module  and do the same for 3rd and 4th etc.
}

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


  // Robojax.com test video
  Serial.print("Temperature 1: ");
  Serial.print(getTemp("c", 1));// get DHT1 temperature in C 
  Serial.print(" *C ");
  
  Serial.print(getTemp("f", 1));// get DHT1 temperature in F
  Serial.println (" *F");
  Serial.println("-----------------");  


  Serial.print("Temperature 2: ");
  Serial.print(getTemp("c", 2));// get DHT2 temperature in C 
  Serial.print(" *C ");
  
  Serial.print(getTemp("f", 2));// get DHT2 temperature in F
  Serial.println (" *F");
  Serial.println("-----------------");  
}


/*
 * getTemp(String req, int dhtCount)
 * returns the temprature related parameters
 * req is string request
 dhtCount is 1 or 2 or 3 as you wish
 * This code can display temprature in:
 * getTemp("c", 1) is used to get Celsius for first DHT
 * getTemp("f", 2) is used to get fahrenheit for 2nd DHT
 * getTemp("k"m 1) is used for Kelvin for first DHT
 * getTemp("hif", 1) is used to get fahrenheit for first DHT
 * getTemp("hic", 2) is used to get Celsius for 2nd DHT
 * getTemp("f", 2) is used to get humidity for 2nd DHT
 */
float getTemp(String req, int dhtCount)
{

if(dhtCount ==1){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht1.readHumidity();
  // Read temperature as Celsius (the default)
  float t1 = dht1.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f1 = dht1.readTemperature(true);

  // Compute heat index in Fahrenheit (the default)
  float hif1 = dht1.computeHeatIndex(f1, h1);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic1 = dht1.computeHeatIndex(t1, h1, false);

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

  // Compute heat index in Kelvin 
  float k1 = t1 + 273.15;
  if(req =="c"){
    return t1;//return Cilsus
  }else if(req =="f"){
    return f1;// return Fahrenheit
  }else if(req =="h"){
    return h1;// return humidity
  }else if(req =="hif"){
    return hif1;// return heat index in Fahrenheit
  }else if(req =="hic"){
    return hic1;// return heat index in Cilsus
  }else if(req =="k"){
    return k1;// return temprature in Kelvin
  }else{
    return 0.000;// if no reqest found, retun 0.000
  }
}// DHT1 end


if(dhtCount ==2){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h2 = dht2.readHumidity();
  // Read temperature as Celsius (the default)
  float t2 = dht2.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f2 = dht2.readTemperature(true);

  // Compute heat index in Fahrenheit (the default)
  float hif2 = dht2.computeHeatIndex(f1, h1);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic2 = dht2.computeHeatIndex(t2, h2, false);

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

  // Compute heat index in Kelvin 
  float k2 = t2 + 273.15;
  if(req =="c"){
    return t2;//return Cilsus
  }else if(req =="f"){
    return f2;// return Fahrenheit
  }else if(req =="h"){
    return h2;// return humidity
  }else if(req =="hif"){
    return hif2;// return heat index in Fahrenheit
  }else if(req =="hic"){
    return hic2;// return heat index in Cilsus
  }else if(req =="k"){
    return k2;// return temprature in Kelvin
  }else{
    return 0.000;// if no reqest found, retun 0.000
  }
}// DHT2 end

}//getTemp end




   

If you found this tutorial helpful, please support me so I can continue creating content like this. You can support me on Patreon ( Launched June 14, 2019): http://bit.ly/2ZnITLz

Or support me via PayPal