aneuk aneuk - 2 months ago 27
C Question

Connecting two sensors dht11 and dht22 the same nodemcu module esp-12e

I have a project where I'need connect two sensors the DHT11 and DHT22 to module nodemcu esp-12e.
Is there any way to use the two sensors in the same module at the same time?

Answer

If you want an implementation example use steps below:

  • Get DHT library from https://github.com/adafruit/DHT-sensor-library/

  • Use the code snippet below, but do not forget to set your pins :

    #include "DHT.h"
    #define DHT11PIN 2
    #define DHT22PIN 3
    
    DHT dht11(DHT11PIN, DHT11 );
    DHT dht22(DHT22PIN, DHT22 );
    
    void setup() {
      Serial.begin(9600);
      Serial.println("DHTxx test!");
    
      dht11.begin();
      dht22.begin();
    }
    
    void loop() {
      delay(2000);
      float h11 = dht11.readHumidity();
      float t11 = dht11.readTemperature();
      float f11 = dht11.readTemperature(true);
    
      float h22 = dht22.readHumidity();
      float t22 = dht22.readTemperature();
      float f22 = dht22.readTemperature(true);
      //do print them..
    }