Motion detection alarm using a PIR sensor module with a PIC Microcontroller


This project describes a motion sensor alarm based on a Passive Infra-Red (PIR) sensor module. There are many vendors that manufacture the PIR sensor modules and almost all of them are pretty much the same in function. They have a single output that goes high (or low, based on specification) when the motion is detected. In this project, a PIC12F635 microcontroller continuously monitors the output from the sensor module and turns a buzzer on when it goes active.


Certain crystalline materials have the property to generate a surface electric charge when exposed to thermal infrared radiation. This phenomenon is known as pyroelectricity. The Passive Infra-Red (PIR) sensor module works on the same principle. The human body radiates heat in the form of infrared radiation which is maximum at about 9.4 um. The presence of human body creates a sudden change in the IR profile of the surrounding that is sensed by the pyroelectric sensor. The PIR sensor module has an instrumentation circuit on board that amplifies this signal to appropriate voltage level to indicate the detection of motion.

The PIR sensor requires an initial stabilization time of about 10 to 60 seconds in order to function properly. During this time, the sensor gets familiar with the surrounding environment, and any motion in its field of view should be avoided. The PIR sensor has a typical range of 20 feet, and is designed to adjust to slowly changing conditions such as the gradual change in the thermal profile of the surrounding as the day passes. However, any sudden change in the profile (e.g. human body motion) is responded by the sensor. That’s why the PIR sensor module should not be placed near a heater, AC outlet or anything that could create a rapid change in the surrounding environment.

PIR sensor modules usually have a 3-pin connection: Vcc, Output, and Ground. The pinout may vary, so I recommend to check the manufacturer’s datasheet to confirm the pins. Sometime, they do have labels on the board next to the pins. The one I have got does so, and it can be powered through 5-12 V supply as it has its own voltage regulator on board. The output goes high when the motion is sensed.

Besides it also has a 3-pin jumper selection for single or continuous trigger output mode. The two positions have labels H and L. When the jumper is at H position, the output remains high when the sensor is re-triggered repeatedly. In position L, the output goes high and low every time the sensor is triggered. So a continuous motion will give repeated high/low pulses in this mode. The front part of the sensor module has a Fresnel lens to focus the infrared light on to the sensor element.

Circuit Design and Construction

The circuit diagram is quite simple. I have powered my circuit with 4 AA batteries that gives 6 V supply. A diode is used in series to drop the voltage down to 5.4 V as the operating voltage for the PIC microcontroller should be below 5.5 V. Besides, the diode also provides the protection to the circuit in case of reverse polarity of the power supply. I have tested the circuit with NI-MH rechargeable batteries (that gives 4.8 V) and it worked, but I recommend to use the alkaline batteries (1.5 V each) for better performance. You can also use a 9 V battery but then you need a LM7805 regulator IC in your circuit.

The output of the PIR sensor module is monitored through GP5 (pin 2) of PIC12F635. When the motion is sensed, this output is high at about 3.3 V (my sensor module has a 3.3V regulator IC on board). You could still use this voltage as a valid logic high for PIC12F635, but I preferred to use this voltage to drive the base of an NPN transistor (BC547) so that at the collector we will have the full swing of the logic voltages. Now, the microcontroller monitors the voltage at the collector of the transistor. During the normal condition, the transistor is cut off, and the collector output is at logic high (+5 V). When the motion is sensed, the high output from the sensor module saturates the transistor and the voltage at the collector drops down to logic low. The jumper selection for trigger is at H position, so the sensor output will remain active as long as the motion exists. Note that the PIC12F635 microcontroller uses the internal clock oscillator at 4.0 MHz. The MCLR function is disabled and WDT is OFF in this project.

An LED is connected to port GP4 with a current limiting resistor in series. The LED blinks 3 times when the power is turned on. This is to indicate that the system has been powered up. Port pin GP2 drives a piezoelectric buzzer. A piezoelectric buzzer gives the maximum output sound pressure at its resonant frequency. The piezo buzzer I have used is EFM-290ED that has the resonant frequency of  3.4 ±0.5 KHz. After playing with it a little bit, I found the maximum output sound at about 3725 Hz.  Although the specification says the operating voltage is from 7-12 V, it creates fairly loud sound when powered with just 5 V.


The firmware is written in C and compiled with MikroC Pro for PIC. When the power is first turned on, the LED blinks 3 times, indicating the system has been powered on. Then 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 3725 Hz square wave. MikroC has built-in library for generating sound (Sound_Play()). One design question could be how long you want to turn the siren on when the motion is sensed. It depends on how you program the microcontroller. What I have done is this. 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 10 more seconds but with a slightly different frequency (3570 Hz). And if it detects the motion again, it will drive the piezobuzzer at its peak resonant frequency (3725 Hz). In MikroC, you can select the configuration bits through a Edit Project window (Project -> Edit Project). This project uses internal clock source at 4.0 MHz, MCLR disabled, and WDT OFF.

  Project: PIR Motion Sensor Alarm (PIC12F635)
  Piezo: EFM-290ED, 3.7 KHz connected at GP2
  PIR sensor module in retriggering mode
  Internal Clock @ 4.0 MHz, MCLR Disabled, WDT OFF

sbit Sensor_IP at GP5_bit; // sensor I/P
sbit LED at GP4_bit;       // LED O/P
unsigned short trigger, counter;

void Get_Delay(){

void main() {
 CMCON0 = 7;
 TRISIO = 0b00101000;  // GP5, 5 I/P's, Rest O/P's
 GPIO = 0;

 // Blink LED at Startup
 LED = 1;
 LED = 0;
 LED = 1;
 LED = 0;
 LED = 1;
 LED = 0;

 Delay_ms(60000); // 45 Sec delay for PIR module stabilization

 counter = 0;
 trigger = 0;
 do {
   while (!Sensor_IP) {  // Sensor I/P Low
    Sound_Play(3725, 600);
    trigger = 1;
    counter = 0;
   if (trigger) {
    Sound_Play(3570, 600);
    counter = counter+1;
    if(counter == 10) trigger=0;
}  // End main()

Download HEX file


The finished project is shown below.

Related Posts


  • i want to program a microcontroller using mikro c pro such that a sensor when detect a person, the led turn on in proteus but the problem is that in pir sensor there is testpin which act as an input signal ,how can i configure it so that when i press the toggle of pir sensor the led turn it on?

  • hi..this is rakhav.. i am not getting this pic12f635 anywhere so, can u just suggest me some other pic for doing this project with the same code

  • you can make video of this and also how burn program in ic

  • i connect pir sensor with transistor bc547 and buzzer only three item connection diagram

  • Can I use PIC12F675 instead of PIC12F635 here ? will the hex file work?

  • The project is really good. Can you provide me the HEXFILE to my above E mail.

    With regards,

  • what is the cost estimation of this project ?

  • i was wondering if this would work on a corn hole board. i am trying to figure out a sensor that would pick up the bags going through the hole and making a light flash. i figured this site might get me some ideas to get going in the right direction. thanks

  • Hey there,

    Just compiled the provided C source in MicroC for PIC12F683 and loaded HEX file to the sensor. It should be blinking for 3 times but no blink at all on the other hand the alarm works properly after specified amount of seconds. What should I do to make it work? Should the source code be different for PIC12F683?


  • can you give me an idea on how can i add a automatic lock, so that when the alarm and the lock will activate at the same time

  • Hi,
    Anyone know here why i cant compile it on arduino IDE?
    How to compile it on arduino IDE?

  • hi,

    i’m seekink the PIR sensor for proteus library,can you give me whan i can find it

  • HI,
    please can u attach the Proteus file?

  • Raj! Can you use interrupt along TMR0 for keep the time it is going on.. the buzzer?? surely!

    tell me what do you think


  • 9ce Design but I think a display can also be added to show d number of times motion was detected.

  • can it be used to detect fire..? Because fire spark has ir in flame spark

  • This is good project i’vd know about PIR sensor interfacing thanks

  • can you provide circuit and code for 8051 interfacing for pir motion detection???????

  • hi!, am using the pic12f675 to activate a relay.. what are the changes i need to done in this project ckt and code

  • hello,
    I am using an PIR sensor module and want to interface with the 8051, I want to adjust the delay timing of the module can any one please tell, also how to use the H l jumper.

  • Hi! Is it possible to active a relay when human is detected?


  • Muazu JIbrin musa

    Thank you for this important circuit.

    Can I pls. Have the detaile explanation on the circuit operation through my email?

    I will really appreciate that.

    Thanks a lot.

  • please can u help organise and compile the code. please the zipped file containing the asm and hex file is prefarable. you can help send to my box. thanks in advance.

  • can you provide any circuit or code for 8051???for pir motion detection

  • Hello! i tried this, but the led doesn’t flash at all, and stay always on… anyway the buzzer starts.. 🙂

    What would be the problem?

    thank you!

  • i wanted the source code in .asm format.. the assembly language one.. As i have a project to handle.. pls help if possible

  • Pingback: Shift register interfacing with pic micro controller

  • well it’s really a good one but the problem is that here in karachi pic12f635 is not available so can u please tell me the possible replacement for the pic. please tell me.i’am waiting for ur reply b/c i need to do this project please.

  • This can be done simply and cheaply with a couple of NPNs and a 556 timer. Use one side of the 556 for the buzzer, and one side for the warmup delay before buzzer is activated. You can also activate a relay to switch on a relay and activate a more powerful buzzer and even latch it on until power is removed. A Micro-controller is only necessary if you want to get fancy with the timing.

  • I am using a pic16f628a, please can u write a code for me same as above but using pic16f628a for PIR sensor.Thank you.

  • by the way, i think it is possible not to use PIC anymore in this circuit. Just amplify the signal from the PIR motion sensor tru a transistor and then put relay or buzzer with it.

  • does the day time heat affects the stability of the sensor, if i will install it at the gate of our house or shop? or it is only for indoor use?

  • Hi,

    I am using a pic16f628a, please can u write a code for me same as above but using pic16f628a for PIR sensor.

  • Hi,
    I was trying to learn the usage of a PIR sensor.I got enough idea about its Usage.

  • Pingback: Blog » Blog Archive » Motion sensor alarm with PIC Micro

Leave a Reply

Your email address will not be published. Required fields are marked *