How to make a DIY Radar System using an Arduino and an Ultrasonic Sensor

How to make a DIY Radar System using an Arduino and an Ultrasonic Sensor

Hello and welcome back! In this project, we will learn how to make a DIY radar system using an Arduino and an ultrasonic sensor. In this system, the ultrasonic sensor is used to measure the distance of objects, and I used a simple mechanism to do that. For this, I used a servo motor to rotate the ultrasonic sensor left and right. Also, I used an OLED display for the radar visualization. It shows the scanning motion and the detected objects in real time. In addition, I used LEDs and a buzzer to indicate the detected objects. When an object comes closer, the LEDs will light up, and the buzzer will give an alert.

Especially, I have designed a custom PCB with JLCPCB for this project. Because of that, we don’t need extra wires or complex connections. Also, you can expand this project as you like. For example, you can increase the scanning angle, improve the display design, or even connect it to your computer and make a processing code for radar visualization. You can change them as you like.

Ok, let’s do this project step by step. The required components are given below.

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

Step 1

Firstly, identify these components.

Step 2

Secondly, let’s order PCBs. For that, follow the instructions below.

  • Click the “Instant Quote” button and upload the Gerber file, which you can download from the link below.
  • Gerber file – Download
  • For this project, I ordered five Green PCBs. Next, select the build time and shipping method. Finally, click “Save to Cart” and complete the payment.

Step 3

Thirdly, unbox your PCB package.

Step 4

Now, solder the components on the PCB.

Step 5

Next, connect the Arduino Nano board, ultrasonic sensor, OLED display, and servo motor to the PCB properly. After that, connect the Arduino board to your computer.

Step 6

Now, copy and paste the following program into the Arduino IDE. Also, make sure to install the SSD1306 library file.

#include <Servo.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Pins
#define Buzzer 4
#define RLed 3
#define YLed 2
#define ServoPin 9
#define trig 5
#define echo 6

Servo radarServo;

long duration;
int distance;
int angle;

// Trail system
#define TRAIL_POINTS 8
int trailX[TRAIL_POINTS];
int trailY[TRAIL_POINTS];


// ================= STARTUP SCREEN =================
void startupScreen() {

  display.clearDisplay();

  // Title
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10, 10);
  display.print("RADAR");

  display.setCursor(10, 30);
  display.print("SYSTEM");
  display.display();
  delay(1500);

  // Loading bar
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(20, 20);
  display.print("Initializing...");

  for(int i = 0; i <= 100; i += 10) {
    display.drawRect(14, 40, 100, 10, WHITE);
    display.fillRect(14, 40, i, 10, WHITE);
    display.display();
    delay(120);
  }

  delay(500);

  // Ready message
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(15, 20);
  display.print("READY");
  display.display();
  delay(1200);

  // Intro sweep
  display.clearDisplay();
  for(int a = 0; a <= 180; a += 10) {
    display.clearDisplay();

    float rad = a * 3.14 / 180;
    int x = 64 + 50 * cos(rad);
    int y = 63 - 50 * sin(rad);

    display.drawLine(64, 63, x, y, WHITE);
    display.display();

    delay(30);
  }

  display.clearDisplay();
}


// ================= DISTANCE FUNCTION =================
int getDistance() {
  digitalWrite(trig, LOW);
  delayMicroseconds(2);

  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  return duration * 0.034 / 2;
}


// ================= TRAIL SYSTEM =================
void updateTrail(int x, int y) {
  for(int i = TRAIL_POINTS - 1; i > 0; i--) {
    trailX[i] = trailX[i - 1];
    trailY[i] = trailY[i - 1];
  }
  trailX[0] = x;
  trailY[0] = y;
}


// ================= RADAR DRAW =================
void drawRadar(int angle, int distance) {

  display.clearDisplay();

  // Radar arcs
  display.drawCircle(64, 63, 20, WHITE);
  display.drawCircle(64, 63, 40, WHITE);
  display.drawCircle(64, 63, 60, WHITE);

  float rad = angle * 3.14 / 180;

  int x = 64 + 60 * cos(rad);
  int y = 63 - 60 * sin(rad);

  // Sweep line
  display.drawLine(64, 63, x, y, WHITE);

  // Object detection
  if(distance < 60) {
    int objX = 64 + distance * cos(rad);
    int objY = 63 - distance * sin(rad);

    updateTrail(objX, objY);
  }

  // Draw trail
  for(int i = TRAIL_POINTS - 1; i >= 0; i--) {
    if(trailX[i] != -1) {
      display.drawPixel(trailX[i], trailY[i], WHITE);
    }
  }

  // Current object
  if(trailX[0] != -1) {
    display.fillCircle(trailX[0], trailY[0], 2, WHITE);
  }

  // Top info bar
  display.fillRect(0, 0, 128, 16, BLACK);

  display.setTextSize(1);
  display.setTextColor(WHITE);

  display.setCursor(0, 0);
  display.print("A:");
  display.print(angle);

  display.setCursor(64, 0);
  display.print("D:");
  display.print(distance);
  display.print("cm");

  display.display();
}


// ================= ALERT SYSTEM =================
void alertSystem(int distance) {
  if(distance < 20) {
    digitalWrite(RLed, HIGH);
    digitalWrite(YLed, LOW);
    tone(Buzzer, 1200);
  } 
  else if(distance < 50) {
    digitalWrite(YLed, HIGH);
    digitalWrite(RLed, LOW);
    noTone(Buzzer);
  } 
  else {
    digitalWrite(RLed, LOW);
    digitalWrite(YLed, LOW);
    noTone(Buzzer);
  }
}

void setup() {

  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  pinMode(Buzzer, OUTPUT);
  pinMode(RLed, OUTPUT);
  pinMode(YLed, OUTPUT);

  radarServo.attach(ServoPin);

  Serial.begin(9600);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();

  // Initialize trail
  for(int i = 0; i < TRAIL_POINTS; i++) {
    trailX[i] = -1;
    trailY[i] = -1;
  }

  // Run startup animation
  startupScreen();
}

void loop() {

  // LEFT → RIGHT
  for(angle = 0; angle <= 180; angle++) {
    radarServo.write(angle);
    delay(15);

    distance = getDistance();

    drawRadar(angle, distance);
    alertSystem(distance);
  }

  // RIGHT → LEFT
  for(angle = 180; angle >= 0; angle--) {
    radarServo.write(angle);
    delay(15);

    distance = getDistance();

    drawRadar(angle, distance);
    alertSystem(distance);
  }
}
  • Next, select the correct board and port in the Arduino IDE. Then, click the upload button.

Step 7

Afterward, remove the USB cable and make a stand for the project. For this, I used a 5mm thick foam board.

Step 8

Next, install the servo motor in a suitable place and mount the ultrasonic sensor on the servo motor horn.

Step 9

Finally, connect an external power supply to the system. Now you can test the project in any way you like. The full video is given below. We hope to see you in the next project.

How to make a DIY Radar System using an Arduino and an Ultrasonic Sensor

https://youtu.be/DlnoChFjZLk

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *