PIR Sensor and GSM Based Security System using AVR Micrcontroller

PIR Sensor and GSM Based Security System is very secure security system to detect the unwanted person in a restricted area. When someone is detected by the PIR sensor, an alarm will fire and a Call will be made to the owner or concerned person.

GSM Modem Interfacing with AVR Microcontroller

GSM Modem Interfacing with AVR Microcontroller is done using the serial communication. We are using ATmega8 for this serial communication and we call it UART protocol in the more technical way. Each microcontroller has TX and RX pins for UART communication. In ATmega8 PD0 pin is RX pin and PD1 pin as a TX pin.

When a serial communication is to be made with any microcontroller and with the device. We cross connect both microcontroller and the device. But here the communication is from the ATmega8 to the GSM modem. GSM modem will receive only the command that will be sent by the AVR after the person detected. No communication from the GSM to AVR so we do not connect the TX of GSM to AVR. Instead of that, we connect the ground of both devices to each other.

We are using the SIM 300 modem for this PIR Sensor and GSM Based Security System project. And this is working on 9600 default baud rate. So AVR will also send the all AT commands to this baud rate otherwise communication will be wrong. And there will be no call from the GSM modem

The AVR in this project is working on 12MHz.

PIR Sensor Interfacing with AVR

PIR sensor interfacing with atmega8 is done using the interrupt. By using the interrupt to sense the PIR sensor reading gives the reliability to this project. We use the concept of interrupt to read the signal because this is important to serve this signal anyhow when a trespasser is present. ATmega8 has two interrupt pin INT0 and INT1. We have used interrupt INT0.

This interrupt can rise on different condition of input like:

  • Any Logic Change in Input
  • Falling Edge of Input
  • Low Level of Input
  • Rising Edge of Input

How Interrupt Works in Microcontroller

When an interrupt occurs, microcontroller halt/pause the currently executing task(or less important) and control send to serve the interrupt. And a certain code will be executed on receiving the interrupt. The place where this important code is present is called the Interrupt Service Routine(ISR).

We also have the ISR in our program where we triggered the alarm and execute the small code to call the owner. See how ISR looks in our code.

ISR(INT0_vect)
{
PORTB=0b00000001;
USARTWriteChar(‘A’);
USARTWriteChar(‘T’);
USARTWriteChar(‘D’);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘+’);
USARTWriteChar(‘ ‘); //2-digit Country Code
USARTWriteChar(‘ ‘); //2-digit Country Code
USARTWriteChar(‘ ‘); //Phone Number to Call
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘); //Phone Number to Call
USARTWriteChar(‘;’);
USARTWriteChar(‘ ‘);
}

In the above ISR, we first make an alarm the send the call. GSM modem understands only AT commands.

So for call the following is the AT command: ATD + 91xxxxxxxxxx;

Here +91 is country code and xxxxxxxxxx is the phone number to call.

PIR Sensor and GSM Based Security System Circuit

PIR Sensor and GSM Based Security System AVR Code

#include<avr/io.h>
#include<util/delay.h>
#include <inttypes.h>
#include <avr/interrupt.h>
void USARTInit(uint16_t ubrr_value)
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UCSRB=(1<<RXEN)|(1<<TXEN);
}
char USARTReadChar()
{
while(!(UCSRA & (1<<RXC)))
{
//Do nothing
}
return UDR;
}
void USARTWriteChar(char data)
{
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
UDR=data;
}

int main(void)
{
USARTInit(77);          //9600 baud at 12MHz Crystal
DDRB=0xFF;              //Alarm at PB0
DDRD &= ~(1 << DDD2);   // Clear the PD2 pin
PORTD |= (1 << PORTD2); // turn On the Pull-Up

//The rising edge of INT0 generates an interrupt request
MCUCR |= (1 << ISC00);  // set the ISC00 bit
MCUCR |= (1 << ISC01);  // set the ISC01 bit
GICR |= (1 << INT0);    // Turns on INT0
sei();                  // turn on interrupts
while(1)
{

}
return 0;
}
//interrupt code here
ISR(INT0_vect)
{
PORTB=0b00000001;
USARTWriteChar(‘A’);
USARTWriteChar(‘T’);
USARTWriteChar(‘D’);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘+’);
USARTWriteChar(‘ ‘); //2-digit Country Code
USARTWriteChar(‘ ‘); //2-digit Country Code
USARTWriteChar(‘ ‘); //Phone Number to Call
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘);
USARTWriteChar(‘ ‘); //Phone Number to Call
USARTWriteChar(‘;’);
USARTWriteChar(‘ ‘);
}