The circuit in question is designed to interface with various sensors and a Bluetooth module to monitor environmental conditions such as motion, water level, and temperature. It also includes an accelerometer for additional functionality. The central processing unit of the circuit is an Arduino UNO, which is programmed to read sensor data, process it, and communicate with a Bluetooth module (HC-05) for potential remote monitoring or alerting.
#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 is designed to run on the Arduino UNO microcontroller. It initializes the connected sensors and sets up serial communication for both USB and Bluetooth. The loop
function continuously checks for motion, water level, and temperature, and sends alerts through both USB and Bluetooth if certain conditions are met. The MPU6050 accelerometer data is also read and outputted, which could be used for additional features such as detecting orientation or movement.