← All Projects
Air Defense System with Radar
25 viewsApril 2, 2026
arduinoradarair defense
Circuit Connections
| Component | Pin | Pin | Component | |
|---|---|---|---|---|
| Arduino | 9 | → | trig | Ultrasonic |
| Arduino | 10 | → | echo | Ultrasonic |
| Arduino | 5V | → | VCC | Ultrasonic |
| Arduino | GND | → | GND | Ultrasonic |
| Arduino | 11 | → | +VE | Buzzer |
| Arduino | GND | → | GND | Buzzer |
| Arduino | GND | → | BROWN | Servo Motor |
| Arduino | 5V | → | RED | Servo Motor |
| Arduino | 6 | → | ORANGE | Servo Motor |
arduino— code.ino
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int motorPin = 12;
const int servoPin = 6;
Servo myServo;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(motorPin, OUTPUT);
myServo.attach(servoPin);
Serial.begin(9600); // Communication speed
}
void loop() {
// Scan from 10 to 170 degrees
for(int i=10; i<=170; i++){
myServo.write(i);
delay(30);
calculateDistance(i);
}
// Scan back from 170 to 10 degrees
for(int i=170; i>=10; i--){
myServo.write(i);
delay(30);
calculateDistance(i);
}
}
void calculateDistance(int angle) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Send data to Processing in format: "angle,distance."
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
// Physical Alert Logic
if (distance > 0 && distance <= 50) {
digitalWrite(buzzer, HIGH);
digitalWrite(motorPin, HIGH);
} else {
digitalWrite(buzzer, LOW);
digitalWrite(motorPin, LOW);
}
}java— processing
import processing.serial.*;
Serial myPort;
String data = "";
float iAngle, iDistance;
int index1 = 0;
void setup() {
size(1200, 700); // Adjust to your screen size
smooth();
// NOTE: If you get an error, change [0] to [1] or [2]
// to match the COM port your Arduino is plugged into.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('.'); // Stops reading at the dot sent by Arduino
}
void draw() {
fill(0, 15); // This creates the "fade" effect for the sweep
noStroke();
rect(0, 0, width, height - height*0.065);
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent (Serial myPort) {
data = myPort.readStringUntil('.');
data = data.substring(0, data.length()-1); // Remove the dot
index1 = data.indexOf(",");
if (index1 > -1) {
iAngle = float(data.substring(0, index1));
iDistance = float(data.substring(index1 + 1, data.length()));
}
}
void drawRadar() {
pushMatrix();
translate(width/2, height - height*0.074);
noFill();
strokeWeight(2);
stroke(98, 245, 31);
// Drawing the concentric semi-circles
arc(0,0, (width-width*0.0625), (width-width*0.0625), PI, TWO_PI);
arc(0,0, (width-width*0.27), (width-width*0.27), PI, TWO_PI);
arc(0,0, (width-width*0.479), (width-width*0.479), PI, TWO_PI);
arc(0,0, (width-width*0.687), (width-width*0.687), PI, TWO_PI);
// Drawing the angle lines
line(-width/2, 0, width/2, 0);
for (int i=30; i<=150; i+=30) {
line(0,0, (-width/2)*cos(radians(i)), (-width/2)*sin(radians(i)));
}
popMatrix();
}
void drawObject() {
pushMatrix();
translate(width/2, height - height*0.074);
strokeWeight(9);
stroke(255, 10, 10); // Red color for detected threats
// Map the distance to pixels (scale of 50cm max)
float pixsDistance = iDistance * ((height - height * 0.1666) * 0.025);
if (iDistance < 50 && iDistance > 0) {
// Draws a line at the specific angle where object is detected
line(pixsDistance*cos(radians(iAngle)), -pixsDistance*sin(radians(iAngle)),
(width-width*0.505)*cos(radians(iAngle)), -(width-width*0.505)*sin(radians(iAngle)));
}
popMatrix();
}
void drawLine() {
pushMatrix();
strokeWeight(9);
stroke(30, 250, 60); // Bright green sweep line
translate(width/2, height - height*0.074);
line(0, 0, (height - height*0.12)*cos(radians(iAngle)), -(height - height*0.12)*sin(radians(iAngle)));
popMatrix();
}
void drawText() {
pushMatrix();
fill(0);
noStroke();
rect(0, height - height*0.0648, width, height);
fill(98, 245, 31);
textSize(25);
text("Status: " + (iDistance < 50 && iDistance > 0 ? "THREAT DETECTED" : "CLEAR"), width*0.05, height-height*0.027);
text("Angle: " + int(iAngle) + "°", width*0.45, height-height*0.027);
text("Distance: " + int(iDistance) + " cm", width*0.75, height-height*0.027);
popMatrix();
}