Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Security System with Wi-Fi Connectivity and Automated Lighting

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Security System with Wi-Fi Connectivity and Automated Lighting

Circuit Documentation

Summary of the Circuit

This circuit is designed to control a servo and a relay module based on the input from a PIR (Passive Infrared) sensor. The Arduino 101 microcontroller serves as the central processing unit, reading the PIR sensor's output to detect motion and then activating the relay and servo accordingly. Additionally, the circuit includes an ESP8266 module for potential WiFi capabilities, allowing for remote commands to be sent to the Arduino. An LED is also connected to the relay module to indicate the relay's state.

Component List

LED: Two Pin (red)

Servo

RELAY MODULE

Arduino 101

PIR sensor

ESP8266

Wiring Details

LED: Two Pin (red)

Servo

RELAY MODULE

Arduino 101

PIR sensor

ESP8266

Documented Code

Arduino 101 Code

#include <Servo.h>

Servo myServo;
const int pirPin = 2;  // Pin for PIR sensor
const int relayPin = 3; // Pin for relay
int pirState = LOW;     // Initial state for PIR
int val = 0;            // Variable to store the sensor value

void setup() {
  myServo.attach(9);          // Connect servo to pin 9
  pinMode(pirPin, INPUT);     // Set PIR pin as input
  pinMode(relayPin, OUTPUT);  // Set relay pin as output
  Serial.begin(115200);       // Start serial communication at 115200 baud rate
}

void loop() {
  val = digitalRead(pirPin); // Read PIR status
  if (val == HIGH) {         // If motion detected
    if (pirState == LOW) {
      Serial.println("Motion detected!"); // Notify via Serial
      digitalWrite(relayPin, HIGH); // Turn on the light
      myServo.write(90);             // Open the door (adjust angle as needed)
      delay(2000);                   // Wait for a few seconds
      myServo.write(0);              // Close the door
      pirState = HIGH;               // Set PIR status
    }
  } else {
    digitalWrite(relayPin, LOW);    // Turn off the light if no motion is detected
    pirState = LOW;                  // Reset PIR status
  }

  // Check for commands from ESP8266 via Serial
  if (Serial.available()) {
    String command = Serial.readStringUntil('\n'); // Read command from ESP8266
    if (command == "ON") {
      Serial.println("Light turned on");
      digitalWrite(relayPin, HIGH); // Turn on the light
    } else if (command == "OFF") {
      Serial.println("Light turned off");
      digitalWrite(relayPin, LOW);  // Turn off the light
    }
  }
}

ESP8266 Code

#include <ESP8266WiFi.h>

const char* ssid = "Your_SSID"; // Replace with your Wi-Fi SSID
const char* password = "Your_PASSWORD"; // Replace with your Wi-Fi password

void setup() {
  Serial.begin(115200); // Start serial communication
  WiFi.begin(ssid, password); // Connect to Wi-Fi
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  // Code to control devices or take input from applications
  // For example, sending commands to Arduino
  Serial.println("ON"); // Send command to turn on the light
  delay(5000); // Wait for 5 seconds
  Serial.println("OFF"); // Send command to turn off the light
  delay(5000); // Wait for 5 seconds
}

This concludes the documentation for the given circuit design and embedded code.