// #include <Wire.h>
// #include <Adafruit_MPU6050.h>
// #include <Adafruit_Sensor.h>
// #include <WiFi.h> // To add Wi-Fi functionality later if needed
// // Pin Definitions
// #define PIR_PIN 27 // GPIO connected to the PIR sensor
// // Variables for PIR and MPU-6050
// Adafruit_MPU6050 mpu;
// bool pirMotionDetected = false;
// // Function Declarations
// void setupPIR();
// void setupMPU6050();
// void checkPIRSensor();
// void checkMPU6050();
// void sendAlert(const char* message);
// void setup() {
// Serial.begin(115200);
// // Initialize Sensors
// setupPIR();
// setupMPU6050();
// Serial.println("System Initialized. Monitoring for movement...");
// }
// void loop() {
// checkPIRSensor(); // Check for motion via PIR
// checkMPU6050(); // Check for tilt/movement via MPU-6050
// delay(500); // Delay for stability; adjust as necessary
// }
// // Function to initialize the PIR sensor
// void setupPIR() {
// pinMode(PIR_PIN, INPUT);
// Serial.println("PIR sensor initialized.");
// }
// // Function to initialize the MPU-6050 sensor
// void setupMPU6050() {
// if (!mpu.begin()) {
// Serial.println("Failed to find MPU6050 chip.");
// while (1) delay(10); // Halt if MPU fails
// }
// Serial.println("MPU6050 initialized successfully.");
// // Set desired ranges
// mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
// mpu.setGyroRange(MPU6050_RANGE_250_DEG);
// }
// // Function to check for PIR sensor motion
// void checkPIRSensor() {
// int pirStatus = digitalRead(PIR_PIN);
// if (pirStatus == HIGH && !pirMotionDetected) {
// pirMotionDetected = true;
// Serial.println("PIR Motion detected!");
// sendAlert("Motion detected by PIR sensor.");
// } else if (pirStatus == LOW && pirMotionDetected) {
// pirMotionDetected = false;
// Serial.println("No motion detected by PIR sensor.");
// }
// }
// // Function to check for MPU-6050 movement
// void checkMPU6050() {
// sensors_event_t a, g, temp;
// mpu.getEvent(&a, &g, &temp);
// // Define thresholds for motion/tilt detection
// float threshold = 3.0; // Adjust based on sensitivity needs
// if (abs(a.acceleration.x) > threshold ||
// abs(a.acceleration.y) > threshold ||
// abs(a.acceleration.z) > threshold) {
// Serial.println("Significant movement detected by MPU6050!");
// sendAlert("Significant movement detected by MPU6050 sensor.");
// }
// }
// // Function to handle sending alerts (replace with actual Wi-Fi or Cloud code)
// void sendAlert(const char* message) {
// // Placeholder for sending alert (e.g., through Wi-Fi or GSM)
// Serial.print("Alert: ");
// Serial.println(message);
// // You can add Wi-Fi or Cloud notification code here
// // Example: Send data to Firebase or Blynk
// }
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <WiFi.h> // To add Wi-Fi functionality later if needed
// Pin Definitions
#define PIR_PIN 27 // GPIO connected to the PIR sensor
#define TRIG_PIN 12 // GPIO connected to the HC-SR04 Trigger
#define ECHO_PIN 13 // GPIO connected to the HC-SR04 Echo
// Variables for PIR, MPU-6050, and Ultrasonic sensor
Adafruit_MPU6050 mpu;
bool pirMotionDetected = false;
// Function Declarations
void setupPIR();
void setupMPU6050();
void setupUltrasonic();
void checkPIRSensor();
void checkMPU6050();
void checkUltrasonic();
void sendAlert(const char* message);
// Variables for Ultrasonic sensor
long duration;
int distance;
void setup() {
Serial.begin(115200);
// Initialize Sensors
setupPIR();
setupMPU6050();
setupUltrasonic();
Serial.println("System Initialized. Monitoring for movement...");
}
void loop() {
checkPIRSensor(); // Check for motion via PIR
checkMPU6050(); // Check for tilt/movement via MPU-6050
checkUltrasonic(); // Check for distance via HC-SR04 Ultrasonic sensor
delay(500); // Delay for stability; adjust as necessary
}
// Function to initialize the PIR sensor
void setupPIR() {
pinMode(PIR_PIN, INPUT);
Serial.println("PIR sensor initialized.");
}
// Function to initialize the MPU-6050 sensor
void setupMPU6050() {
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip.");
while (1) delay(10); // Halt if MPU fails
}
Serial.println("MPU6050 initialized successfully.");
// Set desired ranges
mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
mpu.setGyroRange(MPU6050_RANGE_250_DEG);
}
// Function to initialize the HC-SR04 ultrasonic sensor
void setupUltrasonic() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.println("HC-SR04 Ultrasonic sensor initialized.");
}
// Function to check for PIR sensor motion
void checkPIRSensor() {
int pirStatus = digitalRead(PIR_PIN);
if (pirStatus == HIGH && !pirMotionDetected) {
pirMotionDetected = true;
Serial.println("PIR Motion detected!");
sendAlert("Motion detected by PIR sensor.");
} else if (pirStatus == LOW && pirMotionDetected) {
pirMotionDetected = false;
Serial.println("No motion detected by PIR sensor.");
}
}
// Function to check for MPU-6050 movement
void checkMPU6050() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Define thresholds for motion/tilt detection
float threshold = 3.0; // Adjust based on sensitivity needs
if (abs(a.acceleration.x) > threshold ||
abs(a.acceleration.y) > threshold ||
abs(a.acceleration.z) > threshold) {
Serial.println("Significant movement detected by MPU6050!");
sendAlert("Significant movement detected by MPU6050 sensor.");
}
}
// Function to check for distance using the HC-SR04 ultrasonic sensor
void checkUltrasonic() {
// Send a pulse to trigger the ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the pulse duration from the Echo pin
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate distance in cm
distance = duration * 0.0344 / 2; // Speed of sound is 0.0344 cm/us
if (distance < 50) { // Example threshold for detecting proximity (less than 50 cm)
Serial.println("Object detected near vehicle!");
sendAlert("Object detected near vehicle by Ultrasonic sensor.");
}
}
// Function to handle sending alerts (replace with actual Wi-Fi or Cloud code)
void sendAlert(const char* message) {
// Placeholder for sending alert (e.g., through Wi-Fi or GSM)
Serial.print("Alert: ");
Serial.println(message);
// You can add Wi-Fi or Cloud notification code here
// Example: Send data to Firebase or Blynk
}