RFID Lock Box

This project was the second major physical computing project I have attempted and I really tried to push my limits of my knowledge and understanding. I started with a different idea to what the end result was.

First there wasclock an idea to recreate the Weasley’s clock from Harry Potter using RFID tags and the internet with multiple Arduinos. After spending time looking in to making this project come to life, I decided to stop it when looking into sending and receiving signals over the net. This seemed beyond the capabilities of the time limit I had and my skills at that time.

I therefore needed a new more sensible and achievable project. I decided upon a triple lock security box that opened the lid automatically and lights up.

The security box was to open first by satisfying a secret knock with a pizo element recognizing the knock sequence. The second task to perform was inputting the correct pin number into a pin pad and the final level of security was having the correct RFID tag. Once all three security tasks were completed (in order) the dead bolts would unlatch and the lid of the box lid would open with the inside being illuminated with colour changing LEDs.

When it came to making the concept in to a real working project I realised that I would need an Arduino Mega to complete all the tasks I originally thought of. However I thought I would try to sort out as many components as possible on an uno before buying a mega.

I started with the hardest component which was the RFID. I tried to get a reader to look for one particular Tag which when found would then run a function. From research I found that the card has 1 Kb of memory on it which means it has 1024 Hex binary digits.

There are 16 Hexadecimal digits. They are the same as the decimal digits up to 9, but then there are the letters A, B, C, D, E and F in place of the decimal numbers 10 to 15:

Hexadecimal: 0 1 2 3 4 5 6 7 8 9 A B C D E F
Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

This means there is more differentiation in the data that can be stored as normal binary only has 2 digits: 1 and 0.

The main thing that needed finding in the 1K of data on the tag was the Unique Identity number (UID). The UID is the first 6 Bytes in Block 0 of Sector 0 of the 1K data stack in the card. The UID is clearly seen in the ‘info dump’ example when downloading the library on RFID from the arduino website.


Library Source https://github.com/miguelbalboa/rfid

Helpful arduino page http://playground.arduino.cc/Learning/MFRC522


memory diagram


Once the RFID was sorted I then moved on to looking at how the Pin Pad worked. There was one way which I managed to get it working properly which required 8 digital output pins to read 16 buttons. At this point I realized that this was not going to fit on one uno at all, as the RFID Reader took 5 pins and more were still needed. To get around this I looked into operating the Pin Pad off of 1 Analogue input pin that I managed to get working. It did not work 100% properly which meant I had to leave the pin pad off the final box as I needed to manage my time best to meet the deadline and focus on the tasks which I thought were most important. Next time I would plan in advance more to make sure I understood the components and knew if they were achievable given my time frame and the resources available to me.

The Analogue pin pad worked by reading different resistances as each button had a unique amount of resistance as a result of the wiring. This is what distinguished the individual keys. Below shows the wiring diagram for the single lead pin pad and then the video is of the 8 lead working with a password that lights up an LED when the pin is correct.


4x3_KeypadLayout


The final fritzig diagram of the keypad module which unfortunately did not fit in to the final RFID box.


key pad example


Below is the code and then video of the pin pad working along with the key library links needed to make the pin pad work.

For the Librerys Keypad and Password http://playground.arduino.cc/code/keypad


/*
|| sorce code
|| Updates Nathan Sobieck: Nathan@Sobisource.com
||
*/
//* is to validate password
//# is to reset password attempt
/////////////////////////////////////////////////////////////////

#include //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include //http://www.arduino.cc/playground/uploads/Code/Keypad.zip

Password password = Password( “3056” );

const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{‘1′,’2′,’3′,’FA’},
{‘4′,’5′,’6′,’FB’},
{‘7′,’8′,’9′,’FC’},
{‘*’,’0′,’#’,’FD’}
};
byte rowPins[ROWS] = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6,7,8, 9};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int ledpin=13;

void setup(){

Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
pinMode (ledpin, OUTPUT);
pinMode (12, OUTPUT);
}

void loop(){
keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print(“Pressed: “);
Serial.println(eKey);
switch (eKey){
case ‘*’: checkPassword(); break;
case ‘#’: password.reset(); break;
default: password.append(eKey);
}
}
}

void checkPassword(){
if (password.evaluate()){
Serial.println(“Success”); //Add code to run if it works
digitalWrite(ledpin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(ledpin, LOW);
}else{
Serial.println(“Wrong”); //add code to run if it did not work
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW);

}
}



With time being tight I compromised and left out the Pizo element too as I needed to make the box do what I wanted it to which was to open itself with use of the capstan that I used for the auto straw project. Other things that needed to work were turning the LED strip lights on and the dead bolts, both were controlled by a servo each. I also added some user interface LEDs to let the user know whether the box was unlocked or not with a red or green LED (red meant locked and green meant unlocked).

The capstan was controlled with a H bridge. Unfortunately for the final prototype it did not produce enough torque to actuate the lip popping up as I had imagined and this was probably due to the lid being too heavy. There was also lots of friction in the contact surfaces.

As for the dead bolts they worked as I had hoped for and the same with the physical light switch being controlled digitally through a servo.

The last thing to do was to put the code together and the box and admire the lights.

Below is the Fritzig diagram of the wiring followed by the components list.


 

box rfid_bb

The components use in the RFID lockbox are as follows…

  • Arduno Uno
  • Half bread board
  • 2 9g servo motors
  • 2 LEDs red and green
  • RFID reader
  • RFID tags 2 min
  • DC motor
  • H bridge chip
  • Jumper cables
  • LED strip with driver and remote

 

The code for the final RFID Lockbox is below followed by the video of the final model.


#include
#include
#include
#define SS_PIN 10 //Arduino Uno
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
int ledpin=8;
Servo myservo; // create servo object to control a servo
Servo myservo2; // twelve servo objects can be created on most boards
int pos = 5;
int ledpin2=7;
int motorpin1 = 2; //define digital output pin no.
int motorpin2 = 4;

void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(“Print block 0 of a MIFARE PICC “);
pinMode (ledpin, OUTPUT);
myservo.attach(6); // attaches the servo on pin 9 to the servo object
myservo2.attach(5);
pinMode (ledpin2, OUTPUT);
pinMode(motorpin1,OUTPUT); //set pin 3 as output
pinMode(motorpin2,OUTPUT);
}
void loop() {

// Prepare key – all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
MFRC522::MIFARE_Key key;
for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;

// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) return;

Serial.print(“Card UID:”); //Dump UID
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? ” 0″ : ” “);
Serial.print(mfrc522.uid.uidByte[i], HEX);

if( (mfrc522.uid.uidByte[i])==0xC3)
{
Serial.print(“MATCH”); //this is were the function Starts.

digitalWrite (ledpin, HIGH);
delay (300);

//servos open (lights on and lock unlock)
for(pos = 5; pos <= 175; pos += 10) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable ‘pos’ myservo2.write(pos); // tell servo to go to position in variable ‘pos’ delay(15); // waits 15ms for the servo to reach the position } delay(100); //motor starts to open and close the box digitalWrite(motorpin1,LOW); digitalWrite(motorpin2,HIGH); delay (10000); digitalWrite(motorpin1,LOW); digitalWrite(motorpin2,LOW); delay (5000); digitalWrite(motorpin1,HIGH); digitalWrite(motorpin2,LOW); delay (10000); digitalWrite(motorpin2,HIGH); delay (1000); // servos close for(pos = 175; pos>=5; pos-=10) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}

//wait then green light gows off
delay (500);
digitalWrite (ledpin, LOW);

}
else

//red light on if rfid dosent match
{
digitalWrite (ledpin2 ,HIGH);
delay (500);
digitalWrite (ledpin2, LOW);
}

//this is where the function stops.
}
Serial.print(” PICC type: “); // Dump PICC type
byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));

byte buffer[18];
byte block = 0;
byte status;
//Serial.println(“Authenticating using key A…”);
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(“PCD_Authenticate() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}

// Read block
byte byteCount = sizeof(buffer);
status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
Serial.print(“MIFARE_Read() failed: “);
Serial.println(mfrc522.GetStatusCodeName(status));
}
else // Dump data
for (byte index = 0; index < 16; index++) {
Serial.print(buffer[index] < 0x10 ? ” 0″ : ” “);
Serial.print(buffer[index], HEX);
if ((index % 4) == 3) Serial.print(” “);
}
Serial.println(” “);
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
}


Overall I was very pleased with the outcome of this project and I really enjoyed the process to get the model to work, including bug fixing. If I were to do this project again there are a few areas where I now know how to improve on. Firstly having an arduino mega would mean I would have plenty of output pins so I was not restricted in this aspect. Secondly I would use a bigger motor with a better gearbox so it had enough power to control the capstan. Finally I would use a ready made box instead of making one myself as this would save on time and allow me to focus on the coding and electronics.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s