This circuit integrates multiple sensors and a Bluetooth module with an Arduino UNO to monitor environmental conditions and send alerts. The sensors include a PIR motion sensor, a water level float switch sensor, an MPU-6050 accelerometer and gyroscope, and a DS18B20 temperature sensor. The HC-05 Bluetooth module is used for wireless communication. The Arduino UNO processes the sensor data and sends alerts via Bluetooth when certain conditions are met.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <MPU6050.h>
#define PIR_PIN 2
#define FLOAT_PIN 4
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
MPU6050 mpu;
bool motionDetected = false;
unsigned long lastMotionTime = 0;
const unsigned long timeoutPeriod = 5000; // 5 seconds
void setup() {
Serial.begin(9600); // Initialize serial communication for USB
Serial1.begin(9600); // Initialize serial communication for Bluetooth (HC-05)
sensors.begin();
mpu.initialize();
pinMode(PIR_PIN, INPUT);
pinMode(FLOAT_PIN, INPUT);
}
void loop() {
// Read the PIR sensor
motionDetected = digitalRead(PIR_PIN);
if (motionDetected) {
lastMotionTime = millis();
} else if (millis() - lastMotionTime > timeoutPeriod) {
sendAlert();
}
// Check water level
if (digitalRead(FLOAT_PIN) == HIGH) {
Serial.println("Water level is high!");
Serial1.println("Water level is high!"); // Send to Bluetooth
sendAlert();
}
// Read temperature
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(temperature);
Serial1.print("Temperature: "); // Send to Bluetooth
Serial1.println(temperature);
// Optional: Read MPU6050 data for more functionality
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
Serial.print("Accelerometer: ");
Serial.print("ax: "); Serial.print(ax);
Serial.print(" ay: "); Serial.print(ay);
Serial.print(" az: "); Serial.println(az);
Serial1.print("Accelerometer: "); // Send to Bluetooth
Serial1.print("ax: "); Serial1.print(ax);
Serial1.print(" ay: "); Serial1.print(ay);
Serial1.print(" az: "); Serial1.println(az);
delay(1000);
}
void sendAlert() {
Serial.println("ALERT: Possible drowning detected!");
Serial1.println("ALERT: Possible drowning detected!"); // Send to Bluetooth
Serial.println("No motion detected for 5 seconds!");
Serial1.println("No motion detected for 5 seconds!"); // Send to Bluetooth
}
This code initializes the sensors and the Bluetooth module, reads data from the sensors, and sends alerts via Bluetooth when certain conditions are met. The sendAlert
function is called when no motion is detected for 5 seconds or when the water level is high. The temperature and accelerometer data are also read and sent to the Bluetooth module.