Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560-Based Obstacle Detection and Moisture Sensing System with Vibration Motor and Buzzer

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560-Based Obstacle Detection and Moisture Sensing System with Vibration Motor and Buzzer

Circuit Documentation

Summary

This circuit is designed to detect obstacles and water using ultrasonic sensors and a moisture sensor, respectively. It uses an Arduino Mega 2560 microcontroller to process sensor data and control a vibration motor and a buzzer. The system includes an emergency rocker switch to activate an emergency mode.

Component List

  1. Rocker Switch

    • Description: A switch used to activate emergency mode.
    • Pins: 1, 2
  2. HC-SR04 Ultrasonic Sensor

    • Description: Ultrasonic sensor used for distance measurement.
    • Pins: VCC, TRIG, ECHO, GND
  3. Vibration Motor

    • Description: Motor that vibrates to provide haptic feedback.
    • Pins: +, -
  4. Buzzer

    • Description: Buzzer used for audio alerts.
    • Pins: GND, IN
  5. LiPo Battery 2200mAH 30C

    • Description: Power source for the circuit.
    • Pins: VCC, GND
  6. Arduino Mega 2560

    • Description: Microcontroller used to control the circuit.
    • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL
  7. Soil Sensor

    • Description: Sensor used to detect moisture.
    • Pins: VCC, GND, D0, A0

Wiring Details

Rocker Switch

HC-SR04 Ultrasonic Sensor (Upper)

HC-SR04 Ultrasonic Sensor (Middle)

HC-SR04 Ultrasonic Sensor (Bottom)

Vibration Motor

Buzzer

LiPo Battery 2200mAH 30C

Soil Sensor

Code Documentation

#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 MOISTURE_SENSOR_PIN 10 // Moisture sensor (water detection)
#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 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 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);
    pinMode(MOISTURE_SENSOR_PIN, INPUT); // Moisture sensor pin

    // 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();  
        handleMoistureDetection(); // New moisture sensor function
    }

    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);
        }
    } else if (distance > 100 && distance <= 200) {
        Serial.println("Obstacle detected above. Vibrating motor at LOW speed.");
        for (int i = 0; i < 3; i++) { // Vibrate 3 times
            analogWrite(VIBRATION_MOTOR_PIN, PWM_LOW);
            delay(400);
            analogWrite(VIBRATION_MOTOR_PIN, 0);
            delay(400);
        }
    } else {
        Serial.println("No obstacle above. Motor OFF.");
        analogWrite(VIBRATION_MOTOR_PIN, 0);
    }
}

// Function to handle middle ultrasonic sensor
void handleMiddleSensor() {
    int distance = sonarMiddle.ping_cm();
    Serial.print("Middle Sensor Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    if (distance > 0 && distance <= 50) {
        Serial.println("Obstacle detected in front. Vibrating motor at HIGH speed.");
        for (int i = 0; i < 2; i++) { // Vibrate 2 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 in front. Vibrating motor at MEDIUM speed.");
        for (int i = 0; i < 2; i++) { // Vibrate 2 times
            analogWrite(VIBRATION_MOTOR_PIN, PWM_MEDIUM);
            delay(400);
            analogWrite(VIBRATION_MOTOR_PIN, 0);
            delay(400);
        }
    } else if (distance > 100 && distance <= 200) {
        Serial.println("Obstacle detected in front. Vibrating motor at LOW speed.");
        for (int i = 0; i < 2; i