#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN 11 // DHT sensor pin
#define DHTTYPE DHT22 // DHT type (DHT22)
DHT dht(DHTPIN, DHTTYPE);
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int ledPin = 13; // LED pin
int buzzerPin = 10; // Buzzer pin
int inputPin = 8; // PIR sensor pin
int pirState = LOW; // We start, assuming no motion detected
int val = 0; // Variable for PIR sensor value
float t; // Temperature variable
float h; // Humidity variable
void setup() {
Serial.begin(9600);
dht.begin();
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(" Welcome to ");
lcd.setCursor(0, 1);
lcd.print(" Smart Temp Sens");
delay(2000);
lcd.clear();
pinMode(ledPin, OUTPUT); // Declare LED as output
pinMode(buzzerPin, OUTPUT); // Declare buzzer as output
pinMode(inputPin, INPUT); // Declare PIR sensor as input
}
void loop() {
val = digitalRead(inputPin); // Read PIR sensor input
if (val == HIGH) { // Check if the input is HIGH
digitalWrite(ledPin, HIGH); // Turn LED ON
digitalWrite(buzzerPin, HIGH); // Turn buzzer ON
delay(1000); // Buzzer sound duration
digitalWrite(buzzerPin, LOW); // Turn buzzer OFF
if (pirState == LOW) {
// We have just turned on
lcd.setCursor(0, 0);
lcd.print(" Motion detected!");
lcd.setCursor(0, 1);
lcd.print(" Temp sens active.");
Serial.println("Motion detected! Temp sensor active.");
delay(3000);
lcd.clear();
lcd.setCursor(0,3);
lcd.print(" Temp sensor active.");
temp_sensor(); // Call the temperature and humidity sensor function
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
if (pirState == HIGH) {
// We have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void temp_sensor() {
h = dht.readHumidity();
t = dht.readTemperature();
// Display temperature in Celsius
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print((char)223); // Degree symbol
lcd.print("C");
// Display humidity
lcd.setCursor(0, 1);
lcd.print("Humid: ");
lcd.print(h);
lcd.print("% ");
delay(5000);
// Display temperature in Fahrenheit
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(dht.convertCtoF(t));
lcd.print(" ");
lcd.print((char)223); // Degree symbol
lcd.print("F");
delay(5000);
}