Arduino Mega 2560-Based Obstacle and Water Detection System with Vibration and Buzzer Alerts
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This document provides a detailed overview of a circuit designed to detect obstacles and water using ultrasonic sensors, and to alert the user through a vibration motor and a buzzer. The circuit is controlled by an Arduino Mega 2560 microcontroller. The system includes multiple ultrasonic sensors, a soil sensor, a vibration motor, a buzzer, and a rocker switch for emergency situations.
Component List
Rocker Switch
- Pins: 1, 2
- Description: A switch used to activate or deactivate the emergency mode.
- Purpose: To provide a manual override for emergency situations.
HC-SR04 Ultrasonic Sensor
- Pins: VCC, TRIG, ECHO, GND
- Description: Ultrasonic sensor for distance measurement.
- Purpose: To detect obstacles at various heights and water levels.
Vibration Motor
- Pins: +, -
- Description: A motor that provides haptic feedback.
- Purpose: To alert the user through vibrations when an obstacle is detected.
Buzzer
- Pins: GND, IN
- Description: An audio alert device.
- Purpose: To provide an audible alert in emergency situations or when water is detected.
Lipo Battery 2200mAH 30c
- Pins: VCC, GND
- Description: A rechargeable battery.
- Purpose: To power the entire circuit.
Arduino Mega 2560
- Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL
- Description: A microcontroller board based on the ATmega2560.
- Purpose: To control the sensors, motor, and buzzer based on the programmed logic.
Soil Sensor
- Pins: VCC, GND, D0, A0
- Description: A sensor to measure soil moisture.
- Purpose: To detect the presence of water.
Wiring Details
Rocker Switch
- Pin 1: Connected to Arduino Mega 2560 pin D8 PWM
- Pin 2: Connected to GND (shared with Vibration Motor, Buzzer, Soil Sensor, and Arduino Mega 2560)
HC-SR04 Ultrasonic Sensor (Upper)
- VCC: Connected to 5V (shared with other HC-SR04 sensors and Soil Sensor)
- TRIG: Connected to Arduino Mega 2560 pin D2 PWM
- ECHO: Connected to Arduino Mega 2560 pin D3 PWM
- GND: Connected to GND (shared with other HC-SR04 sensors and Arduino Mega 2560)
HC-SR04 Ultrasonic Sensor (Middle)
- VCC: Connected to 5V (shared with other HC-SR04 sensors and Soil Sensor)
- TRIG: Connected to Arduino Mega 2560 pin D4 PWM
- ECHO: Connected to Arduino Mega 2560 pin D5 PWM
- GND: Connected to GND (shared with other HC-SR04 sensors and Arduino Mega 2560)
HC-SR04 Ultrasonic Sensor (Bottom)
- VCC: Connected to 5V (shared with other HC-SR04 sensors and Soil Sensor)
- TRIG: Connected to Arduino Mega 2560 pin D12 PWM
- ECHO: Connected to Arduino Mega 2560 pin D13 PWM
- GND: Connected to GND (shared with other HC-SR04 sensors and Arduino Mega 2560)
HC-SR04 Ultrasonic Sensor (Water)
- VCC: Connected to 5V (shared with other HC-SR04 sensors and Soil Sensor)
- TRIG: Connected to Arduino Mega 2560 pin D10 PWM
- ECHO: Connected to Arduino Mega 2560 pin D11 PWM
- GND: Connected to GND (shared with other HC-SR04 sensors and Arduino Mega 2560)
Vibration Motor
- +: Connected to Arduino Mega 2560 pin D9 PWM
- -: Connected to GND (shared with Rocker Switch, Buzzer, Soil Sensor, and Arduino Mega 2560)
Buzzer
- IN: Connected to Arduino Mega 2560 pin D7 PWM
- GND: Connected to GND (shared with Rocker Switch, Vibration Motor, Soil Sensor, and Arduino Mega 2560)
Lipo Battery 2200mAH 30c
- VCC: Connected to Arduino Mega 2560 VIN
- GND: Connected to GND (shared with Arduino Mega 2560)
Arduino Mega 2560
- 5V: Connected to VCC of HC-SR04 sensors and Soil Sensor
- GND: Connected to GND of all components
- VIN: Connected to VCC of Lipo Battery
Soil Sensor
- VCC: Connected to 5V (shared with HC-SR04 sensors)
- GND: Connected to GND (shared with Rocker Switch, Vibration Motor, Buzzer, and Arduino Mega 2560)
- D0: Connected to Arduino Mega 2560 pin D10 PWM
Documented Code
#include <Arduino.h>
#include <NewPing.h>
// Pin Definitions
#define TRIG_PIN_UPPER 2 // Upper ultrasonic sensor (obstacles above user)
#define ECHO_PIN_UPPER 3
#define TRIG_PIN_MIDDLE 4 // Middle ultrasonic sensor (front obstacles)
#define ECHO_PIN_MIDDLE 5
#define TRIG_PIN_WATER 10 // Bottom ultrasonic sensor (water detection)
#define ECHO_PIN_WATER 11
#define TRIG_PIN_BOTTOM 12 // Bottom ultrasonic sensor (replacing IR)
#define ECHO_PIN_BOTTOM 13
#define VIBRATION_MOTOR_PIN 9
#define ROCKER_SWITCH_PIN 8 // Emergency rocker switch
#define BUZZER_PIN 7
// Constants
#define MAX_DISTANCE 200 // Maximum distance in cm for ultrasonic sensors
#define WATER_THRESHOLD 10 // Threshold for water detection in cm
#define DETECTION_DELAY 100 // Delay between detection cycles
#define PWM_HIGH 255 // High-speed vibration
#define PWM_MEDIUM 128 // Medium-speed vibration
#define PWM_LOW 64 // Low-speed vibration
// Objects for ultrasonic sensors
NewPing sonarUpper(TRIG_PIN_UPPER, ECHO_PIN_UPPER, MAX_DISTANCE);
NewPing sonarMiddle(TRIG_PIN_MIDDLE, ECHO_PIN_MIDDLE, MAX_DISTANCE);
NewPing sonarWater(TRIG_PIN_WATER, ECHO_PIN_WATER, MAX_DISTANCE);
NewPing sonarBottom(TRIG_PIN_BOTTOM, ECHO_PIN_BOTTOM, MAX_DISTANCE);
// Global Variables
volatile bool emergencyActive = false; // Emergency flag
void setup() {
pinMode(VIBRATION_MOTOR_PIN, OUTPUT);
pinMode(ROCKER_SWITCH_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
// Interrupt for emergency rocker switch
attachInterrupt(digitalPinToInterrupt(ROCKER_SWITCH_PIN), handleEmergencyInterrupt, CHANGE);
// Start Serial for debugging
Serial.begin(9600);
Serial.println("System Initialized.");
}
void loop() {
if (emergencyActive) {
// Activate buzzer for emergency
digitalWrite(BUZZER_PIN, HIGH);
Serial.println("Emergency Mode Active! Buzzer ON.");
} else {
// Turn off emergency buzzer and handle other functionality
digitalWrite(BUZZER_PIN, LOW);
handleUpperSensor();
handleMiddleSensor();
handleBottomSensor();
handleWaterDetection();
}
delay(DETECTION_DELAY); // Small delay between cycles
}
// Interrupt Service Routine for Emergency System
void handleEmergencyInterrupt() {
emergencyActive = (digitalRead(ROCKER_SWITCH_PIN) == LOW);
Serial.print("Emergency Switch State Changed. Emergency Active: ");
Serial.println(emergencyActive);
}
// Function to handle upper ultrasonic sensor
void handleUpperSensor() {
int distance = sonarUpper.ping_cm();
Serial.print("Upper Sensor Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > 0 && distance <= 50) {
Serial.println("Obstacle detected above. Vibrating motor at HIGH speed.");
for (int i = 0; i < 3; i++) { // Vibrate 3 times
analogWrite(VIBRATION_MOTOR_PIN, PWM_HIGH);
delay(400);
analogWrite(VIBRATION_MOTOR_PIN, 0);
delay(400); // Adjust the pause between vibrations
}
} else if (distance > 50 && distance <= 100) {
Serial.println("Obstacle detected above. Vibrating motor at MEDIUM speed.");
for (int i = 0; i < 3; i++) { // Vibrate 3 times
analogWrite(VIBRATION_MOTOR_PIN, PWM_MEDIUM);
delay(400);
analogWrite(VIBRATION_MOTOR_PIN, 0);
delay(400