// Simulation of a smart home automation system on Wokwi
// This code simulates the behavior of motion sensors, light sensors, and a lamp
// Pin Definitions
#define MOTION_SENSOR_PIN 22
#define LIGHT_SENSOR_PIN 34
#define LAMP_PIN 26
// Variables to hold sensor readings
int motionSensorValue = 0;
int lightSensorValue = 0;
void setup() {
// Set pin modes
pinMode(MOTION_SENSOR_PIN, INPUT);
pinMode(LIGHT_SENSOR_PIN, INPUT);
pinMode(LAMP_PIN, OUTPUT);
// Initialize Serial Monitor
Serial.begin(115200);
Serial.println("Smart Home Automation System Initialized");
}
void loop() {
// Read sensor values
motionSensorValue = digitalRead(MOTION_SENSOR_PIN);
lightSensorValue = analogRead(LIGHT_SENSOR_PIN);
// Print sensor readings to the Serial Monitor
Serial.print("Motion Sensor: ");
Serial.print(motionSensorValue == HIGH ? "Detected" : "Not Detected");
Serial.print(" | Light Sensor Value: ");
Serial.println(lightSensorValue);
// Check if motion is detected
if (motionSensorValue == HIGH) {
// Turn on the lamp if the light level is low
if (lightSensorValue < 500) {
digitalWrite(LAMP_PIN, HIGH);
Serial.println("Condition: Motion detected, light level is low. Lamp ON");
} else {
digitalWrite(LAMP_PIN, LOW);
Serial.println("Condition: Motion detected, light level is sufficient. Lamp OFF");
}
} else {
// Turn off the lamp when no motion is detected
digitalWrite(LAMP_PIN, LOW);
Serial.println("Condition: No motion detected. Lamp OFF");
}
// Delay for stability
delay(100);
}