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
| From | To | Note |
|---|---|---|
| HC-SR04 VCC | Arduino 5V | - |
| HC-SR04 GND | Arduino GND | - |
| HC-SR04 TRIG | Arduino D9 | - |
| HC-SR04 ECHO | Arduino D10 | - |
| Servo signal (orange) | Arduino D6 | - |
| Servo VCC (red) | Arduino 5V | Use 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
- The controller fires a 10 microsecond pulse on TRIG and measures the echo pulse width on ECHO.
- Distance in centimeters = pulse width (microseconds) / 58.
- If distance < OPEN_DISTANCE, a person is considered present and the door opens (servo to 90 degrees).
- While the person is present, the door stays open and the timer keeps resetting.
- 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 System2#include <Servo.h>34const int TRIG_PIN = 9;5const int ECHO_PIN = 10;6const int SERVO_PIN = 6;78Servo doorServo;9const int OPEN_DISTANCE = 50; // cm - closer than this = person detected10const unsigned long CLOSE_DELAY = 3000; // ms to wait before closing11unsigned long clearSince = 0;1213float 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 range21 return duration / 58.0;22}2324void setup() {25 pinMode(TRIG_PIN, OUTPUT);26 pinMode(ECHO_PIN, INPUT);27 doorServo.attach(SERVO_PIN);28 doorServo.write(0); // Start closed29 Serial.begin(9600);30}3132void loop() {33 float distance = readDistanceCM();34 Serial.print("Distance: ");35 Serial.println(distance);3637 if (distance < OPEN_DISTANCE) {38 clearSince = 0; // Person present: reset timer39 if (doorServo.read() != 90) {40 doorServo.write(90); // Open the door41 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 door47 Serial.println("CLEAR -> DOOR CLOSED");48 }49 }50 delay(100);51}