Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Ultrasonic Sensor with Dual Servo Missile Ejection System

Image of Arduino-Controlled Ultrasonic Sensor with Dual Servo Missile Ejection System

Circuit Documentation

Summary

This circuit is designed to control a pair of SG90 servo motors and an HC-SR04 ultrasonic sensor using an Arduino UNO microcontroller. One servo motor is used for rotating the ultrasonic sensor, and the other is for ejecting a missile when an object is detected within a certain distance. The circuit is powered by two 9V batteries, with a 7805 voltage regulator providing a stable 5V supply to the components. The ultrasonic sensor measures the distance to objects, and the Arduino UNO processes this information to control the servo motors based on the programmed logic.

Component List

9V Battery

  • Description: Provides power to the circuit.
  • Pins: +, -

SG90 Servo Motor

  • Description: Actuates to rotate the ultrasonic sensor and eject the missile.
  • Pins: PWM, 5V, GND

HC-SR04 Ultrasonic Sensor

  • Description: Measures the distance to objects by emitting ultrasonic waves.
  • Pins: VCC, TRIG, ECHO, GND

7805 Voltage Regulator

  • Description: Regulates the input voltage to a stable 5V output.
  • Pins: INPUT, OUTPUT, GND

Arduino UNO

  • Description: Microcontroller board that controls the logic of the circuit.
  • Pins: Multiple digital and analog pins, power supply pins (3.3V, 5V, GND, Vin), and communication pins (SCL, SDA).

Wiring Details

9V Battery

  • Positive Terminal: Connected to the INPUT pin of the 7805 voltage regulator.
  • Negative Terminal: Common ground with all GND pins of the components.

SG90 Servo Motor

  • PWM: Connected to digital pins D9 and D10 on the Arduino UNO for control signals.
  • 5V: Connected to the OUTPUT pin of the 7805 voltage regulator.
  • GND: Common ground with all GND pins of the components.

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to the OUTPUT pin of the 7805 voltage regulator.
  • TRIG: Connected to digital pin D7 on the Arduino UNO.
  • ECHO: Connected to digital pin D8 on the Arduino UNO.
  • GND: Common ground with all GND pins of the components.

7805 Voltage Regulator

  • INPUT: Connected to the positive terminal of the 9V battery.
  • OUTPUT: Provides a stable 5V to the 5V pins of the servo motors, ultrasonic sensor, and Arduino UNO.
  • GND: Common ground with all GND pins of the components.

Arduino UNO

  • Digital Pins (D7, D8, D9, D10): Control signals for the ultrasonic sensor and servo motors.
  • 5V and GND Pins: Power supply for the board and common ground.

Documented Code

#include <Servo.h>

// Pin Definitions
const int trigPin = 7;    // Ultrasonic sensor trig pin
const int echoPin = 8;    // Ultrasonic sensor echo pin
const int rotateServoPin = 9;  // Servo for rotating the ultrasonic sensor
const int ejectServoPin = 10;  // Servo for ejecting the missile

Servo rotateServo;  // Servo object for rotation
Servo ejectServo;   // Servo object for ejection

long duration;
int distance;
int thresholdDistance = 40; // Eject missile if object is within 40 cm

void setup() {
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    rotateServo.attach(rotateServoPin); // Attach the rotation servo
    ejectServo.attach(ejectServoPin);   // Attach the ejection servo
    Serial.begin(9600);
}

void loop() {
    // Rotate the sensor and missile (Servo 1)
    for (int i = 15; i <= 165; i++) {
        rotateServo.write(i);
        delay(30);
        distance = calculateDistance(); // Calculate the distance
        Serial.print("Angle: ");
        Serial.print(i);
        Serial.print(", Distance: ");
        Serial.println(distance);
        // Check if object is within the threshold distance
        if (distance <= thresholdDistance) {
            ejectMissile();  // Call the ejection function
            delay(2000);     // Wait for 2 seconds before continuing
        }
    }
    // Rotate the sensor back (Servo 1)
    for (int i = 165; i >= 15; i--) {
        rotateServo.write(i);
        delay(30);
        distance = calculateDistance(); // Calculate the distance
        Serial.print("Angle: ");
        Serial.print(i);
        Serial.print(", Distance: ");
        Serial.println(distance);
        // Check if object is within the threshold distance
        if (distance <= thresholdDistance) {
            ejectMissile();  // Call the ejection function
            delay(2000);     // Wait for 2 seconds before continuing
        }
    }
}

// Function to calculate distance using Ultrasonic Sensor
int calculateDistance() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    return duration * 0.034 / 2;  // Convert to cm
}

// Function to eject the missile
void ejectMissile() {
    ejectServo.write(90);  // Rotate servo to 90 degrees to pull the needle
    delay(500);            // Wait for the ejection
    ejectServo.write(0);   // Return servo to original position
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes two servo motors and an ultrasonic sensor, then enters a loop where it rotates the sensor, measures the distance to objects, and ejects a missile if the object is within a predefined distance. The calculateDistance function is used to measure the distance using the ultrasonic sensor, and the ejectMissile function controls the servo motor responsible for ejection.