Team member(s): divya gogia
Modified by divya gogia on November 30, 2023
The project aims to create an intelligent home security system that not only detects potential threats using an Arduino, an ultrasonic sensor, and a gas/smoke sensor but also responds to those threats using a buzzer for audible alarms and a servo motor for physical actions. The system will monitor for both intruders and fire hazards, providing real-time alerts to homeowners and automatically triggering responses such as camera activation or door control.
COMPONENTS
WORKFLOW
ASSEMBLY
CODE
// Define pin numbers
const int fireSensorPin = A0; // Analog pin for the fire sensor
const int ultrasonicTrigPin = 9; // Digital pin for the ultrasonic sensor trigger
const int ultrasonicEchoPin = 10; // Digital pin for the ultrasonic sensor echo
const int servoPin = 6; // Digital pin for the servo motor
const int buzzerPin = 8; // Digital pin for the buzzer
// Threshold values
const int fireThreshold = 500; // Adjust based on your sensor
const int distanceThreshold = 30; // Adjust based on your setup (distance in centimeters)
const int motorRunTime = 20000; // Motor run time in milliseconds (20 seconds)
Servo servo;
unsigned long alarmStartTime = 0;
void setup() {
Serial.begin(9600);
pinMode(fireSensorPin, INPUT);
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
pinMode(servoPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
servo.attach(servoPin);
}
void loop() {
int fireValue = analogRead(fireSensorPin);
int distance = getDistance();
if (fireValue > fireThreshold || distance < distanceThreshold) {
activateAlarm();
}
// Stop motor movement after 20 seconds
if (millis() – alarmStartTime > motorRunTime) {
stopAlarm();
}
}
void activateAlarm() {
Serial.println(“Security threat detected!”);
// Move the servo motor to a suitable angle (adjust as needed)
servo.write(90); // Assuming the valid range is 0 to 180 degrees
delay(500);
tone(buzzerPin, 1000); // Adjust frequency as needed
Serial.println(“Status 1”);
delay(5000); // Keep the alarm active for 5 seconds
// Reset the servo motor to its initial position
servo.write(0); // Assuming the valid range is 0 to 180 degrees
delay(500);
noTone(buzzerPin); // Turn off the buzzer
// Record the start time of the alarm
alarmStartTime = millis();
Serial.println(“Status 2”);
}
void stopAlarm() {
Serial.println(“Alarm stopped.”);
// Additional actions to stop the alarm if needed
}
int getDistance() {
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
return pulseIn(ultrasonicEchoPin, HIGH) * 0.034 / 2;
}