In this project I will show you how to make an Arduino Radar yourself.
This is an intermediate / advanced project.
When your project is assembled, objects up to 50 cm from the sensor will display. If you change the code, the radar can detect objects up to 5 meters.
For this project you need two different programs Arduino IDE en Processing. The language that Processing uses is Java, so keep this in mind.
- Level - Advanced / Expert 65%
- Duration - 30/45 Min 40%
- Costs - € 49.75 euros complete 40%
Step 1: Requirements
1X Arduino UNO
1X breadboard
4X m / m jumper cables
4X f / m jumper cables
1X 180 Servo
1X HC-SR04
1X HC-SR04 Holder
Step 2: Building and Wiring
Now we are going to put the project together.
You start by sending the 5V from the Arduino to the breadboard. Then you go from the breadboard to the plus port of the servo and the sensor.
Once you've done that, go from the GND of the servo and the sensor to the GND of the Arduino.
Finally, connect the servo control cable to pin 9, the echo pin to pin 6 and the trigpin to pin 7.
Step 3: Programming
Now that you're done wiring and building your Arduino radar, you can start programming the Arduino Uno.
For this code we have to insert an include file.
A Library or Include file often contains code that can often be reused in other Sketches. Often libraries (libraries) are a collection of related functions aimed at a particular application. For example to control a strip with LEDs, special mathematical functions or, for example, to control an LCD screen.
For this project we use the include servo file, if you don't put it in then you can't control the Arduino servo and then your sonar will not work.
You can copy and paste the code below. We recommend that you copy the code yourself as you learn more from it.
leftRightServo.write (leftRightPos);
for (index = 0, index <= numReadings, index ++) {
digitalWrite (trigPin, LOW);
delayMicroseconds (2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin, LOW);
pulseTime = pulseIn (echoPin, HIGH);
distance = pulseTime / 13;
total = total + distance;
delay (10);
}
average = total / numReadings;
if (index> = numReadings) {
index = 0;
total = 0;
}
if (average> = 300) {
average = 300;
}
Serial.print (“X”);
Serial.print (leftRightPos);
Serial.print (“V”);
Serial.println (average);
}
for (leftRightPos = 180; leftRightPos> 0; leftRightPos–) {
leftRightServo.write (leftRightPos);
for (index = 0, index <= numReadings, index ++) {
digitalWrite (trigPin, LOW);
delayMicroseconds (2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin, LOW);
pulseTime = pulseIn (echoPin, HIGH);
distance = pulseTime / 13;
total = total + distance;
delay (10);
}
average = total / numReadings;
if (index> = numReadings) {
index = 0;
total = 0;
}
if (average> = 300) {
average = 300;
}
Serial.print (“X”);
Serial.print (leftRightPos);
Serial.print (“V”);
Serial.println (average);
}
}
After entering the code in the Arduino IDE you can enter the code below in Processing.
import processing.serial. *;
Serial bluestone port;
float x, y;
int radius = 350;
int w = 300;
int degree = 0;
int value = 0;
intmotion = 0;
int [] newValue = new int [181];
int [] oldValue = new int [181];
PFont myFont;
int radarDist = 0;
int firstRun = 0;
void setup () {
size (750, 450);
background(0);
myFont = createFont ("verdana", 12);
textFont (myFont);
arduinoport = new Serial (this, “COM10”, 9600);
}
void draw () {
fill (0);
noStroke ();
ellipse (radius, radius, 750, 750);
rectMode (CENTER);
rect (350,402,800,100);
if (degree> = 179) {
motion = 1;
}
if (degree <= 1) {
motion = 0;
}
strokeWeight (7);
if(motion == 0) {
for (int i = 0; i <= 20; i ++) {
stroke (0, (10 * i), 0);
line (radius, radius, radius + cos (radians (degree + (180 + i))) * w, radius + sin (radians (degree + (180 + i))) * w);
}
} Else {
for (int i = 20, i> = 0, i–) {
stroke (0,200- (10 * i), 0);
line (radius, radius, radius + cos (radians (degree + (180 + i))) * w, radius + sin (radians (degree + (180 + i))) * w);
}
}
noStroke ();
fill (0,50,0);
beginShape ();
for (int i = 0; i <180; i ++) {
x = radius + cos (radians ((180 + i))) * ((oldValue [i]));
y = radius + sin (radians ((180 + i))) * ((oldValue [i]));
vertex (x, y);
}
endShape ();
fill (0,110,0);
beginShape ();
for (int i = 0; i <180; i ++) {
x = radius + cos (radians ((180 + i))) * (newValue [i]);
y = radius + sin (radians ((180 + i))) * (newValue [i]);
vertex (x, y);
}
endShape ();
fill (0,170,0);
beginShape ();
for (int i = 0; i <180; i ++) {
x = radius + cos (radians ((180 + i))) * ((newValue [i] + oldValue [i]) / 2);
y = radius + sin (radians ((180 + i))) * ((newValue [i] + oldValue [i]) / 2);
vertex (x, y);
}
endShape ();
if (firstRun> = 360) {
stroke (150,0,0);
strokeWeight (1);
noFill ();
for (int i = 0; i <180; i ++) {
if (oldValue [i] - newValue [i]> 35 || newValue [i] - oldValue [i]> 35) {
x = radius + cos (radians ((180 + i))) * (newValue [i]);
y = radius + sin (radians ((180 + i))) * (newValue [i]);
ellipse (x, y, 10, 10);
}
}
}
for (int i = 0; i <= 5; i ++) {
noFill ();
strokeWeight (1);
stroke (0- (255 * i), 30);
ellipse (radius, radius, (120 * i), (120 * i));
fill (0, 100, 0);
}
for (int i = 0; i <= 6; i ++) {
strokeWeight (1);
stroke (0, 55);
line (radius, radius, radius + cos (radians (180+ (30 * i))) * w, radius + sin (radians (180+ (30 * i))) * w);
fill (0, 55, 0);
noStroke ();
if (180+ (30 * i)> = 300) {
text (Integer.toString (180+ (30 * i)), (radius + 10) + cos (radians (180+ (30 * i)))) * (w + 10), (radius + 10) + sin (radians (180+ (30 * i))) * (w + 10), 25,50);
} Else {
text (Integer.toString (180+ (30 * i))), radius + cos (radians (180+ (30 * i))) * w, radius + sin (radians (180+ (30 * i))) * w , 60,40);
}
}
noStroke ();
fill (0);
rect (350,402,800,100);
fill (0, 100, 0);
text ("Degrees:" + Integer.toString (degree), 100, 380, 100, 50);
text ("Radar screen code", 540, 380, 250, 50);
fill (0);
rect (70,60,150,100);
fill (0, 100, 0);
text ("Screen Key:", 100, 50, 150, 50);
fill (0,50,0);
rect (30,53,10,10);
text ("First sweep", 115, 70, 150, 50);
fill (0,110,0);
rect (30,73,10,10);
text ("Second sweep", 115, 90, 150, 50);
fill (0,170,0);
rect (30,93,10,10);
text ("Average", 115, 110, 150, 50);
fill (0, 255, 0);
text ("10", 455, 375, 200, 200);
fill (0, 255, 0);
text ("20", 455, 310, 200, 200);
fill (0, 255, 0);
text ("30", 455, 250, 200, 200);
fill (0, 255, 0);
text ("40", 455, 190, 200, 200);
fill (0, 255, 0);
text ("50", 455, 130, 200, 200);
}
void serialEvent(Serial arduinoport) {
String xString = arduinoport.readStringUntil ('\ n');
if (xString! = null) {
xString = trim (xString);
String getX = xString.substring (1, xString.indexOf (“V”));
String getV = xString.substring (xString.indexOf (“V”) + 1, xString.length ());
degree = Integer.parseInt (getX);
value = Integer.parseInt (getV);
oldValue [degree] = newValue [degree];
newValue [degree] = value;
firstRun ++;
if (firstRun> 330) {
firstRun = 333;
}
}
}
This is sonar, not radar!