#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <DHT_U.h>
#include <SPI.h>
#include <SD.h>
// Define the pins for the sensors
#define DHTTYPE DHT22
#define DHT1_PIN 21
#define DHT2_PIN 14
#define DHT3_PIN 13
#define SD_CS_PIN 5
#define FILENAME "data.txt"
DHT dht2(DHT2_PIN, DHTTYPE);
DHT dht3(DHT3_PIN, DHTTYPE);
DHT dht1(DHT1_PIN, DHTTYPE);
File dataFile;
// Initialize the LCD screen
LiquidCrystal_I2C lcd(0x27, 20, 4); // Change the I2C address and size of the LCD if necessary
void setup() {
// Start the serial communication
Serial.begin(9600);
dht1.begin();
dht2.begin();
dht3.begin();
// Initialize the LCD screen
lcd.init();
lcd.backlight();
// Print the sensor headers on the LCD screen
lcd.setCursor(0, 0);
lcd.print("DHT 1: ");
lcd.setCursor(0, 1);
lcd.print("DHT 2: ");
lcd.setCursor(0, 2);
lcd.print("DHT 3: ");
SPI.begin();
pinMode(SD_CS_PIN, OUTPUT);
digitalWrite(SD_CS_PIN,LOW);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("Card failed, or not present");
return;
}
else{
Serial.println("card initialised");
}
dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Timestamp,Temperature(*C),Humidity(%)");
dataFile.close();
}
else {
Serial.println("error opening file");
}
}
void loop() {
// Read the sensor values
float DHT1_TEMP = dht1.readTemperature(DHT1_PIN);
float DHT2_TEMP = dht2.readTemperature(DHT2_PIN);
float DHT3_TEMP = dht3.readTemperature(DHT3_PIN);
float DHT1_HUM = dht1.readHumidity(DHT1_PIN);
float DHT2_HUM = dht2.readHumidity(DHT2_PIN);
float DHT3_HUM = dht3.readHumidity(DHT3_PIN);
// Print the sensor values on the serial monitor
Serial.print("\nSensor 1: ");
Serial.print("\ttemperature: ");
Serial.print(DHT1_TEMP);
Serial.print("\thumidity: ");
Serial.print(DHT1_HUM);
Serial.print("\nSensor 2: ");
Serial.print("\ttemperature: ");
Serial.print(DHT2_TEMP);
Serial.print("\thumidity: ");
Serial.print(DHT2_HUM);
Serial.print("\nSensor 3: ");
Serial.print("\ttemperature: ");
Serial.print(DHT3_TEMP);
Serial.print("\thumidity: ");
Serial.print(DHT3_HUM);
// Print the sensor values on the LCD screen
lcd.setCursor(7, 0);
lcd.print(DHT1_TEMP);
lcd.print("c");
lcd.print(DHT1_HUM);
lcd.print("%");
lcd.setCursor(7, 1);
lcd.print(DHT2_TEMP);
lcd.print("c");
lcd.print(DHT2_HUM);
lcd.print("%");
lcd.setCursor(7, 2);
lcd.print(DHT3_TEMP);
lcd.print("c");
lcd.print(DHT3_HUM);
lcd.print("%");
// Wait for 1 second before reading the sensors again
delay(10000);
}