Create a Motion-Sensing Alarm with an Arduino and IR Sensors

June 02, 2015 by Tim Youngblood

Infrared (IR) sensors are normally used to measure distances, but they can also be used to detect objects. By connecting a couple of IR sensors to an Arduino, you can make an intruder alarm.

Infrared (IR) sensors are normally used to measure distances, but they can also be used to detect objects. By connecting a couple of IR sensors to an Arduino, you can make an intruder alarm.


Infrared (IR) sensors are normally used to estimate the distance of an object, but you can also use them to detect the presence of objects. IR sensors consist of an IR transmitter and and IR receiver. The transmitter outputs pulses of infrared radiation, while at the same time, the receiver detects any reflections. If the receiver does detect a reflection, it means that there is an object at some distance in front of the sensor. If there is no reflection, then there is no object.

The IR sensor that we are using in this project is a sharp infrared IR ranger. These sensors have a small linear charge-coupled device (CCD) array that detects the angle at which the IR radiation returns to the sensor. As shown in the figure below, the sensor transmits an infrared pulse into the field, and when there is an object in front of the sensor, the pulse is reflected back to the sensor at an angle proportional to the distance that the object is away from the sensor. The sensor's receiver detects and outputs  the angle, and using this value, you can calculate the distance.

By connecting a couple of IR sensors to an Arduino, we can make a simple intruder alarm. We will connect the sensors to a door jamb, and by properly aligning the sensors, we can detect when someone passes through the door. When this happens, the output of the IR sensor will change, and we will detect this change by continually reading the output of the IR sensors with an Arduino. In this example, we know that an object is passing through the door when the IR sensor’s output exceeds  400. When this occurs, the Arduino will set the alarm to ON. To reset the alarm, a user can press a pushbutton. 

Hardware Required

  • 2 x long-range IR sensors
  • 1 x Arduino Mega2560
  • 1 x buzzer
  • 1 x pushbutton
  • 1 x 470 ohm resistor
  • 1 x NPN transistor
  • jumper wires

Wiring Diagram

The circuit for this project is shown in the diagram below. The outputs of the two IR sensors connect to A0 and A1. The other two wires are connected to 5V and GND. A 12V buzzer is connected to pin 3 through a transistor and the pushbutton used to shut the alarm OFF connects to pin 4. 

The photo below shows how we taped the sensors to the door jamb for this experiment. You would, of course, install the sensors much differently for a more permanent installation.


  1. Connect Arduino 5V and GND pins the +5V and ground terminals of the sensors. You can also supply voltage to them externally.
  2. Connect the sensor output terminals to the Arduino A0 and A1 pins.
  3. Connect Arduino pin 3 to the base of the transistor through a 1K resister.
  4. Apply 12V to collector of transistor.
  5. Connect positive terminal of 12V buzzer to emitter and negative to ground.
  6. Connect Arduino pin 4 to 5V through a push button. Its always better to connect a small resistor with it to be on a safe side to avoid flow of excessive current.
  7. Connect the Arduino using Arduino USB cable and upload the program to Arduino using Arduino IDE software.
  8. Provide power to the Arduino board using power supply, battery or USB cable.


const int buzzer=3;     // pin 3 is the buzzer output
const int pushbutton=4; // pin 4 is the pushbutton input

void setup()
  pinMode(buzzer,OUTPUT);    //configure pin 4 as OUTPUT
  pinMode(pushbutton,INPUT); //configure pin 4 as INPUT
void loop()
  int sensor1_value=analogRead(A0);   //read the output of both sensors and compare to the threshold value
  int sensor2_value=analogRead(A1);
  if (sensor1_value>400||sensor2_value>400)
      digitalWrite(buzzer,HIGH);        //sets the alarm ON
  digitalWrite(buzzer,LOW); //turn off alarm 


Give this project a try for yourself! Get the BOM.

  • Michael Margolese September 20, 2016

    Do you have a part number and/or data sheet for these IR Sensors ?

    Like. Reply
  • T
    tejaswi07 September 23, 2019

    can we use arduino uno instead of arduino mega2560?

    Like. Reply
    • A
      apaularoth June 30, 2020
      arduino uno works also. Please study the code above and notice that the comment "//configure pin 4 as OUTPUT" is incorrect but will not affect the operation. Should state "//configure pin 3 as OUTPUT". Also, the zip file is incomplete and will not work as written. After you unzip the zip file compare it with the code above and make the changes to match the code above. Good luck and have fun.
      Like. Reply