// Simulation of a smart home automation system on Wokwi
// This code simulates the behavior of motion sensors, light sensors, lamps, and a fan controlled by a relay
// Pin Definitions
#define MOTION_SENSOR_PIN 22
#define LIGHT_SENSOR_PIN 34
#define RELAY_PIN 18
#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(RELAY_PIN, OUTPUT);
  pinMode(LAMP_PIN, OUTPUT);
}
void loop() {
  // Read sensor values
  motionSensorValue = digitalRead(MOTION_SENSOR_PIN);
  lightSensorValue = analogRead(LIGHT_SENSOR_PIN);
  // Check if motion is detected
   //if (motionSensorValue == HIGH) {
    // Turn on the lamp
    digitalWrite(LAMP_PIN, HIGH);
    
    // Check if light level is low
   // if (lightSensorValue < 500) {
      // Turn on the fan (activate the relay)
      digitalWrite(RELAY_PIN, HIGH);
    // } else {
      // Turn off the fan (deactivate the relay)
     // digitalWrite(RELAY_PIN, LOW);
   // }
  //} else {
    // Turn off the lamp and the fan
    //digitalWrite(LAMP_PIN, LOW);
    //digitalWrite(RELAY_PIN, LOW);
  //}
  // Delay for stability
  delay(100);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4