Arduino Project

Radar Guard 5.0

Course Introduction

This project uses an ultrasonic sensor, a servo motor, and an Arduino to create a simple radar system with LED and buzzer alerts.

The servo rotates the sensor to scan for obstacles. If an object is within a set distance, a red LED and buzzer are activated. Otherwise, a green LED shows it’s clear.

Angle and distance data are sent via the serial port for monitoring.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima

1

2

USB Type-C cable

1

×

3

Breadboard

1

4

Wires

Several

5

1kΩ resistor

2

6

Ultrasonic Sensor Module

1

7

LED

2

8

Digital Servo Motor

1

9

Buzzer

1

×

Wiring

3.webp__PID:4f9c4039-1ea4-49fd-99e3-e84080279d1c

Common Connections:

LED
Connect the LEDs anode to the 3, 4 on Arduino, and the LEDs cathode to a 1kΩ resistor then to negative power bus on the breadboard.

Digital Servo Motor
Connect to breadboard’s positive power bus.
Connect to breadboard’s negative power bus. 
Connect to 12 on the Arduino.

Buzzer
+:
Connect to breadboard’s positive power bus.
-:Connect to transistor.

Ultrasonic Sensor Module
Trig:
Connect to 10 on the Arduino.
Echo: Connect to 11 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

Writing the Code

Note
Build the circuit.
Upload the code to the Arduino board using Arduino IDE.
In the Arduino IDE, check the current Arduino port(COMx).
The ArduinoRadarGUI is used here. You can click here ArduinoSonarGUI.zip to download it.
Open ArduinoLidarGUI.pde in the Processing IDE .
Modify the code in line 35 to ensure the correct port number(COMx).
Run the Processing sketch to visualize the Lidar data.


/*
  Modified Arduino Radar Code with LED and Buzzer Alerts
*/

#include 

// Ultrasonic sensor pins
const int trigPin = 10;
const int echoPin = 11;

const int servoPin = 12;
Servo myServo;

// LEDs and buzzer pins
const int redLED = 7;
const int greenLED = 6;
const int buzzerPin = 5;

// Distance threshold in cm
const int threshold = 50;

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  Serial.begin(9600);
  myServo.attach(servoPin);
}

void loop() {
  for (int i = 15; i <= 165; i++) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();
    alertSystem(distance);
    sendData(i, distance);
  }
  for (int i = 165; i > 15; i--) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();
    alertSystem(distance);
    sendData(i, distance);
  }
}

int calculateDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  float distance = pulseIn(echoPin, HIGH) / 58.00;
  return (int)distance;
}

void alertSystem(int dist) {
  if (dist > 0 && dist <= threshold) {
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);

    int beepDelay = map(dist, 1, threshold, 50, 300); // Closer = faster beep
    digitalWrite(buzzerPin, HIGH);
    delay(5);
    digitalWrite(buzzerPin, LOW);
    delay(beepDelay);
  } else {
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, HIGH);
    digitalWrite(buzzerPin, LOW);
  }
}

void sendData(int angle, int dist) {
  Serial.print(angle);
  Serial.print(",");
  Serial.print(dist);
  Serial.print(".");
}