Ultrasonic Radar System Using Arduino (Automation Project)
In this project, an Arduino is used to control a servo motor that sweeps an ultrasonic distance sensor back and forth. The distance to obstacles is measured using the sensor, and the information is used to trigger different levels of buzzer alerts. This system is useful in applications where obstacle detection and varying levels of alerts are needed.
Project : 48
Obstacle Detection System
with Servo and Buzzer
Introduction
In this project, an Arduino is used to control a servo motor that sweeps
an ultrasonic distance sensor back and forth. The distance to obstacles
is measured using the sensor, and the information is used to trigger different levels of buzzer alerts. This system is useful in applications where obstacle
detection and varying levels of alerts are needed.
Components Required
1.
Arduino Board:
The main control unit that processes sensor data and controls the servo and buzzer.
2.
Ultrasonic
Distance Sensor (HC-SR04):
Provides auditory alerts based on obstacle proximity
Pin Configuration
·
Ultrasonic Sensor:
o
Trigger Pin (Trig) connected to Arduino pin 2.
o
Echo Pin connected
to Arduino pin 3.
·
Servo Motor: Connected to Arduino pin 6.
·
Buzzer: Connected to Arduino pin 9.
Libraries Used
·
Servo: For controlling the servo motor.
Code
#include <Servo.h>
// Pin
definitions const int trigPin = 2; const int echoPin = 3; const int servoPin = 6;
const int buzzerPin = 9;
// Variables for distance measurement long duration;
int distance;
Servo myServo; void setup() {
pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT); Serial.begin(9600);
myServo.attach(servoPin);
}
void loop() {
// Sweep servo from 15 to 165 degrees for (int i = 15; i <= 165; i++)
{
myServo.write(i); delay(30);
distance = calculateDistance();
handleBuzzer(distance);
Serial.print(i);
Serial.print(",");
Serial.print(distance); Serial.print(".");
}
// Sweep servo from 165 to 15 degrees for (int i = 165; i >= 15; i--) {
myServo.write(i); delay(30);
distance = calculateDistance();
handleBuzzer(distance);
Serial.print(i);
Serial.print(",");
Serial.print(distance); Serial.print(".");
}
}
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2;
return distance;
}
void handleBuzzer(int distance) { if (distance > 39) {
digitalWrite(buzzerPin, LOW);
}
else if (distance <=
40 && distance
> 20) {
beepBuzzer(150);
} else if (distance <= 20 && distance > 10) {
beepBuzzer(70);
} else if (distance <= 10) { beepBuzzer(10);
}
}
void beepBuzzer(int delayTime) { for (int j = 0; j < 6; j++) {
digitalWrite(buzzerPin, HIGH); delay(delayTime);
digitalWrite(buzzerPin, LOW);
delay(delayTime);
}
}
Working
1.
Initialization:
o
The Arduino initializes serial communication at 9600 baud.
o The servo motor and buzzer are set up with their respective pins.
o The Servo library is used to control the servo motor, and the distance sensor is initialized.
2.
Main Loop:
o The servo
motor sweeps the ultrasonic sensor
from 15 to 165
degrees and then back.
o During each sweep, the distance to the closest
obstacle is measured.
o
Based on the distance, the buzzer provides
alerts:
§ If the distance is greater than 39 cm, the buzzer is off.
§ If the distance is between 40 cm and 20 cm, the buzzer beeps with a delay of 150ms.
§ If the distance is between 20 cm and 10 cm, the buzzer beeps with a delay of 70ms.
§ If the distance is less than 10 cm, the buzzer beeps with a
delay of 10 ms.
o The servo motor angle and distance
measurements are output to
the Serial Monitor.
3.
Distance Calculation:
o The calculateDistance() function triggers the ultrasonic sensor and
calculates the distance based on the time taken for the echo to
return.
4.
Buzzer Handling:
o The handleBuzzer() function determines the appropriate buzzer alert based on the measured
distance.
5.
Buzzer Beeping:
o The beepBuzzer() function creates a sequence of beeps for a
specified delay time.
Testing
1.
Setup: Assemble all components as per the pin configuration.
2. Power Up: Power the Arduino and ensure all components are properly
connected.
3. Sensor Testing: Place obstacles at various
distances to test the sensor's response and verify that the
buzzer alerts correspond to the distances.
4. Servo Movement: Observe the servo sweeping
action and ensure
that it covers the intended range.
5. Monitoring: Continuously monitor
the distance readings
on the Serial Monitor and check if the buzzer alerts match the expected
behavior.
Conclusion
The Obstacle Detection System
with Servo and Buzzer project
successfully
demonstrates how to integrate
a servo motor, ultrasonic sensor,
and buzzer to create
a real-time obstacle detection and alert system.
The project provides a practical example of how to combine different
components for effective
environmental monitoring and alerting, showcasing the versatility of Arduino-
based projects in practical applications.
Team Members:
1) Aadarasha
2) Aman
3) Annie
4) Bidhya
5) Rijan
6) Samikshya
7) Sauravi
College : Nepathya College,Tilottama-5