Arduino RFID Door Lock
Once again me and one of my class mates have finished a project in school using the Arduino.
This time it’s an RFID Door Lock made with a real door and a real lock. The lock is turned using a standard 6V servo.
The project was about luxury, and we thought it might be luxurious if the door could be unlock automatically, fx when you come home from shopping, carrying a bag in each hand. This was the beginning of this project.
In the video below you can see a short demonstration of the system.
We thought you might be interested in the code, and to see how we implemented the “Master-system”, so the code can be found beneath. Remember that you should download the Servo library, and the Parallax RFID reader should be connected to the Serial RX pin (Pin 0).
#include <EEPROM.h>
#include <Servo.h>
//Initialiser
Servo servoLock; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define RFID_Enabled_Pin 2 // sets RFID enable pin to pin 2
#define servoPin 3 // sets the servo's pin to pin 3
#define LockedPos 10
#define UnlockedPos 110
byte buffer[100]; //used to store the incoming bytes from the RFID
byte RFID_Master[10] = {'1', '7', '0', '0', '7', 'D', 'B', '2', '4', 'F'}; //the secound RFID fob (key) to look for
byte RFID_Slave1[10], RFID_Slave2[10], RFID_Slave3[10]; //the first RFID fob (key) to look for
byte i; //used to keep track of which bit to write to
byte i2; //used to erase the RFID fob (key) number from the buffer
boolean DoorLocked; //true if door is locked
byte checkPosition; //used to check if the incomming number is true or false
boolean RFID_Master_Correct; //true if it's the right RFID fob (key), false if not
boolean RFID_Slave1_Correct, RFID_Slave2_Correct, RFID_Slave3_Correct; //true if it's the right RFID fob (key), false if not
boolean RFID_SaveNextRead;
void setup() {
pinMode(13, OUTPUT); //enables the diode on the arduino
pinMode(RFID_Enabled_Pin, OUTPUT); //sets the RFID pin to output
RFID_Enable(false); //used to set the status of the RFID reader
EEPROM_Read_Slaves();
//Lock
PreLock(); //locks the arduino on startup
RFID_Enable(true); //used to set the status of the RFID reader
Serial.begin(2400); //sets baudrate
i = 1; //sets the varible to 1, and thereby skip the start byte (0x0A)
}
//Go To
//Loop
void loop() { //the main loop
// Serial data
if (Serial.available()) { //check if the RFID reader sends anything
if (buffer[0] != 0x0A) { //check if it the start bit is 0x0A
buffer[0] = Serial.read(); //write bit to buffer
} else {
//Recieve
buffer[i] = Serial.read(); //write next bit to buffer
if (buffer[i] == 0x0D) { //if end bit is send, disable the RFID reader temporary
Serial.print("RFID Tag scanned: ");
RFID_Enable(false);
RFID_Master_Correct = true;
RFID_Slave1_Correct = true;
RFID_Slave2_Correct = true;
RFID_Slave3_Correct = true;
//RFID ID
// We have read all bytes - we are now going to check them
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
Serial.print(buffer[checkPosition+1], BYTE);
if (buffer[checkPosition+1] == RFID_Slave1[checkPosition] && RFID_Slave1_Correct == true) { // compares the written bits to "RFID1"
RFID_Slave1_Correct = true; //Slave1 RFID tag is detected
} else {
RFID_Slave1_Correct = false; //Slave1 RFID tag is not detected
}
if (buffer[checkPosition+1] == RFID_Slave2[checkPosition] && RFID_Slave2_Correct == true) { // compares the written bits to "RFID1"
RFID_Slave2_Correct = true; //Slave2 RFID tag is detected
} else {
RFID_Slave2_Correct = false; //Slave2 RFID tag is not detected
}
if (buffer[checkPosition+1] == RFID_Slave3[checkPosition] && RFID_Slave3_Correct == true) { // compares the written bits to "RFID1"
RFID_Slave3_Correct = true; //Slave3 RFID tag is detected
} else {
RFID_Slave3_Correct = false; //Slave3 RFID tag is detected
}
if (buffer[checkPosition+1] == RFID_Master[checkPosition] && RFID_Master_Correct == true) { // compares the written bits to "RFID1"
RFID_Master_Correct = true; //Master RFID tag is detected
} else {
RFID_Master_Correct = false; //Master RFID tag is detected
}
}
Serial.println("");
if (RFID_SaveNextRead == false && (RFID_Slave1_Correct == true || RFID_Slave2_Correct == true || RFID_Slave3_Correct == true) && RFID_Master_Correct == false) { //see if the right RFID fob (key) is detected
if (RFID_Slave1_Correct == true) { Serial.println("Slave1 Card Scanned"); }
if (RFID_Slave2_Correct == true) { Serial.println("Slave2 Card Scanned"); }
if (RFID_Slave3_Correct == true) { Serial.println("Slave3 Card Scanned"); }
//Door
if (DoorLocked == true) { //see if door is locked or not
//Unlock
Serial.print("Unlocking..."); //if the door is locked then unlocked it
Unlock(5); //unlock with 5ms delay
Serial.println(" Unlocked!");
} else {
//Lock
Serial.print("Locking..."); //if the door is unlocked then lock it
Lock(5); //lock with 5ms delay
Serial.println(" Locked!");
}
//Vent
delay(1000); // Wait for you to remove the RFID fob (key)
} else if (RFID_Master_Correct == true && RFID_SaveNextRead == false) { // If the Master Card is scanned when not in programming mode
Serial.println("Master Card Scanned - Programming mode Enabled");
delay(1000);
RFID_SaveNextRead = true; // Enable programming mode
} else if (RFID_Master_Correct == false && RFID_SaveNextRead == true) { // If another card is scanned when in programming mode
// Save the Card
if (RFID_Slave1[0] == 0) { // Is the Slave1 Card slot empty?
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave1[checkPosition] = buffer[checkPosition+1]; // Save the scanned card as Slave1
}
Serial.println("RFID Card saved in: Slave1");
delay(1000);
} else if (RFID_Slave2[0] == 0) { // Is the Slave2 Card slot empty?
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave2[checkPosition] = buffer[checkPosition+1]; // Save the scanned card as Slave2
}
Serial.println("RFID Card saved in: Slave2");
delay(1000);
} else if (RFID_Slave3[0] == 0) { // Is the Slave3 Card slot empty?
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave3[checkPosition] = buffer[checkPosition+1]; // Save the scanned card as Slave3
}
Serial.println("RFID Card saved in: Slave3");
delay(1000);
} else {
Serial.println("No free Card slots");
RFID_Enable(true); //turns on the RFID reader
delay(1000);
RFID_Enable(false); //turns off the RFID reader
delay(1000);
}
EEPROM_Save_Slaves();
RFID_SaveNextRead = false;
} else if (RFID_Master_Correct == true && RFID_SaveNextRead == true) { // If the Master Card is scanned when in programming mode
Serial.println("Master Card Scanned again - Removing all saved Cards");
delay(1000);
// Remove all Slave Cards
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave1[checkPosition] = 0;
}
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave2[checkPosition] = 0;
}
for (checkPosition = 0; checkPosition < 10; checkPosition++) { //Read bit fra 0-9
RFID_Slave3[checkPosition] = 0;
}
EEPROM_Save_Slaves();
RFID_SaveNextRead = false;
}
RFID_Enable(true); //turns on the RFID reader
EmptySerialBuffer(); //erase the buffer
Serial.println("");
}
i++; //used in the beginning to write to each bit in the buffer
}
}
//Go to
}
void EmptySerialBuffer() { //replaces all bits in the buffer with zeros
while (Serial.available()) { Serial.read(); }
for (i2 = 0; i2 <= i; i2++) {
buffer[i2] = 0;
}
i = 0;
}
void PreLock() {
servoLock.attach(servoPin); // attaches the servo on pin 3 to the servo object
servoLock.write(LockedPos); // tell servo to go to position in variable 'LockedPos'
delay(250); // waits 1s for the servo to reach the position
servoLock.detach(); //detaches the servo, so it's not using power
DoorLocked = true; //the door is locked
}
void Unlock(byte speedDelay) {
int pos;
servoLock.attach(servoPin); // attaches the servo on pin 3 to the servo object
for(pos = LockedPos; pos < UnlockedPos; pos += 1) // goes from 10 degrees to 110 degrees
{ // in steps of 1 degree
servoLock.write(pos); // tell servo to go to position in variable 'pos'
delay(speedDelay); // waits 5ms for the servo to reach the position
}
servoLock.detach(); //detaches the servo, so it's not using power
DoorLocked = false; //the door is unlocked
}
void Lock(byte speedDelay) {
int pos;
servoLock.attach(servoPin); // attaches the servo on pin 3 to the servo object
for(pos = UnlockedPos; pos > LockedPos; pos -= 1) // goes from 110 degrees to 10 degrees
{ // in steps of 1 degree
servoLock.write(pos); // tell servo to go to position in variable 'pos'
delay(speedDelay); // waits 5ms for the servo to reach the position
}
servoLock.detach(); //detaches the servo, so it's not using power
DoorLocked = true; //the door is locked
}
void RFID_Enable(boolean enabled) {
if (enabled == true) {
digitalWrite(RFID_Enabled_Pin, LOW); //enables the RDIF reader and turns on the diode on the arduino
digitalWrite(13, HIGH);
} else { //disables the RDIF reader and turns off the diode on the arduino
digitalWrite(RFID_Enabled_Pin, HIGH);
digitalWrite(13, LOW);
}
}
void EEPROM_Read_Slaves() {
byte EPROMaddr;
for (EPROMaddr = 0; EPROMaddr < 10; EPROMaddr++) { //Read bit fra 0-9
RFID_Slave1[EPROMaddr] = EEPROM.read(EPROMaddr);
}
for (EPROMaddr = 10; EPROMaddr < 20; EPROMaddr++) { //Read bit fra 0-9
RFID_Slave2[EPROMaddr-10] = EEPROM.read(EPROMaddr);
}
for (EPROMaddr = 20; EPROMaddr < 30; EPROMaddr++) { //Read bit fra 0-9
RFID_Slave3[EPROMaddr-20] = EEPROM.read(EPROMaddr);
}
}
void EEPROM_Save_Slaves() {
byte EPROMaddr;
for (EPROMaddr = 0; EPROMaddr < 10; EPROMaddr++) { //Read bit fra 0-9
EEPROM.write(EPROMaddr, RFID_Slave1[EPROMaddr]);
}
for (EPROMaddr = 10; EPROMaddr < 20; EPROMaddr++) { //Read bit fra 0-9
EEPROM.write(EPROMaddr, RFID_Slave2[EPROMaddr-10]);
}
for (EPROMaddr = 20; EPROMaddr < 30; EPROMaddr++) { //Read bit fra 0-9
EEPROM.write(EPROMaddr, RFID_Slave3[EPROMaddr-20]);
}
}


Hello!! I think it is very interesting for me, i have an arduino uno with MF522-AN 13.56 MHz.
I read the code you did it before. Do you think it is possible for me to use it?
My arduino can provide 3.3V??
The codes would be change a lots??
@Sam
You have to change the part which reads from the RFID module, as the one we used works via UART, but you can use the rest of the code without modification.
@Sam
Please have a look at the following page with an Arduino project for the MF522 module: How to get started with the Mifare MF522-AN and Arduino
Follow the guide on how to connect the module to the Arduino and then uncomment all the places where it says “Serial. ….so on” in the code to get some debugging information.
my rfid card reader has no “enable pin”.
i use RDM630 Proximity Reader.
how can i connect the rfid to arduino?
@anne
If you don’t have any Enable pin you should just remove anything “RFID_Enabled_Pin” related. As long as your module is then enabled all the time it doesn’t matter.
A quick search on Google lead me to this page which gives you a good explanation on how to get started with the RDM630 RFID module connected to an Arduino: http://blog.iteadstudio.com/play-rdm630-with-arduino/
A bit more advanced tutorial with Interrupts on read can be found here: http://maniacbug.wordpress.com/2011/10/09/125khz-rfid-module-rdm630/
Regards Thomas
thanks:)
the servo @ rfid works but the problem now is the display..
it doesn’t show anything in the serial monitor..
sorry,;):)i’m not good in programming…:D:D
@anne
Which kind of tutorial have you been following?
You should be cautious about the serial monitor when you have connected the RFID reader to the Serial port (RX/TX).
As the pins for the Serial monitor are the same pins as for the RFID board, connecting to the Arduino with your serial monitor in the same time will make the Arduino think that your inputs are actually data from the RFID module.
Please have this in mind