Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Door Security System with IR Sensor

In this project, we will learn how to make a home security system using an Infrared (IR) sensor. 

An IR Sensor is a type of sensor that detects infrared radiation emitted by objects as heat. The Infrared Obstacle Avoidance IR Sensor is widely used in robotics and automation applications to enable obstacle detection and navigation for robots or automated systems.

Visit Understanding Infrared Sensor: What Is An IR Sensor and How Does It Work? for more information.

Hardware Required

  • Arduino UNO

For more information, visit Getting to Know Your Arduino Uno: A Beginner’s Guide to Its Components.

  • Buzzer
  • 9 V Battery
  • Jumper Wires

Diagram

Setup

1. Connect IR Sensor to Arduino

IR SensorArduino
VCC5V
GNDGND
OUTD2

2. Connect Buzzer to Arduino

BuzzerArduino
Negative terminalGND
Positive terminalD11

3. Connect Switch to Battery Connector

4. Connect Arduino to your computer using a USB cable and upload the program to Arduino through the Arduino IDE software. Visit Getting Started with Arduino IDE: A Beginner’s Guide to Coding and Creating for more information.

5. Connect Battery to Arduino

BatteryArduino
Negative terminalGND
Positive terminalVin

Logic

The IR sensor is an ‘Active Low’ output sensor. The sensor’s output is High when it detects an obstacle and Low when no obstacle is caught.

  • When the door is closed, the IR sensor detects the door frame and sends the Low output. 
  • When the door opens, the IR sensor can’t detect the obstacle and sends the High output. Then, the buzzer will start and create an alarm.

Code

byte ir_sensor = 2;  //Digital pin connected to the IR sensor
byte buzzer = 11;    //PWM pin connected to buzzer

void setup()
{
 pinMode(ir_sensor, INPUT);   //IR sensor is an input device
 pinMode(buzzer, OUTPUT);     //Buzzer is an ouput device
}

void loop()
{
 int sensor_state = digitalRead(ir_sensor);  //Read digital IR sensor pin
 if(sensor_state == HIGH)
 {
  analogWrite(buzzer, 200);   //Set the voltage to 200 and make a noise
  delay(110);                 //Wait for 110 milliseconds
  analogWrite(buzzer, 100);   //Set the voltage to 100 and make a noise
  delay(110);                 //Wait for 110 milliseconds
 }
 else
 {
  digitalWrite(buzzer, LOW);  //Set the voltage to low and make no noise
 }
}