Skip to main content

Command Palette

Search for a command to run...

Smart Door System

Updated
1 min read
#include <Servo.h>
#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 6
Servo lidServo;
void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  lidServo.attach(SERVO_PIN);
  lidServo.write(90); // Lid closed initially
  Serial.begin(9600);
}
void loop() {
  long duration, distance;
  // Send ultrasonic pulse
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  // Read echo
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2; // Convert to cm
 Serial.print(distance);
  // If hand/trash detected within 20 cm
  if (distance > 0 && distance < 10) {
    lidServo.write(0);   // Open lid
    delay(3000);          // Wait 3 seconds
    lidServo.write(90);    // Close lid
  }
  delay(200); // Small delay to avoid too many triggers
}

More from this blog

Aarav's Blogs

19 posts