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

Arduino UNO Automatic Door Opener with Ultrasonic Sensor and Micro Servo

View and Edit Project In Cirkit DesignerImage of Arduino UNO Automatic Door Opener with Ultrasonic Sensor and Micro Servo

Circuit Documentation

Summary

This circuit is designed to automatically open and close a door using an HC-SR04 ultrasonic sensor and a micro servo motor. The Arduino UNO microcontroller reads the distance from the ultrasonic sensor and controls the servo motor to open or close the door based on the detected distance. A 9V battery powers the entire circuit.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. 9V Battery

    • Description: A standard 9V battery used to power the circuit.
    • Pins: -, +
  3. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic sensor used to measure distance.
    • Pins: VCC, TRIG, ECHO, GND
  4. Micro servo 9G

    • Description: A small servo motor used to open and close the door.
    • Pins: GND, +5V, PWM

Wiring Details

Arduino UNO

9V Battery

HC-SR04 Ultrasonic Sensor

Micro servo 9G

Code Documentation

/*
 * Arduino Sketch for Automatic Door Open and Close
 *
 * This sketch uses an HC-SR04 ultrasonic sensor to detect the distance of an
 * object (e.g., a person) from the door. When the object is within a certain
 * range, the micro servo motor will open the door. If the object moves away,
 * the door will close automatically.
 */

#include <Servo.h>

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

// Create Servo object
Servo myServo;

// Variables for distance measurement
long duration;
int distance;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Set pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Attach servo to pin
  myServo.attach(servoPin);

  // Initialize servo position
  myServo.write(0); // Door closed
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Set the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  distance = duration * 0.034 / 2;

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Check if the distance is within the threshold
  if (distance < 50) { // If object is within 50 cm
    myServo.write(90); // Open door
  } else {
    myServo.write(0); // Close door
  }

  // Wait for a short period before next measurement
  delay(500);
}

This code initializes the ultrasonic sensor and servo motor, measures the distance to an object, and controls the servo motor to open or close the door based on the measured distance. The distance is also printed to the Serial Monitor for debugging purposes.