Skip to main content

How To Make Radar With Arduino UNO Using Ultrasonic Sensor

 

How To Make Radar With Arduino UNO Using Ultrasonic Sensor

Creating a radar system with an Arduino UNO and an ultrasonic sensor involves a few straight forward steps. First, you'll need to connect the ultrasonic sensor's trigger pin to one of the Arduino’s digital output pins and the echo pin to a digital input pin. Then, write a simple Arduino sketch to send a pulse from the trigger pin and measure the duration of the pulse received on the echo pin. This duration, proportional to the distance of an object from the sensor, can be calculated using the speed of sound. By continuously taking readings and mapping them to distances, you can plot these measurements to visualize objects in your environment, effectively creating a basic radar system. To display the results, you could use a serial monitor or even integrate an LCD display for real-time feedback.


Here is the code below ,-

//radar.ino

#include <Servo.h> 


const int trigPin = 9;

const int echoPin = 8;


long duration;

int distance;


Servo myServo; 


void setup() {

  pinMode(trigPin, OUTPUT); 

  pinMode(echoPin, INPUT); 

  Serial.begin(9600);

  myServo.attach(6);

}

void loop() {

  

  for(int i=15;i<=165;i++){  

  myServo.write(i);

  delay(30);

  distance = calculateDistance();

  

  Serial.print(i); 

  Serial.print(","); 

  Serial.print(distance); 

  Serial.print("."); 

  }

  

  for(int i=165;i>15;i--){  

  myServo.write(i);

  delay(30);

  distance = calculateDistance();

  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;

}

arduino radar project diagram


Now you have to download the processing app from processing.org website. Then unzip and extract the file and open Processing.exe file and paste the code below and run.


//radar_view.pde


import processing.serial.*; 

import java.awt.event.KeyEvent; 

import java.io.IOException;


Serial myPort; 


String angle="";

String distance="";

String data="";

String noObject;

float pixsDistance;

int iAngle, iDistance;

int index1=0;

int index2=0;

PFont orcFont;


void setup() {

  

 size (1920, 1080);

 smooth();

 myPort = new Serial(this,"COM7", 9600); 

 myPort.bufferUntil('.'); 

}


void draw() {

  

  fill(98,245,31);

  

  noStroke();

  fill(0,4); 

  rect(0, 0, width, 1010); 

  

  fill(98,245,31); 

  drawRadar(); 

  drawLine();

  drawObject();

  drawText();

}


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()); 

  

  iAngle = int(angle);

  iDistance = int(distance);

}


void drawRadar() {

  pushMatrix();

  translate(960,1000);

  noFill();

  strokeWeight(2);

  stroke(98,245,31);

  arc(0,0,1800,1800,PI,TWO_PI);

  arc(0,0,1400,1400,PI,TWO_PI);

  arc(0,0,1000,1000,PI,TWO_PI);

  arc(0,0,600,600,PI,TWO_PI);

  line(-960,0,960,0);

  line(0,0,-960*cos(radians(30)),-960*sin(radians(30)));

  line(0,0,-960*cos(radians(60)),-960*sin(radians(60)));

  line(0,0,-960*cos(radians(90)),-960*sin(radians(90)));

  line(0,0,-960*cos(radians(120)),-960*sin(radians(120)));

  line(0,0,-960*cos(radians(150)),-960*sin(radians(150)));

  line(-960*cos(radians(30)),0,960,0);

  popMatrix();

}


void drawObject() {

  pushMatrix();

  translate(960,1000); 

  strokeWeight(9);

  stroke(255,10,10);

  pixsDistance = iDistance*22.5; 


  if(iDistance<60){

  line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),950*cos(radians(iAngle)),-950*sin(radians(iAngle)));

  }

  popMatrix();

}


void drawLine() {

  pushMatrix();

  strokeWeight(9);

  stroke(30,250,60);

  translate(960,1000); 

  line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)));

  popMatrix();

}


void drawText() { 

  

  pushMatrix();

  if(iDistance>40) {

  noObject = "Out of Range";

  }

  else {

  noObject = "In Range";

  }

  fill(0,0,0);

  noStroke();

  rect(0, 1010, width, 1080);

  fill(98,245,31);

  textSize(25);

  text("10cm",1180,990);

  text("20cm",1380,990);

  text("30cm",1580,990);

  text("40cm",1780,990);

  textSize(40);

  text("Object: " + noObject, 240, 1050);

  text("Angle: " + iAngle +" °", 1050, 1050);

  text("Distance: ", 1380, 1050);

  if(iDistance<40) {

  text("        " + iDistance +" cm", 1400, 1050);

  }

  textSize(25);

  fill(98,245,60);

  translate(961+960*cos(radians(30)),982-960*sin(radians(30)));

  rotate(-radians(-60));

  text("30°",0,0);

  resetMatrix();

  translate(954+960*cos(radians(60)),984-960*sin(radians(60)));

  rotate(-radians(-30));

  text("60°",0,0);

  resetMatrix();

  translate(945+960*cos(radians(90)),990-960*sin(radians(90)));

  rotate(radians(0));

  text("90°",0,0);

  resetMatrix();

  translate(935+960*cos(radians(120)),1003-960*sin(radians(120)));

  rotate(radians(-30));

  text("120°",0,0);

  resetMatrix();

  translate(940+960*cos(radians(150)),1018-960*sin(radians(150)));

  rotate(radians(-60));

  text("150°",0,0);

  popMatrix(); 

}

Comments

Popular posts from this blog

Obstacle Avoiding Robot Car Using Arduino - How To Make a Smart Car

How To Make Smart Robot Car With Arduino Obstacle avoiding robot car using Arduino, how to make a smart car with ultrasonic sensor, servo motor, and L298N motor driver module. Arduino UNO :- https://amzn.to/3MF9jky SG90 Servo Motor And HC-SR04 Ultrasonic Sensor :- https://amzn.to/3EX03pu LM298N motor driver module :- https://amzn.to/3y2tx3Y BO Motor with Wheel Pair :- https://amzn.to/38zFMu6 arduino smart car kit :- https://amzn.to/3kmtZlk Project Code :-  #include <Servo.h>          //Servo motor library. This is standard library #include <NewPing.h>        //Ultrasonic sensor function library. You must install this library //our L298N control pins const int LeftMotorForward = 7; const int LeftMotorBackward = 6; const int RightMotorForward = 4; const int RightMotorBackward = 5; //sensor pins #define trig_pin A1 //analog input 1 #define echo_pin A2 //analog input 2 #define maximum_distance 200 boolean goesForward = ...

How To Make Simple Digital Clock Using Arduino UNO Very Easy Step By Step

Digital clock using Arduino UNO , TM1637 4-digit 7 segment display and DS1307 RTC module. Here I show you how to make a simple digital clock using Arduino UNO very easily.  TM1637 4-digit 7 segment display :-  https://amzn.to/3R3KC3p DS1307 RTC module:-    https://amzn.to/3dPDfyc    Arduino UNO:- https://amzn.to/3KcNfxR Jumper Wire:-    https://amzn.to/3KcBIyH             Project Code :-  const int clock = 11; const int data = 10; uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f }; void setup() { setupInterrupt(); pinMode(clock, OUTPUT); pinMode(data, OUTPUT); start(); writeValue(0x8c); stop(); write(0x00, 0x00, 0x00, 0x00); } byte tcnt2; // set current time by editing the values at line 16 and 17 unsigned long int setMinutes = 9; // set minutes unsigned long int setHours = 9; // set hours unsigned long time = (setMinutes * 60 * 1000) + (setHours * 3600 *10...