Hello Everyone,
I have been struggling & bellow is my last effort. Aim is to display 8 numbers DHT22 Temp & Humidity on I2C LCD. Wokwi simulator is used to know whether my code is working correctly but all in vain as Display does not show value.
Note: in future I will use for 16 DHT, arduino will not have enough pins as 16 Pins for dht & other components like push buttons,LED hence Multiplexer is must
Could you please help me?
Thanks
Rezox
Bellow is wokwi link with code & connetion diagram
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <CD74HC4067.h>
// Pins for CD74HC4067 control
#define MUX_SIG 7
#define MUX_S0 8
#define MUX_S1 9
#define MUX_S2 10
#define MUX_S3 11
// LCD address and size
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// DHT sensor type
#define DHT_TYPE DHT22
// Number of DHT sensors
#define NUM_SENSORS 8
// DHT sensor pins connected to the multiplexer channels
int dhtPins[NUM_SENSORS] = {A0, A1, A2, A3, A4, A5, A6, A7};
// Create an array of DHT objects
DHT dhtSensors[NUM_SENSORS] = {
DHT(dhtPins[0], DHT_TYPE),
DHT(dhtPins[1], DHT_TYPE),
DHT(dhtPins[2], DHT_TYPE),
DHT(dhtPins[3], DHT_TYPE),
DHT(dhtPins[4], DHT_TYPE),
DHT(dhtPins[5], DHT_TYPE),
DHT(dhtPins[6], DHT_TYPE),
DHT(dhtPins[7], DHT_TYPE)
};
// Create LiquidCrystal_I2C instance
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
void setup() {
// Initialize CD74HC4067 control pins
pinMode(MUX_SIG, OUTPUT);
pinMode(MUX_S0, OUTPUT);
pinMode(MUX_S1, OUTPUT);
pinMode(MUX_S2, OUTPUT);
pinMode(MUX_S3, OUTPUT);
// Start I2C communication
Wire.begin();
// Initialize LCD
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.setBacklight(LOW); // Adjust backlight as needed
// Initialize DHT sensors
for (int i = 0; i < NUM_SENSORS; i++) {
dhtSensors[i].begin();
}
}
void loop() {
// Loop through all sensors
for (int i = 0; i < NUM_SENSORS; i++) {
// Select the MUX channel
selectMuxChannel(i);
// Read temperature and humidity from the sensor
float temperature = dhtSensors[i].readTemperature();
float humidity = dhtSensors[i].readHumidity();
// Display temperature and humidity on LCD
displayData(i + 1, temperature, humidity);
// Delay before moving to the next sensor
delay(500);
}
}
void selectMuxChannel(int channel) {
digitalWrite(MUX_S0, channel & 0x01);
digitalWrite(MUX_S1, (channel >> 1) & 0x01);
digitalWrite(MUX_S2, (channel >> 2) & 0x01);
digitalWrite(MUX_S3, (channel >> 3) & 0x01);
digitalWrite(MUX_SIG, HIGH);
delayMicroseconds(100);
digitalWrite(MUX_SIG, LOW);
}
void displayData(int sensorIndex, float temperature, float humidity) {
lcd.setCursor(0, sensorIndex - 1);
lcd.print("Sensor ");
lcd.print(sensorIndex);
lcd.print(": T=");
lcd.print(temperature);
lcd.print("C, H=");
lcd.print(humidity);
lcd.print("%");
}