Arduino Radar System

by Param785609 in Circuits > Arduino

126 Views, 0 Favorites, 0 Comments

Arduino Radar System

20250122_132233.jpg
20250114_142153.jpg

In this tutorial, I'll walk you through step by step how to assemble the components on a breadboard and how to program the Arduino board. We'll use the Arduino IDE to write the code that controls the servo motor and the ultrasonic sensor. By the end of this tutorial, you'll have a fully functional radar system that can detect objects and display their distance on the serial monitor.

Supplies

Capture.JPG

Here are the material you're going to need to make it: (click the words to find the link to where it can be bought)


1. Arduino UNO ($27.60)

2. 1 Servo Motor ($13.99)

3. 1 Breadboard ($7.65)

4. Ultrasonic Sensor ($13.99)

5. Jumper Wires ($9.99)

6. Some sort of device that that physically display the radar (price varies)

Glue It Down!

Screenshot 2025-01-22 003238.png
Screenshot 2025-01-22 003646.png
Screenshot 2025-01-22 003856.png
Screenshot 2025-01-22 003942.png

Glue down all the parts required for the breadboard:

  1. Stick the ultrasonic sensor to the servo motor. I advise this to be done with hot glue as it looks much cleaner rather than using something like tape.
  2. Now place that sensor/motor object to the breadboard, it doesn't matter which side the object is placed as the motor spins 360 degrees.
  3. Attach the Arduino UNO board on the breadboard. I would suggest not using hot glue for this as Arduino UNO's can be used for many purposes. Make sure the side with the port is place on the outside of the breadboard as suggested in the image.

Connections

CIRCUIT_DIAGRAM.png

Now connect all the wires using the diagram above:

Breadboard Connections:


Red Wire: Connect the VIN pin on the Arduino to the positive rail on the breadboard.

Black Wire: Connect the GND pin on the Arduino to the negative rail (ground) on the breadboard.


Servo Connections:


Blue Wire: Connect pin 12 on the Arduino to the orange wire (signal) of the servo.

Red Wire: Connect the positive rail (5V) on the breadboard to the red wire (power) of the servo.

Black Wire: Connect the negative rail (ground) on the breadboard to the brown wire (ground) of the servo.


Ultrasonic Sensor Connections:


Red Wire: Connect the VCC pin on the ultrasonic sensor to the positive rail (5V) on the breadboard.

Black Wire: Connect the GND pin on the ultrasonic sensor to the negative rail (ground) on the breadboard.

Purple Wire: Connect the Trig pin on the ultrasonic sensor to pin 11 on the Arduino.

Grey Wire: Connect the Echo pin on the ultrasonic sensor to pin 10 on the Arduino.





Arduino Code!

sCXSdHW5FTPfREOF.jpg
Screenshot 2025-01-22 012819.png

For this step you will required to download Arduino IDE. Click this to be redirected to the download page and instructions . Below is the code that will be run the Arduino. Open the application and paste the code in below:


// Includes the Servo library
#include <Servo.h>

// Defines Trig and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;

// Variables for the duration and the distance
long duration;
int distance;
Servo myServo; // Creates a servo object to control the servo motor

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts serial communication
myServo.attach(12); // Attaches the servo motor to pin 12
}

void loop() {
// Rotate the servo motor from 15 to 165 degrees
for (int i = 15; i <= 165; i += 3) { // Increase step size for faster movement
myServo.write(i);
delay(15); // Reduce delay for faster sweep
distance = calculateDistance(); // Calls a function to calculate distance

// Send data to Serial (angle and distance)
Serial.print(i); // Current angle
Serial.print(","); // Separator
Serial.print(distance); // Distance
Serial.println("."); // End marker for Processing to parse
}

// Rotate the servo motor back from 165 to 15 degrees
for (int i = 165; i >= 15; i -= 3) { // Increase step size for faster movement
myServo.write(i);
delay(15); // Reduce delay for faster sweep
distance = calculateDistance();

// Send data to Serial (angle and distance)
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.println(".");
}
}

// Function to calculate distance using the Ultrasonic sensor
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Trigger the ultrasonic pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);

// Calculate the distance in cm
distance = duration * 0.034 / 2;

// Constrain distance to a maximum value for stability
distance = constrain(distance, 0, 400);

return distance;
}


Go to the select port menu and select Arduino UNO.

Make sure you are aware of what port you're using (ex. COM2, COM3, COM4)

Verify the code just in case their is something wrong on your end.

Processing Code

Screenshot 2025-01-22 013443.png
Capture.JPG

For this next step you will need to download this processing software (Click this to be redirected to a page to download the software)

Using the code below you will have a visual representation of your radar's movement and what it detects.

import processing.serial.*; // Library for serial communication
Serial myPort; // Serial object

// Variables
String angle = "";
String distance = "";
String data = "";
String noObject = "Out of Range";
float pixsDistance = 0;
int iAngle = 0, iDistance = 0;
int index1 = 0;

// Constants
final int MAX_DISTANCE = 40; // Maximum range of the radar in cm
PFont orcFont;

void setup() {
size(1200, 700); // Set the screen resolution
smooth();
try {
myPort = new Serial(this, "COM3", 9600); // Start serial communication
myPort.bufferUntil('.'); // Read data until '.' character
} catch (Exception e) {
println("Error initializing serial port: " + e.getMessage());
}
frameRate(30); // Limit frame rate to optimize performance
}

void draw() {
fill(98, 245, 31);
// Simulating motion blur and slow fade of the moving line
noStroke();
fill(0, 8);
rect(0, 0, width, height - height * 0.065);

fill(98, 245, 31); // Green color
drawRadar(); // Draw the radar structure
drawLine(); // Draw the sweeping line
drawObject(); // Draw detected objects
drawText(); // Display text information
}

void serialEvent(Serial myPort) {
try {
// Read data from the Serial Port up to the character '.'
data = myPort.readStringUntil('.');
if (data != null && data.contains(",")) {
data = data.substring(0, data.length() - 1); // Remove the '.' at the end
index1 = data.indexOf(",");
angle = data.substring(0, index1);
distance = data.substring(index1 + 1);
iAngle = int(trim(angle)); // Ensure trimmed and valid
iDistance = int(trim(distance)); // Ensure trimmed and valid
}
} catch (Exception e) {
println("Error processing serial data: " + e.getMessage());
}
}

void drawRadar() {
pushMatrix();
translate(width / 2, height - height * 0.074); // Move the origin
noFill();
strokeWeight(2);
stroke(98, 245, 31);

// Draw arcs for radar range
arc(0, 0, width * 0.9375, width * 0.9375, PI, TWO_PI);
arc(0, 0, width * 0.73, width * 0.73, PI, TWO_PI);
arc(0, 0, width * 0.521, width * 0.521, PI, TWO_PI);
arc(0, 0, width * 0.313, width * 0.313, PI, TWO_PI);

// Draw angle lines
for (int angle = 30; angle <= 150; angle += 30) {
line(0, 0, (-width / 2) * cos(radians(angle)), (-width / 2) * sin(radians(angle)));
}
line(-width / 2, 0, width / 2, 0); // Draw horizontal line
popMatrix();
}

void drawObject() {
pushMatrix();
translate(width / 2, height - height * 0.074); // Move the origin
strokeWeight(5);
if (iDistance > 0 && iDistance <= MAX_DISTANCE) {
pixsDistance = iDistance * ((height - height * 0.1666) * 0.025); // Convert distance to pixels
stroke(255, 10, 10); // Red color for detected objects
line(
pixsDistance * cos(radians(iAngle)),
-pixsDistance * sin(radians(iAngle)),
pixsDistance * cos(radians(iAngle)) * 0.95,
-pixsDistance * sin(radians(iAngle)) * 0.95
);
noObject = "In Range";
} else {
noObject = "Out of Range";
}
popMatrix();
}

void drawLine() {
pushMatrix();
strokeWeight(2);
stroke(30, 250, 60); // Green color for the sweeping line
translate(width / 2, height - height * 0.074); // Move the origin
line(0, 0, (height - height * 0.12) * cos(radians(iAngle)), -(height - height * 0.12) * sin(radians(iAngle))); // Draw the line
popMatrix();
}

void drawText() {
pushMatrix();
fill(0);
noStroke();
rect(0, height - height * 0.0648, width, height); // Black background for text
fill(98, 245, 31);

// Display range labels
textSize(25);
text("10cm", width - width * 0.3854, height - height * 0.0833);
text("20cm", width - width * 0.281, height - height * 0.0833);
text("30cm", width - width * 0.177, height - height * 0.0833);
text("40cm", width - width * 0.0729, height - height * 0.0833);

// Display radar information
textSize(40);
text("SciCraft", width - width * 0.875, height - height * 0.0277);
text("Angle: " + iAngle + "°", width - width * 0.48, height - height * 0.0277);
text("Distance: ", width - width * 0.26, height - height * 0.0277);
text(noObject, width - width * 0.16, height - height * 0.0277);

if (iDistance > 0 && iDistance <= MAX_DISTANCE) {
text(iDistance + " cm", width - width * 0.225, height - height * 0.0277);
}
popMatrix();
}

Around the line 15-19 code, change the COM port to the one you connected to your device.


Now you can start the Arduino IDE and processing software and it should start working!

Make It Look Cool!

1000006131.jpg
1000006138.jpg
1000006139.jpg
1000006140.jpg
1000006141.jpg

Present your radar in a cool way! I made a tank, I used cardboard, tape, and a hot glue gun. The hole in the back is for the cable to run to the device.



Congrats, you've now made your very own Arduino Radar system!