#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Define the number of sensors
#define NUM_SENSORS 5
// Define the pins where the sensors are connected
const int sensorPins[NUM_SENSORS] = {2, 3, 4, 5, 6};
// Initialize DHT sensors array for DHT22
DHT dhtSensors[NUM_SENSORS] = {
DHT(sensorPins[0], DHT22),
DHT(sensorPins[1], DHT22),
DHT(sensorPins[2], DHT22),
DHT(sensorPins[3], DHT22),
DHT(sensorPins[4], DHT22)
};
// Initialize the LCD, address 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousMillis = 0;
const long interval = 2000; // interval at which to read sensors and switch pages
int currentPage = 0;
void setup() {
// Initialize Serial
Serial.begin(9600);
// Initialize the LCD with the correct parameters
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Initializing...");
// Initialize all DHT sensors
for (int i = 0; i < NUM_SENSORS; i++) {
dhtSensors[i].begin();
}
delay(2000); // Allow sensors some time to stabilize
lcd.clear();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Clear the LCD
lcd.clear();
// Display data for the current page
displayData(currentPage);
// Move to the next page
currentPage = (currentPage + 1) % ((NUM_SENSORS + 1) / 2); // Adjusted for displaying two sensors per page
}
}
void displayData(int page) {
int sensorIndex1 = page * 2;
int sensorIndex2 = sensorIndex1 + 1;
// Buffers for formatted temperature strings
char tempStr1[6]; // "XX.X"
char tempStr2[6]; // "XX.X"
// Read and display data from the first sensor on the current page
if (sensorIndex1 < NUM_SENSORS) {
float temp1 = dhtSensors[sensorIndex1].readTemperature();
float hum1 = dhtSensors[sensorIndex1].readHumidity();
if (isnan(temp1) || isnan(hum1)) {
lcd.setCursor(0, 0);
lcd.print("T");
lcd.print(sensorIndex1 + 1);
lcd.print(": Error");
Serial.print("Sensor ");
Serial.print(sensorIndex1 + 1);
Serial.println(" - Error reading data");
} else {
dtostrf(temp1, 4, 1, tempStr1); // Format temperature to 1 decimal place
lcd.setCursor(0, 0);
lcd.print("T");
lcd.print(sensorIndex1 + 1);
lcd.print(":");
lcd.print(tempStr1);
lcd.print((char)223); // Degree symbol
lcd.print("C H");
lcd.print(sensorIndex1 + 1);
lcd.print(":");
lcd.print(hum1);
lcd.print("%");
// Print to Serial
Serial.print("Sensor ");
Serial.print(sensorIndex1 + 1);
Serial.print(" - Humidity: ");
Serial.print(hum1);
Serial.print("%, Temp: ");
Serial.print(temp1);
Serial.println("°C");
}
}
// Read and display data from the second sensor on the current page
if (sensorIndex2 < NUM_SENSORS) {
float temp2 = dhtSensors[sensorIndex2].readTemperature();
float hum2 = dhtSensors[sensorIndex2].readHumidity();
// Handle NaN readings
if (isnan(temp2) || isnan(hum2)) {
lcd.setCursor(0, 1);
lcd.print("T");
lcd.print(sensorIndex2 + 1);
lcd.print(": Error");
Serial.print("Sensor ");
Serial.print(sensorIndex2 + 1);
Serial.println(" - Error reading data");
} else {
dtostrf(temp2, 4, 1, tempStr2); // Format temperature to 1 decimal place
lcd.setCursor(0, 1);
lcd.print("T");
lcd.print(sensorIndex2 + 1);
lcd.print(":");
lcd.print(tempStr2);
lcd.print((char)223); // Degree symbol
lcd.print("C H");
lcd.print(sensorIndex2 + 1);
lcd.print(":");
lcd.print(hum2);
lcd.print("%");
// Print to Serial
Serial.print("Sensor ");
Serial.print(sensorIndex2 + 1);
Serial.print(" - Humidity: ");
Serial.print(hum2);
Serial.print("%, Temp: ");
Serial.print(temp2);
Serial.println("°C");
}
}
}