Arduino Ultrasonic Radar
Its is radar made by using ultrasonic sensor.
Components and supplies
Jumper wires (generic)
Buzzer
Breadboard (generic)
5 mm LED: Red
Ultrasonic Sensor - HC-SR04 (Generic)
Arduino UNO
SG90 Micro-servo motor
Apps and platforms
Processing
Arduino IDE
Project description
Code
Code for Arduino IDE
c_cpp
if you face any problem just ask me
1// Includes the Servo library 2#include <Servo.h>. 3// Defines Tirg and Echo pins of the Ultrasonic Sensor 4const int trigPin = 3;//orange 5const int echoPin = 2;//red white - vcc grey -gnd 6// Variables for the duration and the distance 7long duration; 8int distance; 9Servo myServo; // Creates a servo object for controlling the servo motor 10void setup() { 11 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 12 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 13 pinMode(4,OUTPUT);//yellow 14 pinMode(11,OUTPUT); 15 Serial.begin(9600); 16 myServo.attach(9); // Defines on which pin is the servo motor attached on 9 purple 17} 18void loop() { 19 // rotates the servo motor from 15 to 165 degrees 20 for(int i=0;i<=180;i++){ 21 myServo.write(i); 22 delay(30); 23 distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree 24 25 Serial.print(i); // Sends the current degree into the Serial Port 26 Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 27 Serial.print(distance); // Sends the distance value into the Serial Port 28 Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 29 } 30 // Repeats the previous lines from 165 to 15 degrees 31 for(int i=180;i>0;i--){ 32 myServo.write(i); 33 delay(30); 34 distance = calculateDistance(); 35 Serial.print(i); 36 Serial.print(","); 37 Serial.print(distance); 38 Serial.print("."); 39 } 40} 41// Function for calculating the distance measured by the Ultrasonic sensor 42int calculateDistance(){ 43 44 digitalWrite(trigPin, LOW); 45 delayMicroseconds(2); 46 // Sets the trigPin on HIGH state for 10 micro seconds 47 digitalWrite(trigPin, HIGH); 48 delayMicroseconds(10); 49 digitalWrite(trigPin, LOW); 50 duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds 51 distance= duration*0.034/2; 52 if (distance < 40) 53 { 54 digitalWrite(4,HIGH); 55 digitalWrite(11,HIGH); 56 57 } 58 else 59 { 60 digitalWrite(4,LOW); 61 digitalWrite(11,LOW); 62 } 63 64 65 66} 67
Code for Processing App
java
Just paste the code in the processing app and dont forget to change the com you are using and screen resolution of your pc.
1/This Code is Written By Namanbir/ 2import processing.serial.*; // imports library for serial communication 3import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 4import java.io.IOException; 5Serial myPort; // defines Object Serial 6// defubes variables 7String angle=""; 8String distance=""; 9String data=""; 10String noObject; 11float pixsDistance; 12int iAngle, iDistance; 13int index1=0; 14int index2=0; 15PFont orcFont; 16void setup() { 17 18 size (1200, 700); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION*** 19 smooth(); 20 myPort = new Serial(this,"COM3", 9600); // starts the serial communication 21 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 22} 23void draw() { 24 25 fill(98,245,31); 26 // simulating motion blur and slow fade of the moving line 27 noStroke(); 28 fill(0,4); 29 rect(0, 0, width, height-height*0.065); 30 31 fill(98,245,31); // green color 32 // calls the functions for drawing the radar 33 drawRadar(); 34 drawLine(); 35 drawObject(); 36 drawText(); 37} 38void serialEvent (Serial myPort) { // starts reading data from the Serial Port 39 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 40 data = myPort.readStringUntil('.'); 41 data = data.substring(0,data.length()-1); 42 43 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 44 angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port 45 distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance 46 47 // converts the String variables into Integer 48 iAngle = int(angle); 49 iDistance = int(distance); 50} 51void drawRadar() { 52 pushMatrix(); 53 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 54 noFill(); 55 strokeWeight(2); 56 stroke(98,245,31); 57 // draws the arc lines 58 arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI); 59 arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI); 60 arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI); 61 arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI); 62 // draws the angle lines 63 line(-width/2,0,width/2,0); 64 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 65 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 66 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 67 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 68 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 69 line((-width/2)*cos(radians(30)),0,width/2,0); 70 popMatrix(); 71} 72void drawObject() { 73 pushMatrix(); 74 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 75 strokeWeight(9); 76 stroke(255,10,10); // red color 77 pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels 78 // limiting the range to 40 cms 79 if(iDistance<40){ 80 // draws the object according to the angle and the distance 81 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle))); 82 } 83 popMatrix(); 84} 85void drawLine() { 86 pushMatrix(); 87 strokeWeight(9); 88 stroke(30,250,60); 89 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 90 line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle 91 popMatrix(); 92} 93void drawText() { // draws the texts on the screen 94 95 pushMatrix(); 96 if(iDistance>40) { 97 noObject = "Out of Range"; 98 } 99 else { 100 noObject = "In Range"; 101 } 102 fill(0,0,0); 103 noStroke(); 104 rect(0, height-height*0.0648, width, height); 105 fill(98,245,31); 106 textSize(25); 107 108 text("10cm",width-width*0.3854,height-height*0.0833); 109 text("20cm",width-width*0.281,height-height*0.0833); 110 text("30cm",width-width*0.177,height-height*0.0833); 111 text("40cm",width-width*0.0729,height-height*0.0833); 112 textSize(40); 113 text("Namanbir ", width-width*0.875, height-height*0.0277); 114 text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277); 115 text("Distance: ", width-width*0.26, height-height*0.0277); 116 if(iDistance<40) { 117 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 118 } 119 textSize(25); 120 fill(98,245,60); 121 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 122 rotate(-radians(-60)); 123 text("30°",0,0); 124 resetMatrix(); 125 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 126 rotate(-radians(-30)); 127 text("60°",0,0); 128 resetMatrix(); 129 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 130 rotate(radians(0)); 131 text("90°",0,0); 132 resetMatrix(); 133 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 134 rotate(radians(-30)); 135 text("120°",0,0); 136 resetMatrix(); 137 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 138 rotate(radians(-60)); 139 text("150°",0,0); 140 popMatrix(); 141}
Code for Arduino IDE
c_cpp
if you face any problem just ask me
1// Includes the Servo library 2#include <Servo.h>. 3// Defines Tirg and Echo pins of the Ultrasonic Sensor 4const int trigPin = 3;//orange 5const int echoPin = 2;//red white - vcc grey -gnd 6// Variables for the duration and the distance 7long duration; 8int distance; 9Servo myServo; // Creates a servo object for controlling the servo motor 10void setup() { 11 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 12 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 13 pinMode(4,OUTPUT);//yellow 14 pinMode(11,OUTPUT); 15 Serial.begin(9600); 16 myServo.attach(9); // Defines on which pin is the servo motor attached on 9 purple 17} 18void loop() { 19 // rotates the servo motor from 15 to 165 degrees 20 for(int i=0;i<=180;i++){ 21 myServo.write(i); 22 delay(30); 23 distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree 24 25 Serial.print(i); // Sends the current degree into the Serial Port 26 Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 27 Serial.print(distance); // Sends the distance value into the Serial Port 28 Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing 29 } 30 // Repeats the previous lines from 165 to 15 degrees 31 for(int i=180;i>0;i--){ 32 myServo.write(i); 33 delay(30); 34 distance = calculateDistance(); 35 Serial.print(i); 36 Serial.print(","); 37 Serial.print(distance); 38 Serial.print("."); 39 } 40} 41// Function for calculating the distance measured by the Ultrasonic sensor 42int calculateDistance(){ 43 44 digitalWrite(trigPin, LOW); 45 delayMicroseconds(2); 46 // Sets the trigPin on HIGH state for 10 micro seconds 47 digitalWrite(trigPin, HIGH); 48 delayMicroseconds(10); 49 digitalWrite(trigPin, LOW); 50 duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds 51 distance= duration*0.034/2; 52 if (distance < 40) 53 { 54 digitalWrite(4,HIGH); 55 digitalWrite(11,HIGH); 56 57 } 58 else 59 { 60 digitalWrite(4,LOW); 61 digitalWrite(11,LOW); 62 } 63 64 65 66} 67
Code for Processing App
java
Just paste the code in the processing app and dont forget to change the com you are using and screen resolution of your pc.
1/This Code is Written By Namanbir/ 2import processing.serial.*; // imports library for serial communication 3import java.awt.event.KeyEvent; // imports library for reading the data from the serial port 4import java.io.IOException; 5Serial myPort; // defines Object Serial 6// defubes variables 7String angle=""; 8String distance=""; 9String data=""; 10String noObject; 11float pixsDistance; 12int iAngle, iDistance; 13int index1=0; 14int index2=0; 15PFont orcFont; 16void setup() { 17 18 size (1200, 700); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION*** 19 smooth(); 20 myPort = new Serial(this,"COM3", 9600); // starts the serial communication 21 myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance. 22} 23void draw() { 24 25 fill(98,245,31); 26 // simulating motion blur and slow fade of the moving line 27 noStroke(); 28 fill(0,4); 29 rect(0, 0, width, height-height*0.065); 30 31 fill(98,245,31); // green color 32 // calls the functions for drawing the radar 33 drawRadar(); 34 drawLine(); 35 drawObject(); 36 drawText(); 37} 38void serialEvent (Serial myPort) { // starts reading data from the Serial Port 39 // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data". 40 data = myPort.readStringUntil('.'); 41 data = data.substring(0,data.length()-1); 42 43 index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1" 44 angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port 45 distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance 46 47 // converts the String variables into Integer 48 iAngle = int(angle); 49 iDistance = int(distance); 50} 51void drawRadar() { 52 pushMatrix(); 53 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 54 noFill(); 55 strokeWeight(2); 56 stroke(98,245,31); 57 // draws the arc lines 58 arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI); 59 arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI); 60 arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI); 61 arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI); 62 // draws the angle lines 63 line(-width/2,0,width/2,0); 64 line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30))); 65 line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60))); 66 line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90))); 67 line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120))); 68 line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150))); 69 line((-width/2)*cos(radians(30)),0,width/2,0); 70 popMatrix(); 71} 72void drawObject() { 73 pushMatrix(); 74 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 75 strokeWeight(9); 76 stroke(255,10,10); // red color 77 pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels 78 // limiting the range to 40 cms 79 if(iDistance<40){ 80 // draws the object according to the angle and the distance 81 line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle))); 82 } 83 popMatrix(); 84} 85void drawLine() { 86 pushMatrix(); 87 strokeWeight(9); 88 stroke(30,250,60); 89 translate(width/2,height-height*0.074); // moves the starting coordinats to new location 90 line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle 91 popMatrix(); 92} 93void drawText() { // draws the texts on the screen 94 95 pushMatrix(); 96 if(iDistance>40) { 97 noObject = "Out of Range"; 98 } 99 else { 100 noObject = "In Range"; 101 } 102 fill(0,0,0); 103 noStroke(); 104 rect(0, height-height*0.0648, width, height); 105 fill(98,245,31); 106 textSize(25); 107 108 text("10cm",width-width*0.3854,height-height*0.0833); 109 text("20cm",width-width*0.281,height-height*0.0833); 110 text("30cm",width-width*0.177,height-height*0.0833); 111 text("40cm",width-width*0.0729,height-height*0.0833); 112 textSize(40); 113 text("Namanbir ", width-width*0.875, height-height*0.0277); 114 text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277); 115 text("Distance: ", width-width*0.26, height-height*0.0277); 116 if(iDistance<40) { 117 text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277); 118 } 119 textSize(25); 120 fill(98,245,60); 121 translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30))); 122 rotate(-radians(-60)); 123 text("30°",0,0); 124 resetMatrix(); 125 translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60))); 126 rotate(-radians(-30)); 127 text("60°",0,0); 128 resetMatrix(); 129 translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90))); 130 rotate(radians(0)); 131 text("90°",0,0); 132 resetMatrix(); 133 translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120))); 134 rotate(radians(-30)); 135 text("120°",0,0); 136 resetMatrix(); 137 translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150))); 138 rotate(radians(-60)); 139 text("150°",0,0); 140 popMatrix(); 141}
Downloadable files
Circuit Diagram of Radar
Circuit Diagram of Radar
screenshot_(18)_Sclt6vXYwv.png
screenshot_(18)_Sclt6vXYwv.png

Circuit Diagram
These are some screenshots from fritzing.
Circuit Diagram

screenshot_(18)_Sclt6vXYwv.png
screenshot_(18)_Sclt6vXYwv.png

Circuit Diagram of Radar
Circuit Diagram of Radar
Comments
Only logged in users can leave comments