Arduino based PIR Motion Sensor Alarm

A human body radiates infrared signal which can be detected using PIR(Passive Infrared) sensor. When human body is detected nearby the sensor we can make sound alarm using buzzer or speaker to inform about intruder detection. Such PIR motion sensor based alarm is created here with Arduino.

Circuit Wiring Diagram

The following is the circuit wiring diagram for the alarm system.

Here we have used the HC SR501 PIR sensor as shown below.

The bottom view of the sensor is shown below.


The PIR sensor has 3 pins called the ground, power and output. The output pin is connected to the Arduino pin 2 as shown in the circuit diagram above. It also has two potentiometer to adjust the sensitivity and the time delay. Also it has a jumper which can be set to either repeating trigger or a single trigger. 

The piezo buzzer pin is connected to the pin 12 of Arduino and the LED pin is connected to pin 11 of Arduino. When the PIR detects objects near it then the LED is turned on and also the buzzer will sound alarm.

The following is the program code for the alarm system.

Program Code

int pinSpeaker = 12; // Pin connected to piezo buzzer
int ledPin = 11; // Pin connected to LED
int inputPin = 2; // Pin connected to PIR sensor
int pirState = LOW; // Start PIR state LOW with no motion

int val = 0; // Variable for reading the pin status

void setup(){
pinMode(ledPin, OUTPUT); // Set LED as output
pinMode(inputPin, INPUT); // Set sensor as input
pinMode(pinSpeaker, OUTPUT);

void loop(){
val = digitalRead(inputPin); // Read PIR input value
	if (val == HIGH){ // Check if input is HIGH
	digitalWrite(ledPin, HIGH); // If it is, turn ON LED
	playTone(300, 160);
		if (pirState == LOW) {
		// Print to the Serial Monitor if motion detected
		Serial.println("Motion detected!");
		pirState = HIGH;
	digitalWrite(ledPin, LOW); // If input is not HIGH,
	// turn OFF LED
	playTone(0, 0);
		if (pirState == HIGH) {
		Serial.println("Motion ended!");
		pirState = LOW;

void playTone(long duration, int freq) { // Duration in ms,
	// frequency in Hz
	duration *= 1000;
	int period = (1.0 / freq) * 1000000;
	long elapsed_time = 0;
	while(elapsed_time < duration) {
	digitalWrite(pinSpeaker, HIGH);
	delayMicroseconds(period / 2);
	digitalWrite(pinSpeaker, LOW);
	delayMicroseconds(period / 2);
	elapsed_time += (period);

See other PIR sensor tutorials:

- PIR Sensor with Arduino

- PIR Sensor with AVR Interfacing and Programming

Post a Comment

Previous Post Next Post