/*
HOME Automation System:-- When the person enters in the house then PIR module detects then it
activates all other sensors. After that when the temperature is above than 30-degree celcius then
it activates the fan(as motor is not present in WOKWI. So, i had used the servo motor). And when
the luminus intensity is less than 350-lux then Led will glow automatically. And there was a
display which display all the data there for the user. And the person goes out of the house then
it will automatically shutdown all the sensor to save the electricity.
*/
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Servo.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int PIR_PIN = 1;
const int LDR_PIN = A0;
const int LED_PIN = 3;
const int SERVO_PIN = 5;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(PIR_PIN, INPUT);
pinMode(LDR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
myservo.attach(SERVO_PIN);
}
void loop() {
int pirValue = digitalRead(PIR_PIN);
int ldrValue = analogRead(LDR_PIN);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("PIR: ");
lcd.print(pirValue);
lcd.setCursor(0, 1);
lcd.print("LDR: ");
lcd.print(ldrValue);
if (ldrValue > 350) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
if (temperature >= 30.0) {
for (int pos = 0; pos <= 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos -= 1) {
myservo.write(pos);
delay(15);
}
} else {
myservo.write(90);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
delay(2000);
}
///// Code for proper funcutionation using PIR Sensor//////
// #include <LiquidCrystal_I2C.h>
// #include <DHT.h>
// #include <Servo.h>
// #define DHTPIN 2
// #define DHTTYPE DHT22
// DHT dht(DHTPIN, DHTTYPE);
// const int PIR_PIN = 1;
// const int LDR_PIN = A0;
// const int LED_PIN = 3;
// const int SERVO_PIN = 5;
// LiquidCrystal_I2C lcd(0x27, 16, 2);
// Servo myservo;
// void setup() {
// Serial.begin(9600);
// dht.begin();
// pinMode(PIR_PIN, INPUT);
// pinMode(LDR_PIN, INPUT);
// pinMode(LED_PIN, OUTPUT);
// lcd.init();
// lcd.backlight();
// lcd.clear();
// myservo.attach(SERVO_PIN);
// }
// void loop() {
// int pirValue = digitalRead(PIR_PIN);
// if (pirValue % 2 != 0) {
// int ldrValue = analogRead(LDR_PIN);
// float humidity = dht.readHumidity();
// float temperature = dht.readTemperature();
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("PIR: ");
// lcd.print(pirValue);
// lcd.setCursor(0, 1);
// lcd.print("LDR: ");
// lcd.print(ldrValue);
// if (ldrValue > 350) {
// digitalWrite(LED_PIN, HIGH);
// } else {
// digitalWrite(LED_PIN, LOW);
// }
// if (temperature >= 30.0) {
// for (int pos = 0; pos <= 180; pos += 1) {
// myservo.write(pos);
// delay(15);
// }
// for (int pos = 180; pos >= 0; pos -= 1) {
// myservo.write(pos);
// delay(15);
// }
// } else {
// myservo.write(90);
// }
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Humidity: ");
// lcd.print(humidity);
// lcd.print(" %");
// lcd.setCursor(0, 1);
// lcd.print("Temp: ");
// lcd.print(temperature);
// lcd.print(" C");
// } else {
// // If even or 0, deactivate all sensors
// digitalWrite(LED_PIN, LOW); // Turn off LED
// myservo.write(90); // Set servo to neutral position
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.print("Sensors OFF");
// }
// delay(2000);
// }