Abdul Aziz M. Abullais

Maker

Web Designer

Graphic Designer

Drone Specialist

Photographer

Blogger

Freelancer

0

No products in the cart.

Abdul Aziz M. Abullais

Maker

Web Designer

Graphic Designer

Drone Specialist

Photographer

Blogger

Freelancer

Blog Post

Security System – Unlock with your NFC ring

Security System – Unlock with your NFC ring

In this project, students will create a security system that keeps the MakerSpace room locked at all times, and in order to unlock the door, we need to use the NFC ring, Bankcard, Metro card or NFC sticker which can be stuck on anything such as your phone.

Steps:

  1. Wiring
  2. Code
  3. Upload libraries
    • NFC library – MFRC522.h
    • Tone Libraries – SoundPitches.h
/* 

NFC \ RFID Arduino Sketch(RC522)
Created by Millennia World School - MakerSpace Students & Teachers a 16 chars and 2 line display


SDA     SCK       MOSI      MISO       RQ     GND       RST     3.3
PURPLE  ORANGE    WHITE     BLACK             GRAY      BROWN   BROWN
10      13        11        12                GND       8       3.3

/* Start of Code */

#include <MFRC522.h>                                          // Include of the RC522 Library
#include "SoundPitches.h"                                     // Notes of the melodies
#include <SPI.h>                                              // Used for communication via SPI with the Module
#include <Servo.h>                                            // Lirary for using Servo
#include <Wire.h>                                             // Lirary for using LCD
#include <LiquidCrystal_I2C.h>                                // Lirary for using LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);                           // Set the LCD address to 0x27 for 


#define SDAPIN 10                                             // RFID Module SDA Pin is connected to the UNO 10 Pin
#define RESETPIN 8                                            // RFID Module RST Pin is connected to the UNO 8 Pin

Servo MyServo;                                                // create servo object to control a servo
int Position;                                                 // variable to store the servo Positionition

#define BuzzerPin 5                                           // Buzzer Pin

int ButtonPin = 4;                                            // the number of the pushbutton pin
int LedPin =  2;                                              // the number of the LED pin
int ButtonState = 1;                                          // State of push utton at the beginning 

int DoorSensor = 9;
int DoorSensorState = 0;                                      // 0 close - 1 open wwitch// variable for reading the pushbutton status

byte FoundTag;                                                // Variable used to check if Tag was found
byte ReadTag;                                                 // Variable used to store anti-collision value to read Tag information
byte TagData[MAX_LEN];                                        // Variable used to store Full Tag Data, in our case its a maximum of 4 Blocks
byte TagSerialNumber[5];                                      // Variable used to store only Tag Serial Number
byte Key1SerialNumber[5] = {0x88, 0x04, 0x43, 0xD1};          // The Key 1 Tag Serial number we are looking for  (RING)
byte Key2SerialNumber[5] = {0xD3, 0x79, 0x1F, 0x24};          // The Key 2 Tag Serial number we are looking for  (NFC Card)
byte Key3SerialNumber[5] = {0x88, 0xA5, 0xCB, 0x24};          // The Key 2 Tag Serial number we are looking for  (Bank Card)
byte Key4SerialNumber[5] = {0xEB, 0xDA, 0xC9, 0x83};          // The Key 2 Tag Serial number we are looking for  (Keychain)

MFRC522 nfc(SDAPIN, RESETPIN);                                // Init of the library using the UNO pins declared above

void setup() {                                                // Void Setup Begin
  SPI.begin();                                                // begin SPI communication
  Serial.begin(115200);                                       // Speed of serial communication clock
  nfc.begin();  
  lcd.begin();                                                // initialize the LCD
  lcd.backlight();                                            // Turn on the blacklight and print a message.
  lcd.clear();
  pinMode(LedPin, OUTPUT);                                    // initialize the LED pin as an output:
  pinMode(ButtonPin, INPUT);                                  // initialize the pushbutton pin as an input:
  pinMode(5, OUTPUT); 
  pinMode(DoorSensor, INPUT_PULLUP);
  MyServo.attach(3);                                          // attaches the servo on pin 3 on the arduino, and delcare it as MyServo
  MyServo.write(80);                                          // tell servo to go to lock Positionition in variable 'Position' 
  Serial.println("Looking for RFID Reader");                  // Display message on computer screen
  byte version = nfc.getFirmwareVersion();                    // Variable to store Firmware version of the NFC Module
    if (! version) {                                          // If can't find an RFID Module
      Serial.print("Didn't find RC522 board.");               // Display message on computer screen
      lcd.print("Didn't find RC522 board");
      while(1);                                               // Wait until a RFID Module is found
    }
  Serial.print("Found chip RC522 ");                          // If found, print the information about the RFID Module
  Serial.print("Firmware version: 0x");                       // Display Firmware Version on computer screen
  Serial.println(version, HEX);Serial.println();
}

void loop() {                                                 // Void Loop Begin
  buttonWait(4);                                              // Function to wait for Button Press
  digitalWrite(LedPin, LOW);                                  // Turn LED OFF  
//--------------------------------Keys Declaration------------------------------------------
  String Key1="False";                                        // Key1 Declare as False
  String Key2="False";                                        // Key2 Declare as False
  String Key3="False";                                        // Key3 Declare as False
  String Key4="False";                                        // Key4 Declare as False 
//------------------------------------------------------------------------------------------   
  FoundTag = nfc.requestTag(MF1_REQIDL, TagData);             // Check to see if a Tag was detected, If yes, then the variable FoundTag will contain "MI_OK" 
    if (FoundTag == MI_OK) {
    delay(200);
    ReadTag = nfc.antiCollision(TagData);                     // Get anti-collision value to properly read information from the Tag
    memcpy(TagSerialNumber, TagData, 4);                      // Write the Tag information in the TagSerialNumber variable
    Serial.println("Tag detected.");
    Serial.print("Serial Number: ");    
    for (int i = 0; i < 4; i++) {                             // Using Loop to print Tag serial number to serial monitor  
      Serial.print(TagSerialNumber[i], HEX);
      Serial.print(", ");
    }
    Serial.println("");
   Serial.println();
//----------------------------------------------Key1 Checking------------------------------------------
  for(int i=0; i <= 3; i++){                                  // Using Loop to Check Tag serial number
    if (Key1SerialNumber[i] != TagSerialNumber[i]) {
      break;                                                  // if not equal, then break out of the "for" loop
    }
    if (i == 3) {                                             // if we made it to 4 loops then the Tag Serial numbers are matching
      Key1="TRUE";
      sing();
      digitalWrite(LedPin, HIGH);
      Serial.println("Ring Opens Door");
    Serial.println();
    openlock();
    lcd.println("Ring Opens Door");
    delay(3000);lcd.clear();  
    closelock();
    } 
  }
//----------------------------------------------Key2 Checking------------------------------------------
  for(int i=0; i <= 3; i++){                                  // Using Loop to Check Tag serial number
    if (Key2SerialNumber[i] != TagSerialNumber[i]) {
      break;                                                  // if not equal, then break out of the "for" loop
    }
    if (i == 3) {                                             // if we made it to 4 loops then the Tag Serial numbers are matching
      Key2="TRUE";
      sing();
      digitalWrite(LedPin, HIGH);
      Serial.println("NFC Opens Door");
    Serial.println();
    openlock();
    lcd.println("NFC Opens Door");
    delay(3000);lcd.clear();
    closelock();
    } 
   }
//----------------------------------------------Key3 Checking------------------------------------------
  for(int i=0; i <= 3; i++){                                  // Using Loop to Check Tag serial number
    if (Key3SerialNumber[i] != TagSerialNumber[i]) {
      break;                                                  // if not equal, then break out of the "for" loop
    }
    if (i == 3) {                                             // if we made it to 4 loops then the Tag Serial numbers are matching
      Key3="TRUE";
      sing();
      digitalWrite(LedPin, HIGH);
      Serial.println("Bank Card Opens Door");
    Serial.println();
    openlock();
    lcd.println("Bank Card Opens Door");
    delay(3000);lcd.clear();
    closelock();
    } 
   }
//----------------------------------------------Key4 Checking------------------------------------------
  for(int i=0; i <= 3; i++){                                  // Using Loop to Check Tag serial number
    if (Key4SerialNumber[i] != TagSerialNumber[i]) {
      break;                                                  // if not equal, then break out of the "for" loop
    }
    if (i == 3) {                                             // if we made it to 4 loops then the Tag Serial numbers are matching
      Key4="TRUE";
      sing();
      digitalWrite(LedPin, HIGH);
    } 
   }
  }
//---------------------------------------------------------------------------------------------------

/*----------------------------------------------------------Key1 Action for confirmed key------------------------------------------
  if (Key1 == "TRUE"){
    Serial.println("Ring Opens Door");
    Serial.println();
    openlock();
    lcd.println("Ring Opens Door");
    delay(3000);lcd.clear();  
    closelock();
  }
//----------------------------------------------------------Key2 Action for confirmed key------------------------------------------
  else if (Key2 == "TRUE"){
    Serial.println("NFC Opens Door");
    Serial.println();
    openlock();
    lcd.println("NFC Opens Door");
    delay(3000);lcd.clear();
    closelock();
  }
//----------------------------------------------------------Key3 Action for confirmed key------------------------------------------
 else if (Key3 == "TRUE"){
    Serial.println("Bank Card Opens Door");
    Serial.println();
    openlock();
    lcd.println("Bank Card Opens Door");
    delay(3000);lcd.clear();
    closelock();
  }
//----------------------------------------------------------Key4 Action for confirmed key------------------------------------------
  else if (Key4 == "TRUE"){
    Serial.println("Keychain Opens Door");
    Serial.println();
    openlock();
    lcd.println("Keychain Opens Door");
    delay(3000);lcd.clear();
    closelock();
  }
//----------------------------------------------------------------------------------------------------------------
  else {                                                      // If wrong Key
    Serial.println("TAG NOT ACCEPTED...... :(");
    Serial.println();
    lcd.println("TAG NOT ACCEPTED...... :(");
    delay(1000);lcd.clear();
    tone(5,2000,500);
    delay(500);
    tone(5,2000,500);
    delay(500); 
  }
 }
 */
}  
/* End of Loop Code */

void buttonWait(int ButtonPin) {                              // Button Function
  int ButtonState = 0;
  while( digitalRead(4) == 1 ) {                              // while the button is pressed
    sing();                                                   // Execute Mario Theme Melody
    digitalWrite(LedPin, HIGH);                               // turn LED on:
    Serial.println("Button Opens Door");
    Serial.println();                                         // tell servo to go to Positionition in variable 'Position'
    openlock(); 
    lcd.println("Button Opens Door");
    delay(3000);lcd.clear();
                                                 // Execute Open Lock Action  
    delay(8000);
    closelock();                                              // Execute Close Lock Action
  }
}

void openlock() {
  for (Position = 80; Position <= 125; Position += 1) {       // goes from 80 degrees to 130 degrees
    MyServo.write(Position);                                  // tell servo to go to Positionition in variable 'Position'
    delay(15);                                                // waits 15ms for the servo to reach the Positionition
  }   
}

void closelock() {
  DoorSensorState = digitalRead(DoorSensor);
  if (DoorSensorState == 0){
    for (Position = 130; Position >= 80; Position -= 1) {     // goes from 130 degrees to 80 degrees
      MyServo.write(Position);  
      delay(15);                                              // waits 15ms for the servo to reach the Positionitionc
    }
      Serial.println("Door Closed");
      Serial.println();                                       // tell servo to go to Positionition in variable 'Position'
      lcd.println("Door Closed");
      delay(1000);lcd.clear();
  }
  else {
    closelock();
  }  
}

void sing() {                                                 // iterate over the notes of the melody:
  int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < 10; thisNote++) {
      int noteDuration = 1000 / tempo[thisNote];
      buzz(BuzzerPin, melody[thisNote], noteDuration);
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      buzz(BuzzerPin, 0, noteDuration);
    }
}

void buzz(int targetPin, long frequency, long length) {
  digitalWrite(BuzzerPin, HIGH);
  long delayValue = 1000000 / frequency / 2;                  // calculate the delay value between transitions
  long numCycles = frequency * length / 1000;                 // calculate the number of cycles for proper timing
  for (long i = 0; i < numCycles; i++) {                      // for the calculated length of time...
    digitalWrite(targetPin, HIGH);                            // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue);                            // wait for the calculated delay value
    digitalWrite(targetPin, LOW);                             // write the buzzer pin low to pull 
back the diaphram
    delayMicroseconds(delayValue);                            // wait again or the calculated delay value
  }
  digitalWrite(5, LOW);
}

Write a comment