After a long semester with nearly no time to tinker with the Raspberry Pi, I am back, this time experimenting with the Passive Infra-Red (PIR) Sensor to build a motion detector alarm.
First let me explain briefly the working principle of the PIR sensor. All objects with a temperature above absolute zero (0° K or -273° C) emit heat energy in the form of radiation.
The PIR sensor detects the heat emitted by a body, more precisely the heat change with respect to the environment. The circuitry of the PIR sensor is quite complex; allowing it to detect only human (sometimes animal) movements, thus being perfect to be used for intruder alarms. With the dome shaped lens, the sensor is able to sense motion in all directions in front of it (180°), the lens converges the radiation to the sensor.

PIR Front (naked)

PIR Front

PIR Lens

PIR Sensor Back
To build the motion detector alarm, we are going to use the following components:
- Raspberry Pi 3
- PIR sensor (cheap)
- Active Buzzer
- Red LED
- NPN transistor (PNP transistor also can be used)
- 2 x 220Ω resistor
- Multiple jumper wires
- Breadboard
Firstly in any project, we need to design the system; draw a schematic diagram. My design is shown below.

Schematic of motion detection alarm
The PIR sensor is powered by 5V supply and is connected to the GPIO to signal if a motion was detected. The PIR also has 2 potentiometers, one is to adjust the delay time and the other for the sensitivity(range).
With the buzzer, we had to use a NPN transistor because it requires a larger current, the NPN transistor amplifies the current. You can also use a PNP transistor but the code will be slightly different.
Note: If you are not used to the Raspberry Pi interface, read the last post about programming LEDs.
The next step is to do the connections of the system, mine shown below.

Motion detector circuit diagram

Motion Detector Arrangement
The code used for the alarm is shown below and the programming language used is C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h> #include <wiringPi.h> //Defining GPIO pins #define PIR_OUT_PIN 0 #define LEDpin 1 #define BeepPin 2 int main(void) { if(wiringPiSetup() == -1) { //when initialize wiring failed,print message to screen printf("setup wiringPi failed !\n"); return -1; } pinMode(PIR_OUT_PIN, INPUT); pinMode(LEDpin,OUTPUT); pinMode(BeepPin, OUTPUT); while(1) { digitalWrite(LEDpin, LOW); // LED OFF while(digitalRead(PIR_OUT_PIN) == 1) { digitalWrite(LEDpin, LOW); // LED ON // Buzzer beeps alternately digitalWrite(BeepPin, HIGH); // change 'HIGH' to 'LOW' if using PNP delay(100); digitalWrite(BeepPin, LOW); // change 'LOW' to 'HIGH' if using PNP digitalWrite(LEDpin, HIGH); //LED OFF delay(100); } } return 0; } |
The only difference in the code when using PNP, the signal argument should be opposite for the BeepPin.
This is a video of me testing out the alarm.
The buzzer rings alternately along with the LED blinking as long as movements are being detected.
To do this project, you could have not used the Raspberry Pi but some calculations and adjustments would be needed to adjust for the power needed by the buzzer.
Further improvement you could do to the project is to add a switch which turns off the alarm therefore making it work like a real intruder alarm.