Project showcase
Ultrasonic Ranging Using Arduino and Processing (Radar)

Ultrasonic Ranging Using Arduino and Processing (Radar)

This project detects the various obstacles and plots them on a graph.

  • 35,110 views
  • 16 comments
  • 55 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
ultrasonic sensor
×1
servo motor
×1

Apps and online services

About this project

Code

Arduino Code for RADARArduino
#include<Servo.h>
int trigPin=2;
int echoPin=3;

long duration;
int distance;
Servo servo;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  servo.attach(9);
}

void loop()
{
  for(int i=15;i<=165;i++)
  {
    servo.write(i);
    delay(100);
    distance=calculateDistance();
    
    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }
  for(int i=165;i>15;i--)
  {
    servo.write(i);
    delay(100);
    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;
}  
Processing Code for DisplayJava
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;


Serial myPort;
String angle="";
String distance="";
String data="";
String noObject;
float pixelsDistance;
int iAngle,iDistance;
int index1=0,index2=0;
PFont orcFont;

void setup()
{
  size(1496,900);
  smooth();
  myPort=new Serial(this,"COM3",9600);
  myPort.bufferUntil('.');
  orcFont=loadFont("OCRAExtended-30.vlw");
}

void draw()
{
  fill(98,245,31);
  textFont(orcFont);
  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(760,800);
  noFill();
  strokeWeight(2);
  stroke(98,245,31);
  arc(0,0,1400,1400,PI,TWO_PI);
  arc(0,0,1100,1100,PI,TWO_PI);
  arc(0,0,800,800,PI,TWO_PI);
  arc(0,0,500,500,PI,TWO_PI);
  line(-760,0,760,0);
  line(0,0,-760*cos(radians(30)),-760*sin(radians(30)));
  line(0,0,-760*cos(radians(60)),-760*sin(radians(60)));
  line(0,0,-760*cos(radians(90)),-760*sin(radians(90)));
  line(0,0,-760*cos(radians(120)),-760*sin(radians(120)));
  line(0,0,-760*cos(radians(150)),-760*sin(radians(150)));
  line(-760*cos(radians(30)),0,760,0);
  popMatrix();
}


void drawObject() {
  pushMatrix();
  translate(760,800); // moves the starting coordinats to new location
  strokeWeight(9);
  stroke(255,10,10); // red color
  pixelsDistance = iDistance*22.5; // covers the distance from the sensor from cm to pixels
  // limiting the range to 40 cms
  if(iDistance<40){
    // draws the object according to the angle and the distance
  line(pixelsDistance*cos(radians(iAngle)),-pixelsDistance*sin(radians(iAngle)),750*cos(radians(iAngle)),-750*sin(radians(iAngle)));
  }
  popMatrix();
}

void drawLine()
{
  pushMatrix();
  strokeWeight(9);
  stroke(30,250,60);
  translate(760,800);
  line(0,0,750*cos(radians(iAngle)),-750*sin(radians(iAngle)));
  popMatrix();
  
}

void drawText()
{
  pushMatrix();
  if(iDistance>40)
  {
    noObject="Out ofRange";
  }
  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(30);
  text("Object: " + noObject,15,100);
  text("Angle: " + iAngle +" °",400, 50);
  text("Distance: ", 13,50);
  if(iDistance<40) {
  text("        " + iDistance +" cm",30,50);
  }
  textSize(15);
  fill(98,245,60);
  translate(761+760*cos(radians(30)),782-760*sin(radians(30)));
  rotate(-radians(-60));
  text("30°",0,0);
  resetMatrix();
  translate(754+760*cos(radians(60)),784-760*sin(radians(60)));
  rotate(-radians(-30));
  text("60°",0,0);
  resetMatrix();
  translate(745+760*cos(radians(90)),790-760*sin(radians(90)));
  rotate(radians(0));
  text("90°",0,0);
  resetMatrix();
  translate(735+760*cos(radians(120)),803-760*sin(radians(120)));
  rotate(radians(-30));
  text("120°",0,0);
  resetMatrix();
  translate(740+760*cos(radians(150)),818-760*sin(radians(150)));
  rotate(radians(-60));
  text("150°",0,0);
  popMatrix();
}

Schematics

Schematic
Radar

Comments

Similar projects you might like

Ultrasonic Rangefinder with Processing

Project showcase by TEAM DIY

  • 10,017 views
  • 4 comments
  • 17 respects

Two Ultrasonic Sensor Arduino Radar - Continuous Rotation

Project showcase by Team MicroLab Greece

  • 10,259 views
  • 7 comments
  • 33 respects

Aurdino Radar With Processing

Project in progress by Akshay6766

  • 10,469 views
  • 4 comments
  • 34 respects

Auto Ultrasonic Car

by Ishaq Yang

  • 11,532 views
  • 2 comments
  • 13 respects

Create a Game with Arduino and Processing

Project showcase by Nicholas_N

  • 5,570 views
  • 3 comments
  • 5 respects
Add projectSign up / Login