Hello friend and i new here..i have problem with my project..i already do home alarm system using PIR sensor and GSM..this is my schematic
my problem is project do not work....i do not know how to interface PIR sensor module with PIC because i have try connect directly to pin PIC and it not work..i hope you can help me to solve this problem. thank you.
//
// Set of AT commands
const char ATat[] = "AT"; // Every AT command starts with "AT"
const char ATecho[] = "ATE0"; // Disable command echo
const char ATtxt[] = "AT+CMGF=1"; // Change SMS mode to Text message mode
const char ATphone[]= "AT+CMGS="123456789""; // Send message to cell number : 123456789 (Enter your number instead of 123456789!)
// Alarm message
const char alarm_Message[] = "Alarm!! Intruder Detected in your Home!!!";
//
// Responses to parse
const GSM_OK = 0;
const GSM_Ready_To_Receive_Message = 1;
//Declare the pins to use for PIR, Push Button and Siren
sbit PIR at RB0_bit;
sbit PIR_Input_Direction at TRISB0_bit;
sbit Push_Button at RB1_bit;
sbit Push_Button_Direction at TRISB1_bit;
sbit Siren at RB2_bit;
sbit Siren_Direction at TRISB2_bit;
sbit Status_LED at RB3_bit;
sbit Status_LED_Direction at TRISB3_bit;
#define ON 1;
#define OFF 0;
char gsm_state = 0;
char response_rcvd = 0;
short responseID = -1, response = -1;
// uart rx interrupt handler
void interrupt(){
char receivedByte;
if (PIR1.RCIF == 1) { // Check if we have uart rx interrupt request?
receivedByte = UART1_Read(); // Get received byte
// process reception of mode feedback, "OK" and "> " responses
switch (gsm_state) {
case 0: {
response = -1; // Clear response
if (receivedByte == 'O') // We have 'O', it could be "OK"
gsm_state = 1; // Expecting 'K'
if (receivedByte == '>') // We have '>', it could be "> "
gsm_state = 10; // Expecting ' '
break;
}
case 1: {
if (receivedByte == 'K') { // We have 'K' ->
response = GSM_OK; // We have "OK" response
gsm_state = 20; // Expecting CR+LF
}
else
gsm_state = 0; // Reset state machine
break;
}
case 10: {
if (receivedByte == ' ') {
response_rcvd = 1; // We have "> " response
response = GSM_Ready_To_Receive_Message; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
case 20: {
if (receivedByte == 13) // We have 13, it could be CR+LF
gsm_state = 21; // Expecting LF
else
gsm_state = 0; // Reset state machine
break;
}
case 21: {
if (receivedByte == 10) { // We have LF, response is complete
response_rcvd = 1; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
default: { // Unwanted character
gsm_state = 0; // Reset state machine
break;
}
}
}
}
// Send command or data to the GSM Module
void Send_to_GSM(const char *x)
{
// Send command or data string
while(*x) {
UART1_Write(*x++);
}
// Terminatation by CR(Enter)
UART1_Write(0x0D);
}
// Get GSM response, if there is any
short Get_response() {
if (response_rcvd) {
response_rcvd = 0;
return responseID;
}
else
return -1;
}
// Wait for GSM response
void Wait_response(char rspns) {
while (Get_response() != rspns)
;
}
// Send alarm SMS!
void Send_Alarm_Message(){
Send_to_GSM(ATtxt); //Set SMS mode to text
Wait_response(GSM_OK); //Wait for modem to reply "OK"
Send_to_GSM(ATphone); //Send message to cell number
Wait_response(GSM_Ready_To_Receive_Message); //Wait for modem to reply "> "
//Send the alarm message
Send_to_GSM("Alarm!!rn");
Send_to_GSM("Intruder Detected in your House!!!rn");
Send_to_GSM("Siren will be activated in few seconds.");
UART1_Write(0x1A); //Cntrl + Z to send message
UART1_Write(0x0D); // Enter
Wait_response(GSM_OK); //Wait for modem to reply "OK"
}
// 30 Seconds delay
void Wait_30S(){
char i;
for(i = 0; i < 30; i++){
Delay_ms(1000);
}
}
// 1 min delay
void Wait_1minute(){
char i;
for(i = 0; i < 60; i++){
Delay_ms(1000);
}
}
char alarm_armed = 0;
int count,i;
void main() {
WDTCON = 0;
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators
// Enable Uart Rx interrupt
PIE1.RCIE = 1;
INTCON.PEIE = 1;
INTCON.GIE = 1;
PIR_Input_Direction = 1; //Set RB0 bit as input to read the state of the PIR
Push_Button_Direction = 1; //Set RB1 bit as input to read the state of the Push Button
Siren_Direction = 0; //Set RB2 bit as output to control the Siren relay
Siren = OFF; //Switch OFF the Siren
Status_LED_Direction = 0; //Set RB3 bit as output to switch ON/OFF the status LED
Status_LED = OFF; //Switch OFF the status LED
UART1_Init(19200); // Initialize USART module
Delay_ms(100);
// synchronize baud rate
while(1) {
Send_to_GSM(ATat); // Send "AT" string until GSM862 sets up its baud rade
Delay_ms(100); // and gets it correctly
if (Get_response() == GSM_OK) // If GSM862 says "OK" on our baud rate we program can continue
break;
}
// Disable command echo
Send_to_GSM(ATecho);
Wait_response(GSM_OK);
while(1){
if (alarm_armed) { //Check if the alarm is armed!
count=0;
if(PIR) { // If motion was detected !
for( i = 0; i < 10; i++){ // Test few times if there is still motion (500ms), so you don't misfire
if (PIR) { // If motion is still detectable
count=1;
delay_ms(50);
}
else
{
count=0;
return;
}
}
if (count)
{
Wait_30S(); //Wait for 30 Seconds
Send_Alarm_Message(); // send alarm message
Wait_30S(); //Wait for 30 Seconds
if (alarm_armed ){
Siren = ON; // Activate the Siren (Relay)
}
}
}
}
if (Push_Button)
{
if (alarm_armed == 0)
{
alarm_armed = 1; //Arm the alarm system
Wait_1minute(); //If armed, wait for 1 minute before going into alarm mode
for(i = 0; i < 60; i++){ //If armed, Blink the Status LED for 1 minute before going into alarm mode
Status_LED = ~ Status_LED;
Delay_ms(1000);
}
Status_LED = ON; //In alarm mode, switch ON the status LED
}
else
{
alarm_armed = 0; //Disarm the alarm system
Siren = OFF; //Switch OFF the siren
Status_LED = OFF; //Switch OFF the status LED
}
}
}
}
// Set of AT commands
const char ATat[] = "AT"; // Every AT command starts with "AT"
const char ATecho[] = "ATE0"; // Disable command echo
const char ATtxt[] = "AT+CMGF=1"; // Change SMS mode to Text message mode
const char ATphone[]= "AT+CMGS="123456789""; // Send message to cell number : 123456789 (Enter your number instead of 123456789!)
// Alarm message
const char alarm_Message[] = "Alarm!! Intruder Detected in your Home!!!";
//
// Responses to parse
const GSM_OK = 0;
const GSM_Ready_To_Receive_Message = 1;
//Declare the pins to use for PIR, Push Button and Siren
sbit PIR at RB0_bit;
sbit PIR_Input_Direction at TRISB0_bit;
sbit Push_Button at RB1_bit;
sbit Push_Button_Direction at TRISB1_bit;
sbit Siren at RB2_bit;
sbit Siren_Direction at TRISB2_bit;
sbit Status_LED at RB3_bit;
sbit Status_LED_Direction at TRISB3_bit;
#define ON 1;
#define OFF 0;
char gsm_state = 0;
char response_rcvd = 0;
short responseID = -1, response = -1;
// uart rx interrupt handler
void interrupt(){
char receivedByte;
if (PIR1.RCIF == 1) { // Check if we have uart rx interrupt request?
receivedByte = UART1_Read(); // Get received byte
// process reception of mode feedback, "OK" and "> " responses
switch (gsm_state) {
case 0: {
response = -1; // Clear response
if (receivedByte == 'O') // We have 'O', it could be "OK"
gsm_state = 1; // Expecting 'K'
if (receivedByte == '>') // We have '>', it could be "> "
gsm_state = 10; // Expecting ' '
break;
}
case 1: {
if (receivedByte == 'K') { // We have 'K' ->
response = GSM_OK; // We have "OK" response
gsm_state = 20; // Expecting CR+LF
}
else
gsm_state = 0; // Reset state machine
break;
}
case 10: {
if (receivedByte == ' ') {
response_rcvd = 1; // We have "> " response
response = GSM_Ready_To_Receive_Message; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
case 20: {
if (receivedByte == 13) // We have 13, it could be CR+LF
gsm_state = 21; // Expecting LF
else
gsm_state = 0; // Reset state machine
break;
}
case 21: {
if (receivedByte == 10) { // We have LF, response is complete
response_rcvd = 1; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
default: { // Unwanted character
gsm_state = 0; // Reset state machine
break;
}
}
}
}
// Send command or data to the GSM Module
void Send_to_GSM(const char *x)
{
// Send command or data string
while(*x) {
UART1_Write(*x++);
}
// Terminatation by CR(Enter)
UART1_Write(0x0D);
}
// Get GSM response, if there is any
short Get_response() {
if (response_rcvd) {
response_rcvd = 0;
return responseID;
}
else
return -1;
}
// Wait for GSM response
void Wait_response(char rspns) {
while (Get_response() != rspns)
;
}
// Send alarm SMS!
void Send_Alarm_Message(){
Send_to_GSM(ATtxt); //Set SMS mode to text
Wait_response(GSM_OK); //Wait for modem to reply "OK"
Send_to_GSM(ATphone); //Send message to cell number
Wait_response(GSM_Ready_To_Receive_Message); //Wait for modem to reply "> "
//Send the alarm message
Send_to_GSM("Alarm!!rn");
Send_to_GSM("Intruder Detected in your House!!!rn");
Send_to_GSM("Siren will be activated in few seconds.");
UART1_Write(0x1A); //Cntrl + Z to send message
UART1_Write(0x0D); // Enter
Wait_response(GSM_OK); //Wait for modem to reply "OK"
}
// 30 Seconds delay
void Wait_30S(){
char i;
for(i = 0; i < 30; i++){
Delay_ms(1000);
}
}
// 1 min delay
void Wait_1minute(){
char i;
for(i = 0; i < 60; i++){
Delay_ms(1000);
}
}
char alarm_armed = 0;
int count,i;
void main() {
WDTCON = 0;
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators
// Enable Uart Rx interrupt
PIE1.RCIE = 1;
INTCON.PEIE = 1;
INTCON.GIE = 1;
PIR_Input_Direction = 1; //Set RB0 bit as input to read the state of the PIR
Push_Button_Direction = 1; //Set RB1 bit as input to read the state of the Push Button
Siren_Direction = 0; //Set RB2 bit as output to control the Siren relay
Siren = OFF; //Switch OFF the Siren
Status_LED_Direction = 0; //Set RB3 bit as output to switch ON/OFF the status LED
Status_LED = OFF; //Switch OFF the status LED
UART1_Init(19200); // Initialize USART module
Delay_ms(100);
// synchronize baud rate
while(1) {
Send_to_GSM(ATat); // Send "AT" string until GSM862 sets up its baud rade
Delay_ms(100); // and gets it correctly
if (Get_response() == GSM_OK) // If GSM862 says "OK" on our baud rate we program can continue
break;
}
// Disable command echo
Send_to_GSM(ATecho);
Wait_response(GSM_OK);
while(1){
if (alarm_armed) { //Check if the alarm is armed!
count=0;
if(PIR) { // If motion was detected !
for( i = 0; i < 10; i++){ // Test few times if there is still motion (500ms), so you don't misfire
if (PIR) { // If motion is still detectable
count=1;
delay_ms(50);
}
else
{
count=0;
return;
}
}
if (count)
{
Wait_30S(); //Wait for 30 Seconds
Send_Alarm_Message(); // send alarm message
Wait_30S(); //Wait for 30 Seconds
if (alarm_armed ){
Siren = ON; // Activate the Siren (Relay)
}
}
}
}
if (Push_Button)
{
if (alarm_armed == 0)
{
alarm_armed = 1; //Arm the alarm system
Wait_1minute(); //If armed, wait for 1 minute before going into alarm mode
for(i = 0; i < 60; i++){ //If armed, Blink the Status LED for 1 minute before going into alarm mode
Status_LED = ~ Status_LED;
Delay_ms(1000);
}
Status_LED = ON; //In alarm mode, switch ON the status LED
}
else
{
alarm_armed = 0; //Disarm the alarm system
Siren = OFF; //Switch OFF the siren
Status_LED = OFF; //Switch OFF the status LED
}
}
}
}
my problem is project do not work....i do not know how to interface PIR sensor module with PIC because i have try connect directly to pin PIC and it not work..i hope you can help me to solve this problem. thank you.
Last edited by a moderator: