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

Arduino UNO Based Ultrasonic Distance Detection System with Visual and Audio Alerts

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Ultrasonic Distance Detection System with Visual and Audio Alerts

Circuit Documentation

Summary

This circuit is designed to utilize an Arduino UNO microcontroller to interface with two HC-SR04 Ultrasonic Sensors, two red LEDs, and a buzzer. The ultrasonic sensors are used to measure distances, and based on these measurements, the LEDs and buzzer are controlled to indicate proximity of objects. The circuit is powered by a 5V battery, and the LEDs are connected through 220 Ohm resistors to limit the current.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor (x2)

LED: Two Pin (red) (x2)

Buzzer

Resistor (x2)

5V Battery

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

LED: Two Pin (red)

Buzzer

Resistor

5V Battery

Documented Code

#define LEFT_TRIGGER_PIN 9
#define LEFT_ECHO_PIN 10
#define RIGHT_TRIGGER_PIN 11
#define RIGHT_ECHO_PIN 12
#define LEFT_LED_PIN 3
#define RIGHT_LED_PIN 4
#define BUZZER_PIN 5

void setup() {
    Serial.begin(9600);
    pinMode(LEFT_TRIGGER_PIN, OUTPUT);
    pinMode(LEFT_ECHO_PIN, INPUT);
    pinMode(RIGHT_TRIGGER_PIN, OUTPUT);
    pinMode(RIGHT_ECHO_PIN, INPUT);
    pinMode(LEFT_LED_PIN, OUTPUT);
    pinMode(RIGHT_LED_PIN, OUTPUT);
    pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
    long leftDistance = getDistance(LEFT_TRIGGER_PIN, LEFT_ECHO_PIN);
    long rightDistance = getDistance(RIGHT_TRIGGER_PIN, RIGHT_ECHO_PIN);

    // Process left sensor
    if (leftDistance < 100) { // Object detected less than 1 meter
        digitalWrite(LEFT_LED_PIN, HIGH);
        tone(BUZZER_PIN, 1000); // Buzzer ON
        delay(200); // Short delay for buzzing
        digitalWrite(LEFT_LED_PIN, LOW);
        delay(200); // LED OFF for blinking
    } else if (leftDistance < 150) { // Object detected between 1 and 1.5 meters
        digitalWrite(LEFT_LED_PIN, HIGH); // LED ON
        noTone(BUZZER_PIN); // Buzzer OFF
    } else { // Object detected more than 1.5 meters
        digitalWrite(LEFT_LED_PIN, LOW); // LED OFF
        noTone(BUZZER_PIN); // Buzzer OFF
    }

    // Process right sensor
    if (rightDistance < 100) { // Object detected less than 1 meter
        digitalWrite(RIGHT_LED_PIN, HIGH);
        tone(BUZZER_PIN, 1000); // Buzzer ON
        delay(200); // Short delay for buzzing
        digitalWrite(RIGHT_LED_PIN, LOW);
        delay(200); // LED OFF for blinking
    } else if (rightDistance < 150) { // Object detected between 1 and 1.5 meters
        digitalWrite(RIGHT_LED_PIN, HIGH); // LED ON
        noTone(BUZZER_PIN); // Buzzer OFF
    } else { // Object detected more than 1.5 meters
        digitalWrite(RIGHT_LED_PIN, LOW); // LED OFF
        noTone(BUZZER_PIN); // Buzzer OFF
    }

    delay(100); // Short delay before next reading
}

long getDistance(int triggerPin, int echoPin) {
    // Clear the trigger
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(2);
    
    // Set the trigger on
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);

    // Read the echo pin
    long duration = pulseIn(echoPin, HIGH);
    
    // Calculate the distance in cm
    long distance = duration * 0.034 / 2; // Speed of sound is 0.034 cm/us
    return distance;
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the ultrasonic sensors, LEDs, and buzzer. In the main loop, it measures distances using the ultrasonic sensors and then activates the LEDs and buzzer based on the distance thresholds defined. The getDistance function is used to encapsulate the logic for triggering the ultrasonic sensor and reading the echo to calculate the distance.