CodeHub
HomeUploadContact
Back to Hub

Radar system Arduino

March 24, 2026 Watch Tutorial

Wiring Schematic

Radar system Arduino wiring diagram

Hardware Required

  • Arduino Board
    Buy on Amazon

Source Code

Arduino / C++
1#include <Servo.h>
2
3Servo myServo;
4
5const int trigPin = 7;
6const int echoPin = 6;
7
8int angle = 0;
9int direction = 1; // 1 = forward, -1 = backward
10
11unsigned long lastServoMove = 0;
12unsigned long lastSensorRead = 0;
13
14int distance = 0;
15
16void setup() {
17  Serial.begin(9600);
18
19  pinMode(trigPin, OUTPUT);
20  pinMode(echoPin, INPUT);
21
22  myServo.attach(9);
23}
24
25void loop() {
26
27  // 🔁 Servo moves continuously (every 20ms)
28  if (millis() - lastServoMove >= 20) {
29    lastServoMove = millis();
30
31    angle += direction;
32
33    if (angle >= 180) direction = -1;
34    if (angle <= 0) direction = 1;
35
36    myServo.write(angle);
37  }
38
39  // 📡 Ultrasonic reads independently (every 50ms)
40  if (millis() - lastSensorRead >= 50) {
41    lastSensorRead = millis();
42
43    distance = getDistance();
44
45    Serial.print(angle);
46    Serial.print(",");
47    Serial.print(distance);
48    Serial.print(".");
49  }
50}
51
52// 📏 Distance function (stable)
53int getDistance() {
54  digitalWrite(trigPin, LOW);
55  delayMicroseconds(2);
56
57  digitalWrite(trigPin, HIGH);
58  delayMicroseconds(10);
59  digitalWrite(trigPin, LOW);
60
61  long duration = pulseIn(echoPin, HIGH, 30000); // timeout added
62
63  if (duration == 0) return 0;
64
65  int dist = duration * 0.034 / 2;
66  return dist;
67} 
68
69
70
71
72
73// Prcoessing Code:-
74
75import processing.serial.*;
76import java.awt.event.KeyEvent;
77import java.io.IOException;
78
79Serial myPort;
80
81String distance="";
82String data="";
83String noObject;
84String angle="";
85
86float pixsDistance;
87int iAngle=0, iDistance=0;
88int index1=0;
89
90PFont orcFont;
91
92void setup() {
93  size(1280,720);   // ✅ FIXED
94  smooth();
95  
96  println(Serial.list());
97  myPort = new Serial(this,"COM3",9600); // change if needed
98  myPort.bufferUntil('.');
99}
100
101void draw() {
102
103  // fade effect (better radar look)
104  fill(0,20);
105  noStroke();
106  rect(0, 0, width, height);
107
108  fill(98,245,31);
109
110  drawRadar();
111  drawLine();
112  drawObject();
113  drawText();
114}
115
116// ✅ FIXED + STABLE SERIAL
117void serialEvent (Serial myPort) {
118  data = myPort.readStringUntil('.');
119  
120  if (data != null) {
121    data = trim(data);
122
123    if (data.contains(",")) {
124      index1 = data.indexOf(',');
125
126      try {
127        iAngle = int(data.substring(0, index1));
128        iDistance = int(data.substring(index1+1));
129      } 
130      catch(Exception e) {
131        // ignore bad data
132      }
133    }
134  }
135}
136
137void drawRadar() {
138  pushMatrix();
139  translate(width/2,height-height*0.074);
140  noFill();
141  strokeWeight(2);
142  stroke(98,245,31);
143
144  arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
145  arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
146  arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
147  arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
148
149  line(-width/2,0,width/2,0);
150  line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
151  line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
152  line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
153  line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
154  line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
155
156  popMatrix();
157}
158
159// 🔴 OBJECT LINE (ONLY WITHIN 10cm)
160void drawObject() {
161  pushMatrix();
162  translate(width/2,height-height*0.074);
163  strokeWeight(9);
164
165  pixsDistance = iDistance*((height-height*0.1666)*0.025);
166
167  if(iDistance > 0 && iDistance <= 10){
168    stroke(255,0,0); // RED
169    line(pixsDistance*cos(radians(iAngle)),
170         -pixsDistance*sin(radians(iAngle)),
171         (width-width*0.505)*cos(radians(iAngle)),
172         -(width-width*0.505)*sin(radians(iAngle)));
173  }
174
175  popMatrix();
176}
177
178// 🔴 SCANNING LINE COLOR CHANGE
179void drawLine() {
180  pushMatrix();
181  strokeWeight(9);
182
183  if(iDistance > 0 && iDistance <= 10){
184    stroke(255,0,0); // RED when detected
185  } else {
186    stroke(30,250,60); // GREEN normal
187  }
188
189  translate(width/2,height-height*0.074);
190  line(0,0,
191       (height-height*0.12)*cos(radians(iAngle)),
192       -(height-height*0.12)*sin(radians(iAngle)));
193
194  popMatrix();
195}
196
197void drawText() {
198  pushMatrix();
199
200  if(iDistance>40) {
201    noObject = "Out of Range";
202  } else {
203    noObject = "In Range";
204  }
205
206  fill(0,0,0);
207  noStroke();
208  rect(0, height-height*0.0648, width, height);
209
210  fill(98,245,31);
211  textSize(25);
212
213  text("10cm",width-width*0.3854,height-height*0.0833);
214  text("20cm",width-width*0.281,height-height*0.0833);
215  text("30cm",width-width*0.177,height-height*0.0833);
216  text("40cm",width-width*0.0729,height-height*0.0833);
217
218  textSize(40);
219  text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277);
220  text("Distance: " + iDistance +" cm", width-width*0.26, height-height*0.0277);
221
222  popMatrix();
223}
Servo Motor
Buy on Amazon
  • Ultrasonic Sensor
    Buy on Amazon
  • Breadboard
    Buy on Amazon
  • Jumper Wires
    Buy on Amazon
  • Usb Cable
    Buy on Amazon
  • Pc
    Buy on Amazon