RFID Based Door Lock System using AVR

We are designing rfid door lock project using the AVR ATmega8. In this project, we have a RFID tag as a key to open the door. We can open and close the door wirelessly in a short distance without the need of any physical key as we need in the traditional lock. We can have the same RFID card tag to open multiple doors in an office or at home.

Each RFID(Radio Frequency Identification Device) tag has a unique value. When this card come in a range of the RFID reader, the tag transmits that unique code to the receiver wirelessly. The RFID receiver receives the tag value and sends this to the AVR ATmega8 serially.

RFID based automatic door locking system project can be designed by using a different microcontroller like AVR, Arduino, Raspberry Pie or 8051. RFID door lock system circuit is very easy to understand if we know the serial communication.

EM18 RFID Reader

This is the very common reader used in many rfid projects. It features very low cost, small size, low power consumption. It communicates with the microcontroller using the UART protocol. It radiates the 125khz carrier frequency through its coil. When a tag comes closer to this reader. The tag antenna gets energised and gets the sufficenet energy to drive the chip that has a unique id. This small RF circuit will then send the unique id to the EM18 RFID reader.

How to Interface RFID Reader with Microcontroller

The interfacing rfid reader with avr using the UART protocol. This is a serial protocol with two wire RX and TX. When a reader reads the tag value it will send the unique tag value to the TX pin. This TX pin is connected to the RX pin of AVR microcontroller. To receive the signal correctly, we set the microcontroller to the 9600 baud rate. The microcontroller may work in different frequency so make sure we managed to operate it in desired baud rate 9600. Here the AVR running at the 12MHz frequency.

RFID Door Lock System Circuit

RFID Based Door Lock System using AVR Code

//PIN configuration
//PD4 -> D4
//PD5 -> D5
//PD6 -> D6
//PD7 -> D7
//PD2 -> RS
//PD3 -> RW
//PC5 -> E
//RFID TX to RX of Atmega8
//L293D for door lock and unlock
//PC0 => 2 pin of l293d
//PC1 => 7 pin of l293d

#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h> // includes delay header file
#include <avr/interrupt.h>
#include “lcd.h”
//This function is used to initialize the USART
//at a given UBRR value
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;
}

void read_tag_value();
void match_tag_value();
void lock();
void unlock();
void stop();
void delay();
char read_tag[12];
char tag1[12]={‘8′,’4′,’0′,’0′,’8′,’1′,’5′,’B’,’8′,’C’,’D’,’2′};
char tag2[12]={‘8′,’4′,’0′,’0′,’8′,’1′,’5′,’B’,’8′,’A’,’D’,’4′};
int status=0;
int valid=0;
int main()
{
//Motor connected at PC0 and PC1
DDRC=0b0000011;
USARTInit(77);
lcd_init(LCD_DISP_ON);
lcd_gotoxy(0,0);
lcd_puts(“Door Lock System”);
lcd_gotoxy(0,1);
lcd_puts(“www.maxphi.com”);
while(1)
{   valid=0;
//Read the RFID data
read_tag_value();
//Match tag value
lcd_gotoxy(0,0);
match_tag_value();
if(status && valid==1){
lcd_puts(“Door Locking….”);
lock();
}
else if(valid==1){
lcd_puts(“Door Unlocking…”);
unlock();
}
_delay_ms(800);
lcd_gotoxy(0,0);
lcd_puts(“Swipe Your Card”);
}
return 0;
}
void read_tag_value(){
for(unsigned char i=0;i<12;i++)
{
read_tag[i]=USARTReadChar();
}
}

void match_tag_value(){
if((read_tag[0]==tag1[0])&&(read_tag[1]==tag1[1])&&(read_tag[2]==tag1[2])&&(read_tag[3]==tag1[3])&&(read_tag[4]==tag1[4])&&(read_tag[5]==tag1[5])&&(read_tag[6]==tag1[6])&&(read_tag[7]==tag1[7])&&(read_tag[8]==tag1[8])&&(read_tag[9]==tag1[9])&&(read_tag[10]==tag1[10])&&(read_tag[11]==tag1[11]))
{
status=~status;
valid=1;
}
else
{
lcd_puts(“Invalid Card…..”);
delay();
}
}
void lock(){
PORTC|=(1<<PORTC0);
PORTC&=~(1<<PORTC1);
delay();
stop();
}
void unlock(){
PORTC&=~(1<<PORTC0);
PORTC|=(1<<PORTC1);
delay();
stop();
}
void stop(){
PORTC|=(1<<PORTC0);
PORTC|=(1<<PORTC1);
}
void delay(){
for(int i=0;i<=150;i++)
_delay_ms(100);
}

One Comment on “RFID Based Door Lock System using AVR”

Leave a Reply

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