#include "DHT.h"
#define trigPin 12 // Trig pin of the ultrasonic sensor connected to Arduino pin 12
#define echoPin 13 // Echo pin of the ultrasonic sensor connected to Arduino pin 13
#define LDR_PIN 34 // LDR connected to GPIO34 (Analog)
#define PIR_PIN 23 // PIR connected to GPIO23 (Digital)
#define LED_PIN 22 // LED 1 connected to GPIO22 (Digital)
#define TEMP_LED_PIN 21 // LED 2 connected to GPIO21 (Digital)
#define COLD_LED_PIN 19 // LED 3 connected to GPIO19 (Digital)
#define DHT_PIN 4 // DHT22 sensor connected to GPIO4
#define LED4 16 // LED 4 connected to Arduino pin 11
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Set trigPin as an output
pinMode(echoPin, INPUT); // Set echoPin as an input
pinMode(PIR_PIN, INPUT); // Set PIR pin as input
pinMode(LED_PIN, OUTPUT); // Set LED 1 pin as output
pinMode(TEMP_LED_PIN, OUTPUT); // Set LED 2 pin as output
pinMode(COLD_LED_PIN, OUTPUT); // Set LED 3 pin as output
pinMode(LED4, OUTPUT);
dht.begin(); // Initialize DHT sensor
digitalWrite(LED_PIN, LOW); // Turn OFF LED 1
digitalWrite(TEMP_LED_PIN, LOW); // Turn OFF LED 2
digitalWrite(COLD_LED_PIN, LOW); // Turn OFF LED 3
digitalWrite(LED4, LOW);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Clear the trigger
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(trigPin, HIGH); // Set the trigger high
delayMicroseconds(10); // Keep it high for 10 microseconds
digitalWrite(trigPin, LOW); // Set the trigger low
// Read the echo pin
duration = pulseIn(echoPin, HIGH); // Read the incoming pulse
distance = (duration / 2) * 0.0343; // Calculate the distance in cm, speed of sound =
int ldrValue = analogRead(LDR_PIN); // Read LDR value
int pirValue = digitalRead(PIR_PIN); // Read PIR value
float temperature = dht.readTemperature(); // Read temperature from DHT22
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(" | PIR Value: ");
Serial.print(pirValue);
Serial.print(" | Temperature: ");
Serial.println(temperature);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check the conditions for LED 1
if (pirValue == HIGH && ldrValue < 200) {
digitalWrite(LED_PIN, HIGH); // Turn ON LED 1
} else {
digitalWrite(LED_PIN, LOW); // Turn OFF LED 1
}
// Check the temperature condition for LED 2
if (pirValue == HIGH && temperature >= 30.0) {
digitalWrite(TEMP_LED_PIN, HIGH); // Turn ON LED 2
} else {
digitalWrite(TEMP_LED_PIN, LOW); // Turn OFF LED 2
}
// Check conditions for LED 3
if (pirValue == HIGH && temperature < 20.0) {
digitalWrite(COLD_LED_PIN, HIGH); // Turn ON LED 3
} else {
digitalWrite(COLD_LED_PIN, LOW); // Turn OFF LED 3
}
// Control LEDs based on distance
if (distance < 200) {
digitalWrite(LED4, HIGH); // Turn on the 4th LED if distance is less than 200 cm
} else {
digitalWrite(LED4, LOW); // Turn off the 4th LED if distance is 200 cm or more
}
delay(100); // Delay for stability and to prevent excessive readings
}