Multiple DHT22 Sensors with LCD I2C display [SOLVED]

Hello everyone, We badly need our help. How to display temperature and humidity readings to LCD I2C display? The first DHT22 is ok but the second DHT22 only reads 3 on temperature and humidity. Here's my code thank you!

#include <DHT.h>
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h>

#define DHTPin1 4
#define DHTPin2 7
#define DHTTYPE1 DHT22
#define DHTTYPE2 DHT22

hd44780_I2Cexp lcd;

DHT dht1(DHTPin1, DHTTYPE1);
DHT dht2(DHTPin2, DHTTYPE2);
const int LCD_COLS = 16;
const int LCD_ROWS = 4;

// Define variables

float hum = 0.0;
float hum2 = 0.0;
float temp = 0.0;
float temp2 = 0.0;
void setup()
{
Serial.begin(9600);
dht1.begin();
dht2.begin();
lcd.begin(LCD_COLS, LCD_ROWS);
}
void loop()
{
delay(4000);

hum = dht1.readHumidity(); //
Serial.print(dht2.readHumidity());
temp = dht1.readTemperature();
Serial.print(dht2.readTemperature());

lcd.setCursor(0, 0);
lcd.print("Temp1:");
lcd.print(temp);
lcd.print("C");

lcd.setCursor(0, 1);
lcd.print("Temp2:");
lcd.print(Serial.print(dht2.readTemperature()));
lcd.print("C");
delay(4000);

lcd.setCursor(4, 2);
lcd.print("Humi1:");
lcd.print(hum);
lcd.print("%");

 lcd.setCursor(4, 3);

lcd.print("Humi2:");
lcd.print(Serial.print(dht2.readHumidity()));
lcd.print("%");
}

What happens when you swap the sensors?

Why all the global variables?

Please remember to use code tags when posting code.

Hmmm

Sorry im new to this forum please bear with me

I swapped sensors and tried swapping pins still the same results

I suggest you ditch the LCD and concentrate on just using Serial prints to debug the sensors.

@dave30, your topic has been moved to a more suitable location on the forum.

Can you please read How to get the best out of this forum (again?) and apply the code tags as described.

1 Like
lcd.print(Serial.print(dht2.readHumidity()));

Oops.

Second time I've seen this one today!

(Must've been asleep earlier,sorry)

Hi,

To add code please click this link;

Can I suggest you read ALL your sensor values at the start of the loop and store them as variables.

Then use those variables in the rest of your code.

In other words take a snapshot of your inputs and use those.
This will save all the

dht2.readTemperature()

etc all over your code.

for example;

hum1 =
temp1 =
hum2 =
temp2=

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I will if I ever use this forum again. Thanks for correcting

Update: Both 2 sensors are working! It just lacks power because I used 5V supply. Thanks for your help guys. :slight_smile: