Arduino Project

Radar Guard 6.0

Course Introduction

This project uses an ultrasonic sensor, a servo motor, and an Arduino to build a radar system with LCD and OLED displays.

The servo scans for obstacles, showing distance and angle data on both screens with a semicircle radar view. If an object is closer than the set threshold, a red LED and buzzer alert; otherwise, a green LED indicates a clear path.

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

220Ω resistor

2

6

Ultrasonic Sensor Module

1

7

LED

2

8

I2C LCD 1602

1

9

Digital Servo Motor

1

10

Active Buzzer

1

×

11

OLED Display Module

1

Wiring

4.webp__PID:00feeb4b-ecb9-4480-92de-1d9e23795893

Common Connections:

LED
Red LED:
Connect the LEDs anode to the 4 on Arduino, and the LEDs cathode to a 220Ω resistor then to negative power bus on the breadboard.
Green LED: Connect the LEDs anode to the 5 on Arduino, and the LEDs cathode to a 220Ω 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 9 on the Arduino.

Active Buzzer
+:
Connect toon the Arduino.
-: Connect to breadboard’s negative power bus.

I2C LCD 1602
SDA:
Connect to A4 on the Arduino.
SCL: Connect to A5 on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

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

OLED Display Module
SDA:
Connect to SDA on the Arduino.
SCK: Connect to SCL on the Arduino.
GND: Connect to breadboard’s negative power bus.
VCC: Connect to breadboard’s red power bus.

Writing the Code

Note
You can copy this code into Arduino IDE.
To install the library, use the Arduino Library Manager and search for LiquidCrystal I2C and Adafruit GFX and Adafruit SSD1306 and install it.
Don’t forget to select the board(Arduino UNO R4 Minima/WIFI) and the correct port before clicking the Upload button.


#include 
#include 
#include 
#include 
#include 

// --- Servo & Ultrasonic ---
Servo servo;
const int trigPin = 2;
const int echoPin = 3;

// --- LED & Buzzer ---
const int ledGreen = 4;
const int ledRed   = 5;
const int buzzer   = 6;

// --- LCD (0x27 or 0x3F) ---
LiquidCrystal_I2C lcd(0x27, 16, 2);

// --- OLED (0x3C) ---
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// --- Parameters ---
int angle = 0;
long distance = 0;
const int DETECT_THRESHOLD = 20;  // cm

// --- Distance measurement function ---
long getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH, 30000); // 30ms timeout
  if (duration == 0) return 999; // timeout handling
  return duration * 0.034 / 2;   // cm
}

void setup() {
  Serial.begin(9600);
  servo.attach(9);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(buzzer, OUTPUT);

  // LCD
  lcd.init();
  lcd.backlight();

  // OLED
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for (;;); // stop if initialization fails
  }
  display.clearDisplay();
  display.display();
}

void loop() {
  // Left to right
  for (angle = 0; angle <= 180; angle += 3) {
    servo.write(angle);
    delay(30);
    distance = getDistance();
    radarAction(distance);
    drawRadar(angle, distance);
  }
  // Right to left
  for (angle = 180; angle >= 0; angle -= 3) {
    servo.write(angle);
    delay(30);
    distance = getDistance();
    radarAction(distance);
    drawRadar(angle, distance);
  }
}

// --- Buzzer/LED/LCD display control ---
void radarAction(long d) {
  lcd.clear();
  if (d < DETECT_THRESHOLD && d > 0) {
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledGreen, LOW);
    digitalWrite(buzzer, HIGH);
    lcd.setCursor(0, 0);
    lcd.print("Detected Object");
    lcd.setCursor(0, 1);
    lcd.print("Dist: ");
    lcd.print(d);
    lcd.print(" cm");
  } else {
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, HIGH);
    digitalWrite(buzzer, LOW);
    lcd.setCursor(0, 0);
    lcd.print("No Object");
    lcd.setCursor(0, 1);
    lcd.print("Dist: ");
    lcd.print(d);
    lcd.print(" cm");
  }
}

// --- OLED semicircle radar drawing ---
void drawRadar(int angle, long d) {
  display.clearDisplay();

  int centerX = SCREEN_WIDTH / 2;
  int centerY = SCREEN_HEIGHT - 1;

  // --- Semicircle concentric circles ---
  for (int r = 15; r <= 60; r += 15) {
    display.drawCircle(centerX, centerY, r, SSD1306_WHITE);
  }
  // --- Radius lines ---
  for (int a = 0; a <= 180; a += 30) {
    float rad = radians(a);
    int x = centerX + cos(rad) * 60;
    int y = centerY - sin(rad) * 60;
    display.drawLine(centerX, centerY, x, y, SSD1306_WHITE);
  }

  // --- Scanning line ---
  float rad = radians(angle);
  int x2 = centerX + cos(rad) * 60;
  int y2 = centerY - sin(rad) * 60;
  display.drawLine(centerX, centerY, x2, y2, SSD1306_WHITE);

  // --- Detected target point ---
  if (d < 60) {
    int tx = centerX + cos(rad) * d;
    int ty = centerY - sin(rad) * d;
    display.fillCircle(tx, ty, 2, SSD1306_WHITE);
  }

  // --- Angle/Distance text ---
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print("Ang:");
  display.print(angle);
  display.print(" D:");
  display.print(d);

  display.display();
}