Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Hackster is hosting Impact Spotlights: Smart Home. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Smart Home. Stream on Thursday!
MattG
Published

Door Alarm With Ultrasonic Sensor

This project uses an ultrasonic sensor to "sense" if the door opens or closes.

BeginnerShowcase (no instructions)2 hours45,471
Door Alarm With Ultrasonic Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
You only one board
×1
Breadboard (generic)
Breadboard (generic)
×1
Buzzer
Buzzer
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

distance warnig

Code

distance alarm

Arduino
An alarm that measures the distance between the wall and door nob or door.
const int TrigPin = 2;
const int EchoPin = 3;
const int Data = 8;
float cm;
void setup()
{
  Serial.begin(9600);
  pinMode(TrigPin, OUTPUT);
  pinMode(EchoPin, INPUT);
  pinMode(Data,OUTPUT);
}
void loop()
{
  digitalWrite(TrigPin, LOW);       
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  cm = pulseIn(EchoPin, HIGH) / 58.0; //The echo time is converted into cm
  cm = (int(cm * 100.0)) / 100.0;     //Keep two decimal places
  Serial.println();
  if(cm<15){ //if you are puting the sensor behind the door use ">" sysmbole insted of ">" p.s. make sure tat the door nob is less than 15 cm away
      digitalWrite(Data,HIGH);
      delay(200);
      Serial.print("Door closed");
    }
    else
    {
      digitalWrite(Data,LOW);
      delay(200);  
      Serial.print("Door open");
    }
  delay(200);
}

Credits

MattG
0 projects • 6 followers
Contact

Comments

Please log in or sign up to comment.