-->

In

Arduino Radar Project

 In this Arduino Tutorial I will show you how you can make this cool looking radar using the Arduino Board and the Processing Development Environment. You can read the written tutorial below for more details.


Components needed for this Arduino Project


You can get these components from any of the sites below:

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.


Circuit Schematics


I connected the Ultrasonic Sensor HC-SR04 to the pins number 10 and 11 and the servo motor to the pin number 12 on the Arduino Board.

Arduino-Radar-Circuit-Schematics

Source codes


Now we need to make a code and upload it to the Arduino Board that will enable the interaction between the Arduino and the Processing IDE.  For understanding how the connection works click here to visit my Arduino and Processing Tutorial.

 Here’s the Arduino Source Code with description of each line of the code:

  1. // Includes the Servo library
  2. #include <Servo.h>.
  3. // Defines Tirg and Echo pins of the Ultrasonic Sensor
  4. const int trigPin = 10;
  5. const int echoPin = 11;
  6. // Variables for the duration and the distance
  7. long duration;
  8. int distance;
  9. Servo myServo; // Creates a servo object for controlling the servo motor
  10. void setup() {
  11. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  12. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  13. Serial.begin(9600);
  14. myServo.attach(12); // Defines on which pin is the servo motor attached
  15. }
  16. void loop() {
  17. // rotates the servo motor from 15 to 165 degrees
  18. for(int i=15;i<=165;i++){
  19. myServo.write(i);
  20. delay(30);
  21. distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
  22. Serial.print(i); // Sends the current degree into the Serial Port
  23. Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  24. Serial.print(distance); // Sends the distance value into the Serial Port
  25. Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  26. }
  27. // Repeats the previous lines from 165 to 15 degrees
  28. for(int i=165;i>15;i--){
  29. myServo.write(i);
  30. delay(30);
  31. distance = calculateDistance();
  32. Serial.print(i);
  33. Serial.print(",");
  34. Serial.print(distance);
  35. Serial.print(".");
  36. }
  37. }
  38. // Function for calculating the distance measured by the Ultrasonic sensor
  39. int calculateDistance(){
  40. digitalWrite(trigPin, LOW);
  41. delayMicroseconds(2);
  42. // Sets the trigPin on HIGH state for 10 micro seconds
  43. digitalWrite(trigPin, HIGH);
  44. delayMicroseconds(10);
  45. digitalWrite(trigPin, LOW);
  46. duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  47. distance= duration*0.034/2;
  48. return distance;
  49. }



Here’s the complete Processing Source Code of the Arduino Radar:
  1. /* Arduino Radar Project
  2. *
  3. * Updated version. Fits any screen resolution!
  4. * Just change the values in the size() function,
  5. * with your screen resolution.
  6. *
  7. * by Dejan Nedelkovski,
  8. * www.HowToMechatronics.com
  9. *
  10. */
  11. import processing.serial.*; // imports library for serial communication
  12. import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
  13. import java.io.IOException;
  14. Serial myPort; // defines Object Serial
  15. // defubes variables
  16. String angle="";
  17. String distance="";
  18. String data="";
  19. String noObject;
  20. float pixsDistance;
  21. int iAngle, iDistance;
  22. int index1=0;
  23. int index2=0;
  24. PFont orcFont;
  25. void setup() {
  26. size (1920, 1080); // ***CHANGE THIS TO YOUR SCREEN RESOLUTION***
  27. smooth();
  28. myPort = new Serial(this,"COM4", 9600); // starts the serial communication
  29. myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
  30. orcFont = loadFont("OCRAExtended-30.vlw");
  31. }
  32. void draw() {
  33. fill(98,245,31);
  34. textFont(orcFont);
  35. // simulating motion blur and slow fade of the moving line
  36. noStroke();
  37. fill(0,4);
  38. rect(0, 0, width, height-height*0.065);
  39. fill(98,245,31); // green color
  40. // calls the functions for drawing the radar
  41. drawRadar();
  42. drawLine();
  43. drawObject();
  44. drawText();
  45. }
  46. void serialEvent (Serial myPort) { // starts reading data from the Serial Port
  47. // reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
  48. data = myPort.readStringUntil('.');
  49. data = data.substring(0,data.length()-1);
  50. index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
  51. 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
  52. 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
  53. // converts the String variables into Integer
  54. iAngle = int(angle);
  55. iDistance = int(distance);
  56. }
  57. void drawRadar() {
  58. pushMatrix();
  59. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  60. noFill();
  61. strokeWeight(2);
  62. stroke(98,245,31);
  63. // draws the arc lines
  64. arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
  65. arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
  66. arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
  67. arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
  68. // draws the angle lines
  69. line(-width/2,0,width/2,0);
  70. line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
  71. line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
  72. line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
  73. line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
  74. line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
  75. line((-width/2)*cos(radians(30)),0,width/2,0);
  76. popMatrix();
  77. }
  78. void drawObject() {
  79. pushMatrix();
  80. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  81. strokeWeight(9);
  82. stroke(255,10,10); // red color
  83. pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels
  84. // limiting the range to 40 cms
  85. if(iDistance<40){
  86. // draws the object according to the angle and the distance
  87. line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
  88. }
  89. popMatrix();
  90. }
  91. void drawLine() {
  92. pushMatrix();
  93. strokeWeight(9);
  94. stroke(30,250,60);
  95. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  96. line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle
  97. popMatrix();
  98. }
  99. void drawText() { // draws the texts on the screen
  100. pushMatrix();
  101. if(iDistance>40) {
  102. noObject = "Out of Range";
  103. }
  104. else {
  105. noObject = "In Range";
  106. }
  107. fill(0,0,0);
  108. noStroke();
  109. rect(0, height-height*0.0648, width, height);
  110. fill(98,245,31);
  111. textSize(25);
  112. text("10cm",width-width*0.3854,height-height*0.0833);
  113. text("20cm",width-width*0.281,height-height*0.0833);
  114. text("30cm",width-width*0.177,height-height*0.0833);
  115. text("40cm",width-width*0.0729,height-height*0.0833);
  116. textSize(40);
  117. text("Object: " + noObject, width-width*0.875, height-height*0.0277);
  118. text("Angle: " + iAngle +" °", width-width*0.48, height-height*0.0277);
  119. text("Distance: ", width-width*0.26, height-height*0.0277);
  120. if(iDistance<40) {
  121. text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277);
  122. }
  123. textSize(25);
  124. fill(98,245,60);
  125. translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
  126. rotate(-radians(-60));
  127. text("30°",0,0);
  128. resetMatrix();
  129. translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
  130. rotate(-radians(-30));
  131. text("60°",0,0);
  132. resetMatrix();
  133. translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
  134. rotate(radians(0));
  135. text("90°",0,0);
  136. resetMatrix();
  137. translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
  138. rotate(radians(-30));
  139. text("120°",0,0);
  140. resetMatrix();
  141. translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
  142. rotate(radians(-60));
  143. text("150°",0,0);
  144. popMatrix();
  145. }

Related Articles

0 comments:

Post a Comment

Search This Blog

Powered by Blogger.
  • ()

Followers