Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Based Ultrasonic Radar System with Servo Motor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Based Ultrasonic Radar System with Servo Motor

Circuit Documentation

Summary of the Circuit

This circuit is designed to create an ultrasonic radar system using an Arduino UNO microcontroller, an HC-SR04 ultrasonic sensor, and an SG90 servo motor. The system operates by sweeping the ultrasonic sensor across a range of 0 to 180 degrees using the servo motor. The sensor detects the presence of obstacles within a certain range and outputs the distance to the obstacle. The Arduino UNO controls the servo's position and processes the distance measurements from the ultrasonic sensor, providing feedback through serial communication.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

SG90 Servo Motor

Documented Code

/*
 * Ultrasonic Radar System
 * This code controls an ultrasonic sensor and a servo motor to create a radar
 * system. The servo motor sweeps the ultrasonic sensor from 0 to 180 degrees.
 * When an obstacle is detected within a certain range, an indication is given.
 */

#include <Servo.h>

// Pin definitions
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 11;

// Create Servo object
Servo myServo;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Initialize pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Attach servo to pin
  myServo.attach(servoPin);
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 1) {
    myServo.write(angle);
    delay(15); // Allow servo to reach position
    long duration = getDistance();
    long distance = duration * 0.034 / 2;
    Serial.print("Angle: ");
    Serial.print(angle);
    Serial.print(" Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
    if (distance < 20) { // If obstacle is within 20 cm
      Serial.println("Obstacle detected!");
    }
  }
  for (int angle = 180; angle >= 0; angle -= 1) {
    myServo.write(angle);
    delay(15);
    long duration = getDistance();
    long distance = duration * 0.034 / 2;
    Serial.print("Angle: ");
    Serial.print(angle);
    Serial.print(" Distance: ");
    Serial.print(distance);
    Serial.println(" cm");
    if (distance < 20) {
      Serial.println("Obstacle detected!");
    }
  }
}

long getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  return duration;
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the servo motor and ultrasonic sensor, then enters a loop where the servo sweeps the sensor from 0 to 180 degrees and back, measuring distance at each angle and printing the results to the serial monitor. If an obstacle is detected within 20 cm, an additional message is printed to indicate the detection.