Arduino UNO Automatic Door Opener with Ultrasonic Sensor and Micro Servo
View and Edit Project In Cirkit Designer
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
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
9V Battery
- Description: A standard 9V battery used to power the circuit.
- Pins: -, +
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic sensor used to measure distance.
- Pins: VCC, TRIG, ECHO, GND
Micro servo 9G
- Description: A small servo motor used to open and close the door.
- Pins: GND, +5V, PWM
Wiring Details
Arduino UNO
- 5V: Connected to VCC of HC-SR04 Ultrasonic Sensor and +5V of Micro servo 9G.
- GND: Connected to GND of HC-SR04 Ultrasonic Sensor, GND of Micro servo 9G, and - of 9V Battery.
- Vin: Connected to + of 9V Battery.
- D10: Connected to ECHO of HC-SR04 Ultrasonic Sensor.
- D9: Connected to TRIG of HC-SR04 Ultrasonic Sensor.
- D6: Connected to GND of Micro servo 9G.
9V Battery
- -: Connected to GND of Arduino UNO.
- +: Connected to Vin of Arduino UNO.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V of Arduino UNO.
- TRIG: Connected to D9 of Arduino UNO.
- ECHO: Connected to D10 of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
Micro servo 9G
- GND: Connected to D6 of Arduino UNO.
- +5V: Connected to 5V of Arduino UNO.
- PWM: Connected to GND of Arduino UNO.
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.