0% found this document useful (0 votes)
3 views

Problem 2

Uploaded by

manish74618877
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Problem 2

Uploaded by

manish74618877
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Problem 2:-

#include <DHT.h>

#define DHTPIN 2

#define PIRPIN 3

#define LIGHTPIN 4

#define FANPIN 5

#define BUZZERPIN 6

#define DHTTYPE DHT11

#define TEMP_THRESHOLD 30

DHT dht(DHTPIN, DHTTYPE);

void setup() {

// Initialize pins

pinMode(PIRPIN, INPUT);

pinMode(LIGHTPIN, OUTPUT);

pinMode(FANPIN, OUTPUT);

pinMode(BUZZERPIN, OUTPUT);

Serial.begin(9600);

dht.begin();

void loop() {

// Read PIR sensor to check for motion

int motionState = digitalRead(PIRPIN);

// Read temperature from DHT11 sensor

float temperature = dht.readTemperature();


if (isnan(temperature)) {

Serial.println("Failed to read temperature!");

return;

Serial.print("Temperature: ");

Serial.print(temperature);

Serial.println(" °C");

if (motionState == HIGH)

digitalWrite(LIGHTPIN, HIGH);

Serial.println("Motion detected! Light ON.");

} else {

digitalWrite(LIGHTPIN, LOW);

Serial.println("No motion detected. Light OFF.");

if (temperature > 25) {

digitalWrite(FANPIN, HIGH);

Serial.println("Fan ON.");

} else {

digitalWrite(FANPIN, LOW);

Serial.println("Fan OFF.");

if (motionState == HIGH && temperature > TEMP_THRESHOLD) {

digitalWrite(BUZZERPIN, HIGH);

Serial.println("ALARM! High temperature with motion detected!");

} else {

digitalWrite(BUZZERPIN, LOW);

delay(2000);

You might also like