#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#define DHTPIN 13 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // Type of DHT sensor (DHT22 in this case)
#define PPM_PIN 4 // Analog pin connected to the PPM sensor
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
LiquidCrystal_I2C lcd(0x27, 16, 2); // Create an LCD object
const int capteurLumierePin = 35; // Pin connected to the light sensor
const int ledRed = 12; // Pin connected to the red LED
const int ledGreen = 14; // Pin connected to the green LED
void setup() {
Serial.begin(115200); // Start serial communication
dht.begin(); // Initialize the DHT sensor
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight of the LCD
pinMode(ledRed, OUTPUT); // Set red LED pin as output
pinMode(ledGreen, OUTPUT); // Set green LED pin as output
pinMode(2, OUTPUT); // Set pin 2 as output
}
void loop() {
delay(2000); // Delay before each sensor reading
// Read temperature and humidity from DHT sensor
float temp = dht.readTemperature(); // Read temperature
float hum = dht.readHumidity(); // Read humidity
// Read PPM value from analog pin
int16_t ppmValue = analogRead(PPM_PIN); // Read analog value from PPM sensor
float voltage = ppmValue * (5.0 / 1023.0); // Convert analog value to voltage
float ppm = voltage * 1000; // Convert voltage to PPM assuming linear relationship
// Read light sensor value
int valeurLuminosite = analogRead(capteurLumierePin); // Read analog value from light sensor
// Control LEDs based on temperature and humidity readings
if (temp > 27 || hum > 70) {
// Blink the red LED in case of high temperature or humidity
for (int i = 0; i < 5; i++) { // Blink 5 times
digitalWrite(ledRed, HIGH); // Turn on the red LED
delay(100); // Wait for 100 milliseconds
digitalWrite(ledRed, LOW); // Turn off the red LED
delay(100); // Wait for 100 milliseconds
}
digitalWrite(ledGreen, LOW); // Turn off the green LED
Serial.println("Warning: High temperature or humidity detected!!!!!"); // Print warning message
} else {
// Blink the green LED if temperature and humidity are within normal range
for (int i = 0; i < 5; i++) { // Blink 5 times
digitalWrite(ledGreen, HIGH); // Turn on the green LED
delay(100); // Wait for 100 milliseconds
digitalWrite(ledGreen, LOW); // Turn off the green LED
delay(100); // Wait for 100 milliseconds
}
digitalWrite(ledRed, LOW); // Turn off the red LED
Serial.println("Temperature and humidity within normal range."); // Print normal range message
}
// Display sensor readings on LCD
displaySensorReadings(temp, hum);
// Display PPM value and light status on LCD with animation
displayPPMAndLight(ppm, valeurLuminosite);
delay(1500); // Adjust delay as needed
}
// Function to display temperature and humidity on LCD
void displaySensorReadings(float temp, float hum) {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print("Temp: "); // Display "Temp: "
lcd.print(temp); // Display temperature
lcd.print(" C"); // Display unit for temperature
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print("Humidity: "); // Display "Humidity: "
lcd.print(hum); // Display humidity
lcd.print(" %"); // Display unit for humidity
}
// Function to display PPM value and light status on LCD with animation
void displayPPMAndLight(float ppm, int valeurLuminosite) {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print("AIR QU: "); // Display "AIR QU: "
lcd.print(ppm); // Display PPM value
lcd.print(" PPM: "); // Display unit for PPM
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print("Light: "); // Display "Light: "
if (valeurLuminosite < 1000) {
lcd.print("Closed"); // If light is low, display "Closed"
} else {
lcd.print("Open"); // If light is high, display "Open"
}
}