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

Arduino UNO-Based Dual Ultrasonic Sensor Distance Measurement System with Buzzer Alert

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Dual Ultrasonic Sensor Distance Measurement System with Buzzer Alert

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller, two ultrasonic sensors, a pushbutton, and a buzzer. The circuit is designed to measure distances using the ultrasonic sensors and trigger a buzzer if an object is detected within a certain range. The pushbutton is used to switch between the two ultrasonic sensors.

Component List

  1. Pushbutton

    • Pins: Pin 1 (in), Pin 2 (in), Pin 3 (out), Pin 4 (out)
    • Description: A simple pushbutton used to switch between two modes of operation.
    • Purpose: To toggle between the two ultrasonic sensors.
  2. Arduino UNO

    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
    • Description: A microcontroller board based on the ATmega328P.
    • Purpose: To control the ultrasonic sensors and the buzzer, and to read the state of the pushbutton.
  3. Ultrasonic Sensor (U1)

    • Pins: +VCC, Trigger, Echo, GND
    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Purpose: To measure the distance to an object.
  4. Ultrasonic Sensor (U2)

    • Pins: +VCC, Trigger, Echo, GND
    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Purpose: To measure the distance to an object.
  5. Buzzer

    • Pins: PIN, GND
    • Description: An electronic device that emits a sound when activated.
    • Purpose: To alert when an object is detected within a certain range.

Wiring Details

Pushbutton

Arduino UNO

Ultrasonic Sensor (U1)

Ultrasonic Sensor (U2)

Buzzer

Code Documentation

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

// Define other pins
const int controlPin = A0; // Set A0 as high
const int modePin = 13;    // Mode pin
const int buzzerPin = 5;   // Buzzer pin

// Function to measure distance from an ultrasonic sensor
long measureDistance(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2; // Convert duration to distance in cm
  return distance;
}

void setup() {
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  
  pinMode(controlPin, OUTPUT);
  pinMode(modePin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  
  digitalWrite(controlPin, HIGH); // Set A0 high
  Serial.begin(9600); // Initialize serial communication for debugging
}

void loop() {
  int mode = digitalRead(modePin);
  
  if (mode == LOW) {
    // Activate sensor U1
    long distance1 = measureDistance(trigPin1, echoPin1);
    if (distance1 < 30) {
      digitalWrite(buzzerPin, HIGH);
    } else {
      digitalWrite(buzzerPin, LOW);
    }
    Serial.print("Distance from U1: ");
    Serial.println(distance1);
  } else {
    // Activate sensor U2
    long distance2 = measureDistance(trigPin2, echoPin2);
    if (distance2 < 30) {
      digitalWrite(buzzerPin, HIGH);
    } else {
      digitalWrite(buzzerPin, LOW);
    }
    Serial.print("Distance from U2: ");
    Serial.println(distance2);
  }
  delay(100);
}

This code initializes the pins for the ultrasonic sensors, the pushbutton, and the buzzer. It measures the distance using the ultrasonic sensors and activates the buzzer if an object is detected within 30 cm. The pushbutton is used to switch between the two ultrasonic sensors.