Home Security Alarm System using PIC18F45K22 AND PIR Motion Sensor2

This project describes a home security alarm based on a Passive Infra-Red sensor module (HC-SR501) and PIC18F45K22 . PIR sensor is used to detect motion in the environment and is commonly known as motion sensor. When the microcontroller detects the PIR sensor is triggered, it drives the relay switch and trigger an alarm. Its quite helpful in security projects where you need to detect motion.

PIR sensors allow you to sense motion. They are often referred to as PIR, “Passive Infrared”, “Pyroelectric”, or “IR motion” sensors. When an object (human or animal) emitting infrared radiations passes through the field of view of the sensor, it detects the change in temperature and therefore can be used to detect motion . So they are used to detect whether a human has moved in or out of the sensors range.

A PIR has three pins :the VCC, the Ground and the Output pin. The PIR acts as a digital input, so all you need to do is chech for the output pin to flip high or low :

– Output pin high :motion detected (the sensor produce 3.3 volt)

– Output pin low :motion not detected (the out pin will be 0 volt).

Datasheet :https://www.mpja.com/download/31227sc.pdf

Software :

The firmware is written in C and compiled with MikroC Pro for PIC. The microcontroller waits for 60 sec before it starts monitoring the PIR sensor output. This wait time is required for the PIR sensor to stabilize when first powered on. When the microcontroller detects the sensor is triggered, it drives the piezo buzzer with a 659Hz and the LAMP is on.Since the sensor is in retriggering mode, the buzzer remains on as long as the motion is continuously sensed. If the motion disappears, and the sensor output changes to logic low, the buzzer will not stop immediately, but still be on for about 250ms and the LAMP is off.  And if it detects the motion again, it will drive the piezo buzzer at its peak resonant frequency (695 Hz)and the LAMP is on again.

Home Security Alarm System using PIC18F45K22 AND PIR Motion Sensor2 (Code)

* Project name:
Home alarm system with PIC and PIR Motion Sensor
* Copyright:
* Revision History:
Version 0.0
* Description:
PIR sensor is ideal for alarm systems, light switch controllers, and similar systems where human presence needs to be detected.
The pyroelectric motion sensor generates a voltage when exposed to infrared radiation emitted by live bodies.
The BISS001 IC processes the signal and sends an interrupt to the MCU.
To set up the detecting range of the sensor (up to 4 meters), use the onboard potentiometer.
* Test configuration:
MCU: PIC18F45K22
Dev. Board: EasyPIC v7 Connectivity
Oscillator: HS-PLL, 8MHz Crystal
ext. modules: PIR Motion Sensor
SW: mikroC PRO for PIC
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
#define PIR PORTB.FO
#define LAMP PORTB.F1
char *text;
void Tone1() {
Sound_Play(659, 250); // Frequency = 659Hz, duration = 250ms
void Tone2() {
Sound_Play(698, 250); // Frequency = 698Hz, duration = 250ms
void Tone3() {
Sound_Play(784, 250); // Frequency = 784Hz, duration = 250ms
// 1 min delay to activate the PIR module
void Wait_1minute(){
char i;
for(i = 0; i < 60; i++){
void main() {
ANSELB = 0; // Configure PortB pins as digital
ANSELC = 0; // Configure PortD pins as digital
TRISB = 0x01; // Configure RB0 as input
TRISC = 0; // Set PortC as output
LATB = 0x00; // Reset PortB
Sound_Init(&PORTB, 2); // Initialization of sound port
LAMP = 0;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
text = "LET'S THINK BINARY"; // Define the first message
Lcd_Out(1,2,text); // Write the first message in the first line
text = "www.electronify.org"; // Define the second message
Lcd_Out(2,1,text); // Write the message in the second line
text = "Home alarm system"; // Define the third message
Lcd_Out(3,2,text); //Write the message in the third line
// Wait_1minute(); //1 min delay to activate the PIR module
// IN PRACTICE (See the datasheet)
if (PORTB.B0) // If sensor is activated
LAMP = 1;
Tone1(); // Play tone
Lcd_Out(4,1,"LAMP ON");
Lcd_Out(4,13,"BUZZ ON");
Lcd_Out(4,1,"LAMP OFF");
Lcd_Out(4,13,"BUZZ OFF");
} // End of while
} // Emd of proram

Home Security Alarm System using PIC18F45K22 AND PIR Motion Sensor2 (Schematic Diagram)


Results :

After the c code is successfully compiled, a HEX file is generated.

For simulating with PROTEUS ISIS hit run button and click the logic button to 1, which will indicate that motion is detected and you will get above output .Now let’s make the logic state to 0, which will indicate that no motion detected, as shown in the simulation.

PIR sensor is ideal for alarm systems, light switch controllers, and similar systems where human presence needs to be detected.

Resource :

You can download the MikroC  Source Code and Proteus files etc from here HOME SECURITY USING pic:
This Our Group (thanks to join and participate) : https://www.facebook.com/groups/816450708460518/
Facebook page : https://www.facebook.com/Lets-Think-Binary-1728910547340654/
Youtube Channel (thanks to subscribe) :


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.