Ultrasonic Radar
This project aims of to build an ultrasonic radar system with arduino to get exact distance and angle of fixed objects placed around the device based on the speed of ultrasonic waves in the air.
Hardware Required
Arduino UNO
Ultrasonic Sensor
Servo Motor
Breadboard
Jumper wires
Software Required
Arduino IDE
Connections
-
Vcc of servomotor to 5v of arduino
-
GND of servomotor to GND of the arduino
-
Signal pin of servo to pin 9 of arduino.
-
-
Vcc of ultrasonic sensor to 5v of arduino
-
GND of ultrasonic sensor to GND of the arduino
-
Trig pin of ultrasonic sensor to pin 8 of arduino
-
Echo pin of ultrasonic sensor to pin 7 of arduino.
Code
#include <Servo.h>
Servo leftRightServo;
int leftRightPos = 0;
const int numReadings = 10;
int index = 0;
int total = 0;
int average = 0;
int echoPin = 7; //
int initPin = 8; //
unsigned long pulseTime = 0;
unsigned long distance = 0;
void setup() {
leftRightServo.attach(9);
pinMode(initPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) {
leftRightServo.write(leftRightPos);
for (index = 0; index<=numReadings;index++) {
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH);
delayMicroseconds(50);
digitalWrite(initPin, LOW);
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime/58;
total = total + distance;
delay(10);
}
average = total/numReadings;
if (index >= numReadings) {
total = 0;
}
Serial.print("X");
Serial.print(leftRightPos);
Serial.print("V");
Serial.println(average);
}
for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) {
leftRightServo.write(leftRightPos);
for (index = 0; index<=numReadings;index++) {
digitalWrite(initPin, LOW);
delayMicroseconds(50);
digitalWrite(initPin, HIGH);
delayMicroseconds(50);
digitalWrite(initPin, LOW);
pulseTime = pulseIn(echoPin, HIGH);
distance = pulseTime/58;
total = total + distance;
delay(10);
}
average = total/numReadings;
if (index >= numReadings) {
index = 0;
total = 0;
}
Serial.print("X");
Serial.print(leftRightPos);
Serial.print("V");
Serial.println(average);
}
}