Hackster is hosting Impact Spotlights: Robotics. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Robotics. Stream on Thursday!
Riku Lahkar
Published © MIT

A Mini Ultrason Radar using ARDUINO UNO

Arduino-based ultrasonic radar that scans & detects objects in real-time using a servo & sensor. A fun beginner-friendly radar project!

BeginnerFull instructions provided12 hours305
A Mini Ultrason Radar using ARDUINO UNO

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Processing
The Processing Foundation Processing

Hand tools and fabrication machines

Tape, Double Sided
Tape, Double Sided

Story

Read more

Schematics

Circuit Diagram

This is the complete wiring diagram for the radar system. It shows the connection between the Arduino Uno, ultrasonic sensor (HC-SR04), servo motor (SG90), LED indicator, and other components.

The trig and echo pins of the HC-SR04 are connected to D8 and D9 of the Arduino.

The servo signal pin is connected to D10.

The LED is connected to D7 and glows when an object is detected within the set range.

All components share a common ground (GND).

Powered via USB or external 5V supply

Code

The complete code fin ARDUINO UNO

Arduino
No preview (download only).

Code for radar.txt

Arduino
#include <Servo.h>

const int trigPin = 8;
const int echoPin = 9;
const int ledPin = 7; //  LED connected to pin 7

long duration;
int distance;
Servo myServo;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT); //  Set LED pin as output
  Serial.begin(9600);
  myServo.attach(10);
}

void loop() {
  for (int i = 15; i <= 165; i++) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();

    //  Check distance and light LED if object is detected
    if (distance <= 20) {
      digitalWrite(ledPin, HIGH); // Turn on LED
    } else {
      digitalWrite(ledPin, LOW);  // Turn off LED
    }

    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }

  for (int i = 165; i > 15; i--) {
    myServo.write(i);
    delay(30);
    distance = calculateDistance();

    if (distance <= 20) {
      digitalWrite(ledPin, HIGH); // Turn on LED
    } else {
      digitalWrite(ledPin, LOW);  // Turn off LED
    }

    Serial.print(i);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }
}

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

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

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  return distance;
}

Credits

Riku Lahkar
3 projects • 1 follower

Comments