← Return to Home

Arduino Sonar Radar System

Ultrasonic Distance Mapping with Real-Time Visualization

Detect. Scan. Visualize.

Project Overview

This project simulates a simple radar system using an Arduino, an ultrasonic sensor, a servo motor, an active buzzer, and an LED. I chose this to complement my NASA RC Rover design—adding obstacle detection for planetary exploration scenarios where visibility and control are limited.

Hardware Inputs

SensorPart #FunctionSignalLabel
Ultrasonic HC-SR04 HC-SR04 Measures distance via ultrasonic pulses Time-based pulse Sonar

Hardware Outputs

DeviceFunctionSignalLabel
Servo Motor Rotates sensor through scan arc PWMScanner
Active Buzzer Audible alert when object < 20 cm Digital (LOW = ON)AlertTone
Red LED Visual alert when object < 20 cm Digital (HIGH = ON)AlertLight

Core Functions

  1. Sweep sensor from 15° to 165° via servo.
  2. Measure distance at each angle.
  3. If distance < 20 cm, turn buzzer & LED ON; otherwise OFF.
  4. Send “angle,distance.” over serial.
  5. Reverse sweep 165° → 15° and repeat.

Design & Calculations

LED current-limiting resistor: 220 Ω (~20 mA).
Sampling interval: ~30 ms per step (delay(30) in code).
No external capacitors used.

Discussion & Improvements

Photo Gallery

Arduino IDE Code


// Includes the Servo library
#include 

// Defines Trig and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
const int buzzerPin = 7; // Active buzzer (LOW = on)
const int ledPin    = 6; // Red LED (HIGH = on)

long duration;
int distance;
Servo myServo;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(buzzerPin, HIGH); // OFF
  digitalWrite(ledPin, LOW);     // OFF
  Serial.begin(9600);
  myServo.attach(12);
}

void loop() {
  // Sweep out
  for (int i = 15; i <= 165; i++) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();
    if (distance > 0 && distance < 20) {
      digitalWrite(buzzerPin, LOW);
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(ledPin, LOW);
    }
    Serial.print(i); Serial.print(",");
    Serial.print(distance); Serial.print(".");
  }
  // Sweep back
  for (int i = 165; i > 15; i--) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();
    if (distance > 0 && distance < 20) {
      digitalWrite(buzzerPin, LOW);
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(buzzerPin, HIGH);
      digitalWrite(ledPin, LOW);
    }
    Serial.print(i); Serial.print(",");
    Serial.print(distance); Serial.print(".");
  }
}

// Return distance in cm
int calculateDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}
    

Processing 4 Sketch


import processing.serial.*;     // serial library
import java.awt.event.KeyEvent; // for keyboard events
import java.io.IOException;

Serial myPort;               // serial object
String angle = "", distance = "", data = "", noObject;
float pixsDistance;
int iAngle, iDistance;

void setup() {
  size(1200, 700);            // match your screen
  smooth();
  myPort = new Serial(this, "COM3", 9600);
  myPort.bufferUntil('.');
}

void draw() {
  // fade effect
  noStroke();
  fill(0,4);
  rect(0, 0, width, height - height*0.065);

  // draw radar
  stroke(98,245,31);
  strokeWeight(2);
  noFill();
  pushMatrix();
  translate(width/2, height - height*0.074);
  arc(0,0, width - width*0.0625, width - width*0.0625, PI, TWO_PI);
  arc(0,0, width - width*0.27, width - width*0.27, PI, TWO_PI);
  arc(0,0, width - width*0.479, width - width*0.479, PI, TWO_PI);
  arc(0,0, width - width*0.687, width - width*0.687, PI, TWO_PI);
  line(-width/2,0, width/2,0);
  popMatrix();

  // draw scan line
  stroke(30,250,60);
  strokeWeight(9);
  pushMatrix();
  translate(width/2, height - height*0.074);
  line(0,0,
    (height - height*0.12)*cos(radians(iAngle)),
    -(height - height*0.12)*sin(radians(iAngle))
  );
  popMatrix();

  // draw object blip
  stroke(255,10,10);
  strokeWeight(9);
  pixsDistance = iDistance * ((height - height*0.1666) * 0.025);
  if (iDistance < 40) {
    pushMatrix();
    translate(width/2, height - height*0.074);
    line(
      pixsDistance*cos(radians(iAngle)),
      -pixsDistance*sin(radians(iAngle)),
      (width - width*0.505)*cos(radians(iAngle)),
      -(width - width*0.505)*sin(radians(iAngle))
    );
    popMatrix();
  }

  // draw text overlay
  fill(0);
  noStroke();
  rect(0, height - height*0.065, width, height);
  fill(98,245,31);
  textSize(40);
  text("Justin Port", 50, height - 20);
  textSize(25);
  text("Angle: " + iAngle + "°", 400, height - 20);
  text("Distance: " + iDistance + " cm", 700, height - 20);
}

void serialEvent(Serial p) {
  data = p.readStringUntil('.');
  data = data.substring(0, data.length() -1);
  int comma = data.indexOf(',');
  angle    = data.substring(0, comma);
  distance = data.substring(comma+1);
  iAngle    = int(angle);
  iDistance = int(distance);
}
    
Back to Home Back to Top