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

Arduino UNO Based Ultrasonic Distance Measurement with LED Indicators and Buzzer Alert

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Ultrasonic Distance Measurement with LED Indicators and Buzzer Alert

Circuit Documentation

Summary

This circuit is designed to utilize an Arduino UNO microcontroller to interface with two HC-SR04 Ultrasonic Sensors, a buzzer, and multiple LEDs of different colors (red, green, and yellow). The ultrasonic sensors are used to measure distances, and based on these measurements, the LEDs are controlled to indicate different distance ranges. Additionally, the buzzer is used to alert when an object is within a very close range to either of the sensors. The circuit is powered by the 5V output from the Arduino UNO, and ground connections are shared among the components.

Component List

Microcontroller

Sensors

Actuators

Indicators

Passive Components

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensors

LEDs

Buzzer

Resistors

Documented Code

// Define pins for ultrasonic sensors
const int trigPin1 = 7;
const int echoPin1 = 6;
const int trigPin2 = 9;
const int echoPin2 = 8;

// Define LED pins (2 sets of 3 LEDs)
const int led1 = 2;
const int led2 = 3;
const int led3 = 4;
const int led4 = 10;
const int led5 = 11;
const int led6 = 12;

// Define Buzzer pin
const int buzzerPin = 5;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Set pin modes for ultrasonic sensors
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  // Set pin modes for LEDs
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);

  // Set pin mode for buzzer
  pinMode(buzzerPin, OUTPUT);
}

long readUltrasonicDistance(int trigPin, int echoPin) {
  // Send a 10us pulse to trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the pulse duration from echo pin
  long duration = pulseIn(echoPin, HIGH);
  
  // Convert to distance in cm
  long distance = duration * 0.034 / 2;
  return distance;
}

void controlLEDSet1(long distance) {
  // Control first set of LEDs based on decreasing distance
  if (distance < 10) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);  // Third LED on (closest distance)
  } else if (distance >= 10 && distance < 20) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);  // Second LED on
    digitalWrite(led3, LOW);
  } else if (distance >= 20) {
    digitalWrite(led1, HIGH);  // First LED on (farthest distance)
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
  }
}

void controlLEDSet2(long distance) {
  // Control second set of LEDs based on decreasing distance
  if (distance < 10) {
    digitalWrite(led4, LOW);
    digitalWrite(led5, LOW);
    digitalWrite(led6, HIGH);  // Third LED on (closest distance)
  } else if (distance >= 10 && distance < 20) {
    digitalWrite(led4, LOW);
    digitalWrite(led5, HIGH);  // Second LED on
    digitalWrite(led6, LOW);
  } else if (distance >= 20) {
    digitalWrite(led4, HIGH);  // First LED on (farthest distance)
    digitalWrite(led5, LOW);
    digitalWrite(led6, LOW);
  }
}

void controlBuzzerAndBlink(long distance1, long distance2) {
  // Blink third LEDs and activate buzzer when distance is less than 8 cm
  if (distance1 < 8 || distance2 < 8) {
    // Blink the last LEDs (led3 and led6)
    digitalWrite(led3, !digitalRead(led3));  // Toggle led3
    digitalWrite(led6, !digitalRead(led6));  // Toggle led6
    
    // Activate buzzer when either third LED is on
    if (digitalRead(led3) == HIGH || digitalRead(led6) == HIGH) {
      digitalWrite(buzzerPin, HIGH);  // Turn buzzer on
    } else {
      digitalWrite(buzzerPin, LOW);   // Turn buzzer off
    }
  } else {
    digitalWrite(buzzerPin, LOW);  // Buzzer off when out of range
  }
}

void loop() {
  // Read distances from both ultrasonic sensors
  long distance1 = readUltrasonicDistance(trigPin1, echoPin1);
  long distance2 = readUltrasonicDistance(trigPin2, echoPin2);

  // Control LEDs and buzzer
  controlLEDSet1(distance1);
  controlLEDSet2(distance2);
  controlBuzzerAndBlink(distance1, distance2);
  
  // Print distances to Serial Monitor (for debugging)
  Serial.print("Distance 1: ");
  Serial.print(distance1);
  Serial.print(" cm, Distance 2: ");
  Serial.println(distance2);

  delay(500);  // Short delay for blinking effect
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins, defines the main loop, and includes functions to read distances from the ultrasonic sensors, control the LEDs, and operate the buzzer based on the sensor readings. The code also includes serial output for debugging purposes.