#include <DHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Pin Definitions
#define LED1_PIN 13 // You can use the built-in LED for the first LED
#define LED2_PIN 5 // Replace with the appropriate pin for LED2
#define LED3_PIN 4 // Replace with the appropriate pin for LED3
#define LED4_PIN 15 // Replace with the appropriate pin for LED4
#define LED5_PIN 2 // Replace with the appropriate pin for LED5
#define LED6_PIN 14 // Replace with the appropriate pin for LED6
#define BUZZER_PIN 12 // Replace with the appropriate pin for the buzzer
#define DHT1_PIN 18 // Replace with the appropriate pin for the DHT22
// Create instances of DHT sensor and Adafruit BME280 sensor
DHT dht(DHT1_PIN, DHT22);
Adafruit_BME280 bme;
void setup() {
// Initialize LEDs as outputs
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(LED4_PIN, OUTPUT);
pinMode(LED5_PIN, OUTPUT);
pinMode(LED6_PIN, OUTPUT);
// Initialize the buzzer
pinMode(BUZZER_PIN, OUTPUT);
// Initialize the DHT sensor
dht.begin();
// Initialize the BME280 sensor
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
void loop() {
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Read temperature and humidity from BME280 sensor
float tempBME = bme.readTemperature();
float humidityBME = bme.readHumidity();
// Check if any of the readings are NaN (not a number)
if (isnan(temperature) || isnan(humidity) || isnan(tempBME) || isnan(humidityBME)) {
Serial.println("Failed to read from DHT22 or BME280 sensor!");
} else {
// Control LEDs based on sensor readings
if (temperature > 30.0) {
digitalWrite(LED1_PIN, HIGH);
} else {
digitalWrite(LED1_PIN, LOW);
}
if (humidity > 60.0) {
digitalWrite(LED2_PIN, HIGH);
} else {
digitalWrite(LED2_PIN, LOW);
}
if (tempBME > 25.0) {
digitalWrite(LED3_PIN, HIGH);
} else {
digitalWrite(LED3_PIN, LOW);
}
// Control buzzer based on sensor readings
if (humidityBME < 40.0) {
tone(BUZZER_PIN, 1000); // Play a sound
} else {
noTone(BUZZER_PIN); // Stop the sound
}
// Control LED4, LED5, and LED6 based on your requirements
// Print sensor readings to serial monitor
Serial.print("DHT Temperature: ");
Serial.println(temperature);
Serial.print("DHT Humidity: ");
Serial.println(humidity);
Serial.print("BME Temperature: ");
Serial.println(tempBME);
Serial.print("BME Humidity: ");
Serial.println(humidityBME);
}
delay(1000); // Delay for 1 second
}