Access control with RFID

In my last blog I created a library for UART communication. Now it can be used for an 125KHz RFID module. This module uses the UART to communicate with a microcontroller. After reading an RFID key it will send a 14 digit code over the UART. In this project the software reads the codes and compares it with a hard coded RFID code. When the codes are the same the user gets access. The results are shown on a lcd in this project, but it can also be send to a computer or where ever you want. See the below movie to get a view of the RFID project.

 

Mostly these types of modules are used to give someone access to a building or get some user data. Another nice thing is setting up a predifined configuration at home. For example some particular lights at home will light up for a user1 and other lights will light up when an other user scans his RFID key. Besides that it would also be fun if the TV gets turned on a particular channel corrosponding to an RFID code. The plan is setting this up in a future blog. For now see the schematic for this project below.

Schematic RFID project

 

 

The RFID module used in this project

 

 

The source code only got some minor changes compared to the last blog. See the changes below or download the full code here.

initialize.c

/*
 * initialize.c
 *
 * Created: 20-5-2017 15:07:22
 * Author: HdH
 * Description: initialization off the project
 */ 

#define PIN_LED1 6
#define PIN_LED2 7
#define PIN_LED3 8

#include <avr/interrupt.h>
#include "gpiolib.h"
#include "timer4clocklib.h"
#include "i2c.h"
#include "i2cdisplay.h"
#include "uart.h"


void initialize(){
	
	char openingText[] = "www.hdhprojects.nl";
	char rfidText[] = "RFID:";
	int i;

	cli();//interrupts disable
	tmr2_init(); // initialize timer2
	i2c_init(); // initialize i2c protocol
	i2cLcd_Init(); // initialize i2c lcd
	UART_Init(); // initialize the UART
	sei();//interrupts enable
	
	clear();
	setCursor(1,0);
	for(i=0; i<18; i++){		
		writeI2cDisplay(openingText[i], DATAREGISTER);
	}
	
	setCursor(0,1);
	for(i=0; i<5; i++){
		writeI2cDisplay(rfidText[i], DATAREGISTER);
	}
	
	pinMode(PIN_LED1,PINMODEOUTPUT); //set pin to output
	pinMode(PIN_LED2,PINMODEOUTPUT); //set pin to output
	pinMode(PIN_LED3,PINMODEOUTPUT); //set pin to output	
}

application1.c

/*
 * application1.c
 *
 * Created: 20-5-2017 15:07:22
 * Author: HdH
 * Description: Application for multi threat, show lcd clock and adc
 */

#define F_CPU 16000000UL // 16 MHz clock speed for delay.h
#define KEY1 "28005A08572D"



#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <string.h>
#include "i2c.h"
#include "i2cdisplay.h"
#include "uart.h"


/****************************************************************************
	Bit and byte definitions
****************************************************************************/

extern int alarmCode;
char sendText2a[] = "Write me!";
char accessGranted[] = "Access granted!";
char accessDenied[] = "Access denied";
char receivedCode[14];


void Application1(void){

int i;
int p;

	if (getRcvdMsgLength() > 0){
		clearDspRange(0, 2, 20); // Clear the receive line on the lcd
		
		setCursor(6,1);
		i=0;
		p = getMsgChar();
		while (p != 0){ // Write the received message on the lcd and in a buffer for comparing
			receivedCode[i] = p;
			writeI2cDisplay(p, DATAREGISTER);
			p = getMsgChar();
			i++;
		}
		
		setCursor(0, 2);
		if(strstr(receivedCode, KEY1) != NULL){ // code is ok		
			for(uint8_t q=0; q<14; q++){
				writeI2cDisplay(accessGranted[q], DATAREGISTER);
			}
		}
		else{ // code is wrong
			for(uint8_t q=0; q<13; q++){
				writeI2cDisplay(accessDenied[q], DATAREGISTER);
			}
		}
	}
}