How to make a RADAR SYSTEM | Radar system with Arduino Nano

How to make a Radar System

           Hello guys, Welcome back to the SriTu Hobby blog. This tutorial covers how to make a radar system using Arduino. This is a project that everyone will love. For this, I mainly use Arduino Nano board, Ultrasonic Sensor, Servo Motor, and Processing IDE. The ultrasonic sensor is used to measure the distance to the object. Click here for more information on the ultrasonic sensors. Also, this servo motor is used to rotate the ultrasonic sensor in both directions. Click this link for more information on the servo motors. Finally, the processing IDE visualizes the distances obtained through the ultrasonic sensor. Click this link for more information about Processing IDE.




Radar System Process

The servo motor rotates the ultrasonic sensor sideways. The ultrasonic sensor then calculates the distances ahead. Then those distances are taken into the processing IDE through serial communication and displayed as a radar system.
The accessories required for this are given below.
Arduino Nano x 1 — Amazon / Banggood
Ultrasonic Sensor x 1 — Amazon / Banggood
Servo motor x 1 — Amazon / Banggood
Breadboard x 1 — Amazon Banggood
Jumper wires  — Amazon / Banggood
OK, Let’s do this project step by step.

Step 1

First, identify these components.
Arduino Nano board.
Ultrasonic sensor.
Servo motor.
Breadboard.
Jumper wires.

Step 2

OK, Now attach the Arduino Nano board to the Breadboard.




Step 3

Then connect the servo motor to one side of the breadboard. For that, I’m using a glue gun.

Step 4

Next, attach the ultrasonic sensor to the top of the servo motor.

Step 5

Now, wire these components using the circuit diagram below.




Step 6

Well, next we will create the required Arduino program for this. It is presented below.
The complete program of this project – Download
/*Ultrasonic radar system.
  *Upload the following code first.
  *Serial monitor readings.
  *created by the SriTu Tech team.
  *Read the code below and use it for any of your creations.
*/
#include <Servo.h>//include library
Servo myServo;//create object your own name
int dis;
void setup() {
  pinMode(2, OUTPUT);//define arduino pin
  pinMode(3, INPUT);//define arduino pin
  Serial.begin(9600);//enable serial monitor
  myServo.attach(9);//servo connect pin
}
void loop() {
  for (int x = 0; x <= 180; x++) { //servo turn left
    myServo.write(x);//rotete servo
    dis=distance();
    Serial.print(x);//print servo angle
    Serial.print(",");
    Serial.print(dis);//print ultrasonic readings
    Serial.print(".");
    delay(50);
  }
  for (int y = 179; y > 0; y--) {//servo turn right
    myServo.write(y);//rotete servo
    dis=distance();
    Serial.print(y);////print servo angle
    Serial.print(",");
    Serial.print(dis);//print ultrasonic readings
    Serial.print(".");
    delay(50);
  }
}
//ultrasonic sensor code
int distance() {
  digitalWrite(2, LOW);
  delayMicroseconds(4);
  digitalWrite(2, HIGH);
  delayMicroseconds(10);
  digitalWrite(2, LOW);
 
 
  int t = pulseIn(3, HIGH);
  int cm = t / 29 / 2; //time convert distance
  return cm;//return value
}

Code explanation

Here we first insert the servo library and create an object called “myservo” for it. Also, a variable called “dis” has been created for distance.
#include <Servo.h>//include library
Servo myServo;//create object your own name
int dis;
After, the pins connecting the ultrasonic sensor and the servo motor in the void setup are defined. Also, the serial monitor is enabled.
void setup() {
  pinMode(2, OUTPUT);//define arduino pin
  pinMode(3, INPUT);//define arduino pin
  Serial.begin(9600);//enable serial monitor
  myServo.attach(9);//servo connect pin
}
 
Then create a function called “distance” and take the distance through the ultrasonic sensor and “return” that value.
int distance() {
  digitalWrite(2, LOW);
  delayMicroseconds(4);
  digitalWrite(2, HIGH);
  delayMicroseconds(10);
  digitalWrite(2, LOW);
 
 
  int t = pulseIn(3, HIGH);
  int cm = t / 29 / 2; //time convert distance
  return cm;//return value
}
Next, the code in the invalid loop rotates the servo motor 0 to 180 degrees and 179 to 0, and the distance from one degree to one degree is printed on the sequential monitor.
void loop() {
  for (int x = 0; x <= 180; x++) { //servo turn left
    myServo.write(x);//rotete servo
    dis=distance();
    Serial.print(x);//print servo angle
    Serial.print(“,”);
    Serial.print(dis);//print ultrasonic readings
    Serial.print(“.”);
    delay(50);
  }
  for (int y = 179; y > 0; y–) {//servo turn right
    myServo.write(y);//rotete servo
    dis=distance();
    Serial.print(y);////print servo angle
    Serial.print(“,”);
    Serial.print(dis);//print ultrasonic readings
    Serial.print(“.”);
    delay(50);
  }
}

Step 7

Well, now upload this code to the Arduino board. For that, select the correct board and port. After, upload this code.




Step 8

OK, Now Create the processing ide program. For that, you must have the Processing IDE installed on your computer. Click this link for the Processing IDE tutorial. The radar system Processing code is shown below.
The complete program of this project – Download
/*Ultrasonic radar system.
  *Run the following code a second time.
  *created by the SriTu Tech team.
  *Read the code below and use it for any of your creations.
*/
 
//import following libraries
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
 
Serial myport;//create serial object
 
PFont f;
int Angle, Distance;
String angle="";
String distance="";
String data;
int index1=0;
int index2=0;
float pixsDistance;
 
void setup() {
  size(1250, 700);//screen size
  smooth();
  printArray(PFont.list());//show font list your computer
  f = createFont("David Bold", 30);//font name and size
  textFont(f);
 
  String portName = Serial.list()[0];//set COM port
  myport = new Serial(this, portName, 9600);
  myport.bufferUntil('.');
}
 
void draw() {
 
  noStroke();
  fill(0, 10); 
  rect(0, 0, width, 700); 
  fill(98, 245, 31); 
  greenmesh();
  radararea();
  words();
  greenLine();
  redline();
}
//get ardunio board serial values
void serialEvent (Serial myport) { 
  data = myport.readStringUntil('.');
  data = data.substring(0, data.length()-1);
 
  index1 = data.indexOf(","); 
  angle= data.substring(0, index1); 
  distance= data.substring(index1+1, data.length()); 
 
  // converts the String variables into Integer
  Angle = int(angle);
  Distance = int(distance);
}
 
//Half circle and lines
void radararea() {
  pushMatrix();
  translate(625, 680); 
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  // draws the arc lines
  arc(0, 0, 1150, 1150, PI, TWO_PI);
  arc(0, 0, 850, 850, PI, TWO_PI);
  arc(0, 0, 550, 550, PI, TWO_PI);
  arc(0, 0, 250, 250, PI, TWO_PI);
  // draws the angle lines
  line(-450, 0, 450, 0);
  line(0, 0, -600*cos(radians(30)), -600*sin(radians(30)));
  line(0, 0, -600*cos(radians(60)), -600*sin(radians(60)));
  line(0, 0, -600*cos(radians(90)), -600*sin(radians(90)));
  line(0, 0, -600*cos(radians(120)), -600*sin(radians(120)));
  line(0, 0, -600*cos(radians(150)), -600*sin(radians(150)));
  line(-960*cos(radians(30)), 0, 960, 0);
  popMatrix();
}
 
//Green box net
void greenmesh() {
  stroke(98, 245, 31);
  strokeWeight(0.1);
  for (int x=0; x<=700; x+=5) {
    line(0, x, width, x);
  }
  for (int y=0; y<=1250; y+=5) {
    line(y, 0, y, height);
  }
}
 
//print text
void words() {
  fill(98, 245, 31);
  text("180'", 10, 670);
  fill(98, 245, 31);
  text("0'", 1210, 670);
  fill(98, 245, 31);
  text("30'", 1160, 380);
  fill(98, 245, 31);
  text("60'", 940, 160);
  fill(98, 245, 31);
  text("90'", 615, 70);
  fill(98, 245, 31);
  text("120'", 310, 150);
  fill(98, 245, 31);
  text("150'", 80, 370);
  fill(255);
  text("SriTu Tech Radar system", 20, 30);
  fill(255);
  text("Angle -- "+Angle+" '", 20, 60);
  fill(255);
  text("Distance -- "+Distance+" cm", 20, 90);
}
//Drawing green lines
void greenLine() {
  pushMatrix();
  strokeWeight(7);
  stroke(30, 250, 60);//green color
  translate(625, 680); 
  line(0, 0, 600*cos(radians(Angle)), -600*sin(radians(Angle))); 
  popMatrix();
}
//Drawing red lines
void redline() {
  pushMatrix();
  translate(625, 680); 
  strokeWeight(7);
  stroke(255, 10, 10); //red color
  pixsDistance = Distance*22.5; 
  // limiting the range to 40 cm
  if (Distance<40) {
    line(pixsDistance*cos(radians(Angle)), -pixsDistance*sin(radians(Angle)), 600*cos(radians(Angle)), -600*sin(radians(Angle)));
  }
  popMatrix();
}

Code explanation

First of all, there are three main libraries included. They are included in the Processing IDE itself.
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
 
Next, an object named “myport” is created to store the values received over the serial port.
Serial myport;//create serial object
 
Later, several variables were created to support the program.
PFont f;
int Angle, Distance;
String angle=””;
String distance=””;
String data;
int index1=0;
int index2=0;
float pixsDistance;
 
In the void setup the length and width of the radar system is set to be displayed. The fonts used for this design are also selected. Then we select the COM port connected to the Arduino board and the values received through it are included in the “myport” variable that we set earlier.
void setup() {
  size(1250, 700);//screen size
  smooth();
  printArray(PFont.list());//show font list your computer
  f = createFont(“David Bold”, 30);//font name and size
  textFont(f);
 
  String portName = Serial.list()[0];//set COM port
  myport = new Serial(this, portName, 9600);
  myport.bufferUntil(‘.’);
}
 
The radar system background design is done in the ‘void draw’ function. Each of these functions is described separately below.
void draw() {
 
  noStroke();
  fill(0, 10); 
  rect(0, 0, width, 700); 
  fill(98, 245, 31); 
  greenmesh();
  radararea();
  words();
  greenLine();
  redline();
}
 

greenmesh ();

The box net of the radar system is created by this code.
void greenmesh() {
  stroke(98, 245, 31);
  strokeWeight(0.1);
  for (int x=0; x<=700; x+=5) {
    line(0, x, width, x);
  }
  for (int y=0; y<=1250; y+=5) {
    line(y, 0, y, height);
  }
}
 

radararea ();

This creates the semicircle that the radar should display.
void radararea() {
  pushMatrix();
  translate(625, 680); 
  noFill();
  strokeWeight(2);
  stroke(98, 245, 31);
  // draws the arc lines
  arc(0, 0, 1150, 1150, PI, TWO_PI);
  arc(0, 0, 850, 850, PI, TWO_PI);
  arc(0, 0, 550, 550, PI, TWO_PI);
  arc(0, 0, 250, 250, PI, TWO_PI);
  // draws the angle lines
  line(-450, 0, 450, 0);
  line(0, 0, -600*cos(radians(30)), -600*sin(radians(30)));
  line(0, 0, -600*cos(radians(60)), -600*sin(radians(60)));
  line(0, 0, -600*cos(radians(90)), -600*sin(radians(90)));
  line(0, 0, -600*cos(radians(120)), -600*sin(radians(120)));
  line(0, 0, -600*cos(radians(150)), -600*sin(radians(150)));
  line(-960*cos(radians(30)), 0, 960, 0);
  popMatrix();
}
 
//Green box net
void greenmesh() {
  stroke(98, 245, 31);
  strokeWeight(0.1);
  for (int x=0; x<=700; x+=5) {
    line(0, x, width, x);
  }
  for (int y=0; y<=1250; y+=5) {
    line(y, 0, y, height);
  }
}
 

words ();

The letters and numbers here are set by this function.
void words() {
  fill(98, 245, 31);
  text(“180′”, 10, 670);
  fill(98, 245, 31);
  text(“0′”, 1210, 670);
  fill(98, 245, 31);
  text(“30′”, 1160, 380);
  fill(98, 245, 31);
  text(“60′”, 940, 160);
  fill(98, 245, 31);
  text(“90′”, 615, 70);
  fill(98, 245, 31);
  text(“120′”, 310, 150);
  fill(98, 245, 31);
  text(“150′”, 80, 370);
  fill(255);
  text(“SriTu Tech Radar system”, 20, 30);
  fill(255);
  text(“Angle — “+Angle+” ‘”, 20, 60);
  fill(255);
  text(“Distance — “+Distance+” cm”, 20, 90);
}
 

greenLine ();

This code creates Green lines that appear when radar is displayed.
void greenLine() {
  pushMatrix();
  strokeWeight(7);
  stroke(30, 250, 60);//green color
  translate(625, 680); 
  line(0, 0, 600*cos(radians(Angle)), -600*sin(radians(Angle))); 
  popMatrix();
}
 

redline ();

This code creates red lines that are drawn when identifying an object.
void redline() {
  pushMatrix();
  translate(625, 680); 
  strokeWeight(7);
  stroke(255, 10, 10); //red color
  pixsDistance = Distance*22.5; 
  // limiting the range to 40 cm
  if (Distance<40) {
    line(pixsDistance*cos(radians(Angle)), -pixsDistance*sin(radians(Angle)), 600*cos(radians(Angle)), -600*sin(radians(Angle)));
  }
  popMatrix();
}
 




Step 9

Well, now let us finally run this processing code.
In this case, remember to connect the project we made earlier to the computer. Now you can see the radar system we have created. Click the link or video below for the full video guide. We will meet in the next tutorial.

How to make a RADAR SYSTEM | Radar system with Arduino Nano

Similar Posts

Leave a Reply

Your email address will not be published.