Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Arduino Project – Door Security System with IR Sensor

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

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
Arduino Uno

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

IR Sensor
  • Buzzer
  • 9 V Battery
  • Jumper Wires

Diagram

The circuit diagram for this project is provided below, and the details will be discussed in the setup section.

Setup

Door Security System
1. Connect the IR sensor to the Arduino
IR SensorArduino
VCC5V
GNDGND
OUTD2
2. Connect the buzzer to the Arduino
BuzzerArduino
Negative terminalGND
Positive terminalD11
3. Connect the switch to the battery connector

Connect the switch, battery connector, and jumper wires as illustrated in the photo.

4. Upload the program

Connect the Arduino to your computer using a USB cable. Upload the program to the 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 the 9V battery to the 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
 }
}