#include <CD74HC4067.h>
#include <DHT.h>
#define DHTTYPE DHT22
// Define pins for multiplexer
const int S0 = 12;
const int S1 = 13;
const int S2 = 14;
const int S3 = 15;
const int SIG_PIN = 4; // Common signal pin connected to ESP32
// Multiplexer object
CD74HC4067 mux(S0, S1, S2, S3);
// DHT object (use same pin for all, as the multiplexer will switch the signal)
DHT dht(SIG_PIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
for (int channel = 0; channel < 3; channel++) { // Adjust the loop to the number of sensors
mux.channel(channel); // Select the correct channel
delay(200); // Small delay to allow switching
// Reading DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.print("Failed to read from DHT sensor on channel ");
Serial.println(channel);
} else {
Serial.print("Sensor ");
Serial.print(channel);
Serial.print(" - Temp: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
delay(2000); // Wait between readings
}
}