// Include necessary libraries
#include <Arduino.h>
// Define pins
#define PIR_SENSOR_PIN 14 // GPIO pin for the PIR motion sensor
#define SMOKE_SENSOR_PIN 34 // GPIO pin for the smoke sensor (analog)
#define LIGHT_SENSOR_PIN 35 // GPIO pin for the light sensor (analog)
#define LED_PIN 2 // GPIO pin for the LED
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Set up pin modes
pinMode(PIR_SENSOR_PIN, INPUT);
pinMode(SMOKE_SENSOR_PIN, INPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
// Initial LED state
digitalWrite(LED_PIN, LOW);
Serial.println("Smart Home System Initialized");
}
void loop() {
// Read the PIR motion sensor state
int motionDetected = digitalRead(PIR_SENSOR_PIN);
// Read the smoke sensor value (analog)
int smokeLevel = analogRead(SMOKE_SENSOR_PIN);
// Read the light sensor value (analog)
int lightLevel = analogRead(LIGHT_SENSOR_PIN);
// Print sensor values to the Serial Monitor
Serial.print("Motion Detected: ");
Serial.println(motionDetected);
Serial.print("Smoke Level: ");
Serial.println(smokeLevel);
Serial.print("Light Level: ");
Serial.println(lightLevel);
// Motion sensor logic
if (motionDetected == HIGH) {
Serial.println("Motion detected! Turning on LED.");
digitalWrite(LED_PIN, HIGH);
delay(1000); // Keep LED on for 1 second
} else {
digitalWrite(LED_PIN, LOW);
}
// Smoke sensor logic (threshold value may vary depending on the sensor)
if (smokeLevel > 300) { // Example threshold for smoke detection
Serial.println("Smoke detected! Blinking LED.");
for (int i = 0; i < 3; i++) { // Blink LED 3 times
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
}
}
// Light sensor logic (threshold value may vary depending on the environment)
if (lightLevel < 500) { // Example threshold for low light detection
Serial.println("Low light detected! Turning on LED.");
digitalWrite(LED_PIN, HIGH);
} else {
Serial.println("Sufficient light detected. Turning off LED.");
digitalWrite(LED_PIN, LOW);
}
delay(500); // Small delay for sensor stabilization
}