This is a simple example program of how one could use RFID to operate a door lock. What you will need:
- Parallax RFID reader (RFID Reader)
- 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 <LiquidCrystal.h>
#include <SoftwareSerial.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backLight = 13; // pin 13 will control the backlight
int val = 0;
int x = 0;
char code[10];
int bytesread = 0;
#define rxPin 8
#define txPin 1
SoftwareSerial RFID = SoftwareSerial(rxPin, txPin);
byte backSlash[8] = {
0, 16, 8, 4, 2, 1, 0, 0
};
void setup()
{
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
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
lcd.begin(16, 2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear();
}
void loop()
{
lcd.createChar(3, backSlash);
if (x == 0) {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting For RFID");
lcd.setCursor(0, 1);
lcd.print("#.#.#. | .#.#.#.");
delay(500);
x++;
}
else if (x == 1) {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting For RFID");
lcd.setCursor(0, 1);
lcd.print(".#.#.# #.#.#.#");
lcd.setCursor(7, 1);
lcd.write(3);
delay(500);
x++;
}
else if (x == 2) {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting For RFID");
lcd.setCursor(0, 1);
lcd.print("#.#.#. - .#.#.#.");
delay(500);
x++;
}
else if (x == 3) {
//lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waiting For RFID");
lcd.setCursor(0, 1);
lcd.print(".#.#.# / #.#.#.#");
delay(500);
x = 0;
}
if (RFID.available() > 0) { // if data available from reader
if ((val = RFID.read()) == 10) { // check for header
bytesread = 0;
while (bytesread < 10) { // read 10 digit code
if (RFID.available() > 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
// lcd.setCursor(1,bytesread);
// lcd.print(val);
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
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RFID Code is....");
lcd.setCursor(0, 1);
lcd.print(code);
lcd.setCursor(10, 1);
lcd.print(" ");
RFID.flush();
}
bytesread = 0;
digitalWrite(7, HIGH); // deactivate the RFID reader for a moment so it will not flood
delay(10000);
lcd.clear();
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 <SoftwareSerial.h>
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 < NumberOfKeys; i++) {
accessList[i] = empty;
}
digitalWrite(ENABLE, LOW); // Activate the RFID reader
}
void loop()
{
if (RFID.available() > 0) { // if data available from reader
if ((val = RFID.read()) == 10) { // check for header [Header of data = 10]
bytesread = 0;
while (bytesread < 10) { // read 10 digit code
if (RFID.available() > 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 < NumberOfKeys; i++) //check entered key against saved keys
{
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.
Leave a Reply