Project – RFID Security Box

Project – RFID Security Box

Here we are with a new project!! This is a security box that will keep safe your things!

How it works

A servo will lock the door and with a RFID tag you can unlock it. Then you’ll have 3 seconds to open it. Moreover If you won’t open the door, the servo will lock the door. An IR sensor is used to verify if the door is closed. If it is closed, the servo will lock the door.

Materials

  • Arduino Nano
  • 1 RFID module and some tags
  • 1 IR module
  • a plastic container
  • 1 servo
  • 1 LED
  • 9V battery

Where to buy materials

Please note that those are affiliate links. This means that I will have a percentage if you buy from those links. You will pay the same price anyway!

I will choose the better options for you. There are more than 1 option for some products because I want to let you choose the best for you!

Schematics

RFID schematic

IR module schematic
Servo schematic

LED schematic
Battery schematic

Sketch

#include <SPI.h>        //include this three libraries
#include <MFRC522.h>
#include <Servo.h>

//RFID pin
#define N 1 //max number of tags that can be used
#define RST_PIN   9
#define SS_PIN    10

//Led
const int ledPin = 2;

//Servo
Servo myServo;
const int servoPin = 7;

//ProxSensor
const int proxPin = 6;

MFRC522 mfrc522(SS_PIN, RST_PIN);   //MRRC522 object

byte readCard[4];
char* myTags[N] = {};               //here we save our tag

String tagID = "";

int state;                          //this variable is used for the switch(case), I called it 'state' to remember a finite-state machine.
bool done = false;                  //this boolean variable is used in the while in the setup. To be sure you select a tag first of all.

void setup() 
{  
  SPI.begin();                      //Initialize the SPI bus 

  myServo.attach(servoPin);
  myServo.write(90);                 //initial angle
  
  pinMode(ledPin, OUTPUT);

  mfrc522.PCD_Init();               //Initialize MFRC522

// Now Arduino waits until a tag is scanned
  
  while (!done)
  {
    blinkingLed();          //the led will blink, this means that you have
                            //to choose a tag
    if (getID())                              
    {                                         
      myTags[0] = strdup(tagID.c_str()); //Sets the tag into position 0 in the array. For our tutorial this will be the only position available.
                                         //This means that you can save only one tag
      digitalWrite(ledPin, HIGH);        //you will understand that the tag has been saved. The led will stop
      delay(2000);                       //blinking and stay HIGH for two seconds.
      state=0;                           //go to state 0
      done = true;                       //exit from the while
    }
  }
}

void loop()
{
  switch(state)
  {      
    case 0:
      myServo.write(90);                  //set the servo angle to 0
      digitalWrite(ledPin, LOW);         //Put the led LOW, Arduino is waiting for a tag    
      if(getID())   
      {
        if(!correctTag())
        {
          for(int n=0; n<100; n++)
          {
            blinkingLed();              //wrong tag, led blinking
          }
          state=0;
        }
        else if(correctTag())
        {
          digitalWrite(ledPin, HIGH);   //correct tag, led HIGH
          state=1;   //if the target is correct go to state 1
        }   
      }
      break;

    case 1: 
        myServo.write(10);
        delay(3000);         //you have 3 seconds to open the door
        if(!digitalRead(proxPin))
        {
          state = 0;        //go to state 0
        }
  }
}

bool correctTag()       //this function checks if the tag is correct(the first we saved) or not
{
  if(tagID==myTags[0])
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

bool getID() 
{
  // Getting ready to read PICCs
  if ( ! mfrc522.PICC_IsNewCardPresent()) //If a new PICC is placed to RFID reader continue
  {
    return 0;
  }
  
  if ( ! mfrc522.PICC_ReadCardSerial()) //Since a PICC is placed get Serial and continue
  {
    return 0;
  }
  
  tagID = "";
  for ( uint8_t i = 0; i < 4; i++) 
  {  //The MIFARE PICCs that we use have 4 byte UID
    readCard[i] = mfrc522.uid.uidByte[i];
    tagID.concat(String(mfrc522.uid.uidByte[i], HEX)); //Adds the 4 bytes in a single String variable
  }
  tagID.toUpperCase();
  mfrc522.PICC_HaltA(); // Stop reading
  return 1;   //return 1 only if we put a tag on the reader
}

void blinkingLed()
{
    digitalWrite(ledPin, HIGH);          
    delay(300);                         
    digitalWrite(ledPin, LOW);                      
    delay(300); 
}

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: