This is a simple example program of how one could use RFID to operate a door lock.  What you will need:

  • Arduino with USB cable
  • Arduino sketches, shown below
  1. Upload the sketch below and connect the RFID reader from sout to pin 8 and enable to pin 7.  Wave the key that you want to be the master key and take note of its code.

// RFID reader for Arduino
#include
int val = 0;
char code[10];
int bytesread = 0;
#define rxPin 8
#define txPin 1
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);


void setup() {
RFID.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(7,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(7, LOW); // Activate the RFID reader
}


void loop() {
if(RFID.available() > 0) { // if data available from reader
if((val = RFID.read()) == 10) { // check for header
bytesread = 0;
while(bytesread 0) {
val = RFID.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
RFID.print("Keycode is: "); // possibly a good TAG
RFID.println(code); // print the TAG code
RFID.flush();
}
bytesread = 0;
digitalWrite(7, HIGH); // deactivate the RFID reader for a moment so it will not flood
delay(10000);
RFID.flush();
digitalWrite(7, LOW); // Activate the RFID reader
}
}
}

2.  Once you know the code for what you want to be the master key you will modify and upload the following sketch.

#include
String keyCode;
int val = 0;
char code[10];
int bytesread = 0;
String Master = "xxxxxxxxxx"; //Stores the master key used for programming the other keys
String empty = "0000000000";
#define NumberOfKeys 30
String accessList[NumberOfKeys];
boolean programmingMode,isInAccessList,KeyFound = false;
boolean keyTrue = false;
//DEFINES//
#define BLINKS 3
#define ENABLE 9
//LED Indicators
#define OPEN_LIGHT 10 //LED indicator. Indicates the correct key was entered
#define DENIED_LIGHT 11 //LED indicator. Indicates the wrong key was entered
#define PROGRAMMING_LIGHT 12 //LED indicator. Indicates Programming mode is activated
#define rxPin 8
#define txPin 1
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
void setup()
{
RFID.begin(2400);
pinMode(OPEN_LIGHT,OUTPUT);
pinMode(DENIED_LIGHT,OUTPUT);
pinMode(PROGRAMMING_LIGHT,OUTPUT);
pinMode(ENABLE,OUTPUT);
for(int i = 0;i 0) { // if data available from reader
if((val = RFID.read()) == 10) { // check for header [Header of data = 10]
bytesread = 0;
while(bytesread 0) {
val = RFID.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
digitalWrite(ENABLE, HIGH); // deactivate the RFID reader for a moment so it will not flood
keyCode = code;
keyCode = keyCode.substring(0,10);

/*** INTERPRETATION OF DATA /
//At this point, data was read, and there is a keycode in the keyCode variable
if((keyCode == Master) && (programmingMode == false))
{programmingMode = true;
digitalWrite(PROGRAMMING_LIGHT,HIGH);
}
else
{if(programmingMode == false)
{
for(int i = 0;i {
if(keyCode.equals(accessList[i])){
KeyFound = true;} //if the key is in the access list, KeyFound = true;
}
if(KeyFound)
{
accessGranted();
KeyFound = false;
}
else{
accessDenied();
}
}
else if(programmingMode)
{
if(keyCode.equals(Master)){ //Master Key swipped during programming mode
}else
{
isInAccessList = false; //this is an initialization value, if the key is in the access list, this variable will be true later.
for(int i = 0;i< NumberOfKeys;i++) //Scans Access List
{if(keyCode.equals(accessList[i])) //if the card read is in the Access List
{
accessList[i] = empty; //delete it
isInAccessList = true; //isInAccessList set to true, meaning the key was in the access list
keyDeleted();
}
}
if(isInAccessList == false) //if the key swipped is not in the access list and...
{
for(int i = 0;i< NumberOfKeys;i++){
if(accessList[i].equals(empty)) //...if current Access List Slot is Empty
{accessList[i] = keyCode; //Enter Key into Access List Slot
newKeyEntered();
break;
}
if(i == (NumberOfKeys - 1)) //if you get to this point at last interation of loop, access List is full
listFull();
}
}
}
digitalWrite(PROGRAMMING_LIGHT,LOW);
programmingMode = false; //Programming is done, turn programming mode and indicator off
}
}
resetIndicators();
} //end of data reading
}
}
}

/
CLEARS DATA FROM BUFFERS TO AVOID DOUBLE READS/
void flushBuffers(){
while(RFID.available() > 0) //if there is still data remaining in the RFID reader buffer
RFID.read(); //read it into arduino buffer
RFID.flush(); //then flush it
}


/
RESETTING INDICATORS /
void resetIndicators(){
delay(500); //wait a little
flushBuffers(); //clear buffers
digitalWrite(OPEN_LIGHT,LOW);
digitalWrite(DENIED_LIGHT,LOW);
digitalWrite(ENABLE,LOW);
}


/
BEHAVIOR OF LED INDICATORS IF A KEY IN THE ACCESS LIST IS SWIPPED /
void accessGranted(){
digitalWrite(OPEN_LIGHT,HIGH);
delay(500);
}
/
BEHAVIOR OF LED INDICATORS IF A KEY THAT'S NOT IN THE ACCESS LIST IS SWIPPED /
void accessDenied(){
digitalWrite(DENIED_LIGHT,HIGH);
delay(250);
}


/
BEHAVIOR OF LED INDICATORS IF A NEW KEY IS ENTERED IN THE ACCESS LIST /
void newKeyEntered(){
for(int i = 0; i<BLINKS;i++)
{
digitalWrite(PROGRAMMING_LIGHT,LOW);
delay(250);
digitalWrite(PROGRAMMING_LIGHT,HIGH);
delay(250);
}
digitalWrite(OPEN_LIGHT,HIGH);
}


/
BEHAVIOR OF LED INDICATORS IF A KEY IS DELETED FROM ACCESS LIST /
void keyDeleted(){
for(int i = 0; i<5;i++)
{
digitalWrite(PROGRAMMING_LIGHT,LOW);
delay(250);
digitalWrite(PROGRAMMING_LIGHT,HIGH);
delay(250);
}
digitalWrite(DENIED_LIGHT,HIGH);


}


/
BEHAVIOR OF LED INDICATORS IF ACCESS LIST IS FULL ***/
void listFull(){
for(int i = 0; i<BLINKS;i++)
{
delay(250);
digitalWrite(PROGRAMMING_LIGHT,HIGH);
digitalWrite(DENIED_LIGHT,HIGH);
digitalWrite(OPEN_LIGHT,HIGH);
delay(250);
digitalWrite(PROGRAMMING_LIGHT,LOW);
digitalWrite(DENIED_LIGHT,LOW);
digitalWrite(OPEN_LIGHT,LOW);
}
digitalWrite(PROGRAMMING_LIGHT,HIGH);
digitalWrite(DENIED_LIGHT,HIGH);
digitalWrite(OPEN_LIGHT,HIGH);
}

3. Now you can choose to modify the sketch to add in code to open or close a relay to open and close a door, etc. The default configuration is RFID Reader Sout –>pin 8, Enable –> pin 9, Open (green) light –> pin 10, Denied (red) light –> pin 11, Programming Light (blue) –> pin 12.  To operate the program wave the master key over the reader and the blue light should turn on, if not check to make sure that you changed the default code where it says “String master =”xxxxxxxxxx”;”.  Once the blue light is on swipe a key which you want to add or remove to the system. If the key is in the system it will be removed, if not it will be added.  Out of programming mode swiping a know key will light the green light, and an unknown key the red light.

4. Where do we go from here? I decided to take this a bit further and create two products using custom PCBs.

The design files for both are here. This zip file includes Fritzing design files for an Arduino shield and a standalone arduino with the same lights.

If you would like to buy these PCBs either fully populated or unpopulated please email me: michael@mikedombrowski.com

Leave a Reply