Getting multiple readings of DHT22 sensors using esp32

hi guys;
i really need your help, cause i couldn't display data on serial monitor from the two sensors simultaneously;here is the code :

#include <DHT.h>;
#define DHTPIN2 4     // dht 22 sensor 1
#define DHTPIN1 14     // dht 22 sensor 2
#define DHTTYPE1 DHT22   // DHT 22
#define DHTTYPE2 DHT22   // DHT 22
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
double h1, t1;
double h2, t2;
void setup()
{
  delay(500); 
  Serial.begin(9600);
  delay(500);
  dht1.begin();
  dht2.begin();
}

void loop()
{
  h1 = dht1.readHumidity();
  t1 = dht1.readTemperature();
  h2 = dht2.readHumidity();
  t2 = dht2.readTemperature();

  //Print temp and humidity values to serial monitor.
  Serial.print("DHT1: ");
  Serial.print(h1);
  Serial.print("% , ");
  Serial.print(t1);
  Serial.print("°C   DHT2: ");
  Serial.print(h2);
  Serial.print("% , ");
  Serial.print(t2);
  Serial.println("°C , ");

  delay(1000); 
}

i would really appreciate your help!!!

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

What happens when you try the improperly posted code?

The DHT22 sensors are very slow. Don't try to read them more often than every 2 seconds.

What Arduino board are you using?

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

What happens when you run the sketch ?

What happens when you try?

1 Like

error

there is the output

I'm using the esp32
this is what i found after running the code

error

These comments are confusing.

If you swap the sensor pins what happens? Maybe sensor 2 is a dud?

I tried, but still have the same problem

The exact same problem? DHT2 does not show any values? or DHT1?

It's DHT2 ,cause the DHT1 keeps showing values
i'll try to change the pin

nothing change unfortunatlly

DHT22 are slow. Did you slow down the reading to 2000ms or more?

I tried like it's showen here

#include <DHT.h>;
#define DHTPIN2 27    
#define DHTPIN1 14     
#define DHTTYPE1 DHT22   // DHT 22
#define DHTTYPE2 DHT22   // DHT 22
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
double h1, t1;
double h2, t2;
void setup()
{
  delay(500); 
  Serial.begin(9600);
  delay(500);
  dht1.begin();
  dht2.begin();
}

void loop()
{
  h1 = dht1.readHumidity();
  t1 = dht1.readTemperature();
  delay(3000); 
  h2 = dht2.readHumidity();
  t2 = dht2.readTemperature();

  //Print temp and humidity values to serial monitor.
  Serial.print("DHT1: ");
  Serial.print(h1);
  Serial.print("% , ");
  Serial.print(t1);
  Serial.print("°C  ");
  delay(2000); 
  Serial.print("DHT2: ");
  Serial.print(h2);
  Serial.print("% , ");
  Serial.print(t2);
  Serial.println("°C  ");
  delay(2000); 
}

i only got this
image

I don't have a DHT22 to hand but your sketch works for me using 2 DHT11s by just changing the DHTTYPEs in the sketch

Are you using bare DHT22s (4 pins) or DHT22 modules (3 pins) ?

1 Like

with 4 pins ,so i'm using also a pull up resistor

How are you connecting the DHTs, the resistors and ESP32 ? If you are using a breadboard then check the connections and jumpers carefully.

Some breadboards have breaks in the centre of their power rails so make sure that both sensors have power and GND connections

I suggest that you write a small sketch that reads the temperature and humidity from one DHT22 and prints it. If that works change the DHT pin to your other one. If that works try both pins with your other DHT22

yes,I've already did this ,each of the sensor can work alone but when i combine them it doesn't work

Check your wiring very carefully. Are you using a breadboard with the 2 sensors ?

Post a clear 'photo of your project with 2 sensors

could you show me the wiring of what you done so it works for u?