PIC Basic - Interfacing Motion Detector PIR SE-10

This project is first part of a series of projects designed to experiment and understand how PIC microcontroller can be effectively used to interface with external circuit and get data from and control external devices. Once you have completed hands on experiment with this series you should able to design a minimal robot or other such relatively complex programmable control system.

To see a list of all basic PIC projects planned in this series please click here. To get a basic understanding of PIC microcontrollers and how to use them please click here. This link covers the basic FAQ of PIC for newbies as there is a lot of literature available on the web for advanced users. If you are new to PIC world it is strongly recommended to visit the above link before proceeding further.

In this project a Passive Infrared (PIR) sensor (SE-10) is used to detect human movement and trigger turning on a light or an alarm. A typical PIR sensor comes pre-packaged with all the logic required to, detect IR radiation from living being and raise alarm. SE-10 has just three pins, two for power supply and third for alarm. So, all that is required to do is to connect it to the power supply and connect the alarm pin to a microcontroller pin to detect any changes. The alarm pin is a collector drain (goes to 0V when alarm is set) hence the MCU pin must be pulled up (connected to + vdd through a 10K ) to detect changes when alarm is triggered.

The C program is written to get interrupt when the alarm is triggered and turns ON an LED. The LED is set ON for a few seconds and then turned OFF. The duration can be modified in the program as required.

There is one gotcha you need to be aware of using SE-10. Though SE-10 datasheet specifies the power supply as 5-12V, it works erratically when 5V supply is given (spend couple of days trying to figure this out). But I could get it working alright with 9V supply.

Probably you may not need a PIC to control a PIR, but if you are using it with day light detection and other complex logic it will make it much easier and accurate to use a PIC.

SE-10 can detect motion up to about 20 feet and effective for a large office or bedroom. To read more about how PIRs work click here

Also go through the datasheets for PIC16F84A to get a good understanding of other details of the microcontroller.

The C program code main.
#define _XTAL_FREQ 0x3D0900
#include <htc.h>

__CONFIG(XT & WDTDIS & PWRTDIS & UNPROTECT);

unsigned int tmr_ticked = 0;
bit volatile switch_debounced;
void init(void)
{
switch_debounced=1;
T0CS =0;
RBIE=1; //enable portb interrupts
PSA = 0;//Set prescaler to 1:256
PS0 = 0;
PS1 = 0;
PS2 = 0;
T0IE = 1; //Enable timer 1 interrupts
ei(); // initialising interrupts
// port directions: 1=input, 0=output
TRISB = 0b11110000;
PORTB=0b00001111;
switch_debounced=1;
}

void main(void)
{
init();
while (1)
{
}
}
The C program interrupt code.
#include <htc.h>

extern unsigned int tmr_ticked;
extern bit volatile switch_debounced;

void interrupt my_int(void)
{
if(T0IE && T0IF)
{
//debouncing routine
tmr_ticked++;
if(tmr_ticked>4000)
{
PORTB = 0b00000000;
tmr_ticked=0;
switch_debounced=1;
RBIF=0; // ensuring this change is not picked up as an interrupt
}
T0IF=0;
}
if (RBIE && RBIF)
{
// Here interrupt from motion detector is treated as the switch
if( switch_debounced == 1)
{
//figuring out which switch is pressed and turning the right LED on
if(PORTB & 0b00010000)
{
PORTB=0b00001111;
switch_debounced=0;
tmr_ticked=0;
}
}
RBIF=0;
}

return;
// process other interrupt sources here
}

HEX file for project click here