Practical 3

Automatic Door Opening System

Contactless door driven by a servo motor

Aim

To open and close a door automatically whenever a person approaches, using an ultrasonic sensor to detect presence and a servo motor to actuate the door.

Overview

An HC-SR04 ultrasonic sensor mounted above the door continuously measures the distance of objects in front of it. When a person comes closer than the detection range, the Arduino commands a servo motor to rotate from 0 degrees (closed) to 90 degrees (open). After the person passes and the area remains clear for a few seconds, the servo returns the door to the closed position.

Components Required

  • Arduino Uno
  • HC-SR04 ultrasonic distance sensor
  • SG90 servo motor (door actuator)
  • Optional buzzer (door alert)
  • 5V power supply capable of 500 mA+ for the servo
  • Breadboard and jumper wires

Circuit Connections

FromToNote
HC-SR04 VCCArduino 5V-
HC-SR04 GNDArduino GND-
HC-SR04 TRIGArduino D9-
HC-SR04 ECHOArduino D10-
Servo signal (orange)Arduino D6-
Servo VCC (red)Arduino 5VUse external 5V for real servo
Servo GND (brown)Arduino GND-

Interactive Circuit Diagram

Simulate the circuit: adjust the sensor inputs below and watch the outputs react according to the control logic.

Loading Wokwi components...

Working

  1. The controller fires a 10 microsecond pulse on TRIG and measures the echo pulse width on ECHO.
  2. Distance in centimeters = pulse width (microseconds) / 58.
  3. If distance < OPEN_DISTANCE, a person is considered present and the door opens (servo to 90 degrees).
  4. While the person is present, the door stays open and the timer keeps resetting.
  5. When the area is clear for a continuous CLOSE_DELAY (e.g. 3 seconds), the servo returns to 0 degrees.

Control Logic

DOOR = OPEN when distance < 50 cm; otherwise keep open until the area has been clear for 3 s, then CLOSE. The dwell (hold-open) timer avoids slamming the door on someone standing in the doorway.

Arduino Code

1// Practical 3: Automatic Door Opening System
2#include <Servo.h>
3
4const int TRIG_PIN = 9;
5const int ECHO_PIN = 10;
6const int SERVO_PIN = 6;
7
8Servo doorServo;
9const int OPEN_DISTANCE = 50; // cm - closer than this = person detected
10const unsigned long CLOSE_DELAY = 3000; // ms to wait before closing
11unsigned long clearSince = 0;
12
13float readDistanceCM() {
14 digitalWrite(TRIG_PIN, LOW);
15 delayMicroseconds(2);
16 digitalWrite(TRIG_PIN, HIGH);
17 delayMicroseconds(10);
18 digitalWrite(TRIG_PIN, LOW);
19 unsigned long duration = pulseIn(ECHO_PIN, HIGH, 30000);
20 if (duration == 0) return 999.0; // No echo = out of range
21 return duration / 58.0;
22}
23
24void setup() {
25 pinMode(TRIG_PIN, OUTPUT);
26 pinMode(ECHO_PIN, INPUT);
27 doorServo.attach(SERVO_PIN);
28 doorServo.write(0); // Start closed
29 Serial.begin(9600);
30}
31
32void loop() {
33 float distance = readDistanceCM();
34 Serial.print("Distance: ");
35 Serial.println(distance);
36
37 if (distance < OPEN_DISTANCE) {
38 clearSince = 0; // Person present: reset timer
39 if (doorServo.read() != 90) {
40 doorServo.write(90); // Open the door
41 Serial.println("PERSON -> DOOR OPEN");
42 }
43 } else {
44 if (clearSince == 0) clearSince = millis();
45 if (millis() - clearSince > CLOSE_DELAY && doorServo.read() != 0) {
46 doorServo.write(0); // Area clear: close the door
47 Serial.println("CLEAR -> DOOR CLOSED");
48 }
49 }
50 delay(100);
51}