RFID Door lock help

This is a special forum devoted to educators using Adafruit and Arduino products for teaching.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jpap
 
Posts: 3
Joined: Sun Sep 08, 2013 1:24 pm

RFID Door lock help

Post by jpap »

Students in my advanced electronics class are building an RFID door lock using the PN532 RFID shield and arduino uno. They have used some code gathered from the internet and it is reading cards correctly and opening the magnetic lock well. The only problem that exists, is it accepts any mifare card that is used. We are having trouble adding the encoded card number(s) to be selective on which cards are able to open the lock. Class and myself are new to arduino and coding, so any guidance would be appreciated! Code used is below

Code: Select all

#include <Wire.h>
#include <Adafruit_NFCShield_I2C.h>



#define MISO (5)
#define DOOR (6)
#define LED (4)
#define IRQ   (2)
#define RESET (3)  // Not connected by default on the NFC Shield

Adafruit_NFCShield_I2C nfc(IRQ, RESET);

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");

  // pin setup
  pinMode(LED, OUTPUT);
  pinMode(DOOR, OUTPUT);
  digitalWrite(LED, LOW);
  digitalWrite(DOOR, LOW);

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  
  // configure board to read RFID tags
  nfc.SAMConfig();
  
  Serial.println("Waiting for Technology Education Card ...");
}


void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
    
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  
  if(success) {
    
    // out uid
    Serial.print("Detected UID:");
    for(int i=0; i < uidLength; i++) {
      Serial.print(" ");
      Serial.print(uid[i]);
    }
    Serial.println("");

    // unlock the door
    digitalWrite(DOOR, HIGH);
    
    // blink LED
    for(int i=0; i<5; i = i+1) {
      digitalWrite(LED, LOW);
      delay(100);    
      digitalWrite(LED, HIGH);
      delay(100);
    }

    // wait another five seconds before re-entering the loop
    delay(5000);
    
    // turn led off
    digitalWrite(LED, LOW);
    
    // lock door
    digitalWrite(DOOR, LOW);    

  }
    
}
Last edited by adafruit_support_bill on Fri Oct 03, 2014 8:43 am, edited 1 time in total.
Reason: please use the </> button when submitting code. press </>, then paste your code between the [code] [/code] tags.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: RFID Door lock help

Post by adafruit_support_bill »

Your code is detecting and printing the UIDs. But the unlock code is unconditional.

Code: Select all

    Serial.print("Detected UID:");
    for(int i=0; i < uidLength; i++) {
      Serial.print(" ");
      Serial.print(uid[i]);
    }
    Serial.println("");

    // unlock the door
    digitalWrite(DOOR, HIGH);
You need to add some conditional code with a list of the authorized UIDs. The most straightforward implementation would be to create an array of UIDs and iterate through it until you find a match. How many UIDs do you need to recognize? Memory could become an issue if there are a lot.

User avatar
jpap
 
Posts: 3
Joined: Sun Sep 08, 2013 1:24 pm

Re: RFID Door lock help

Post by jpap »

When you have 30 kids waiting for an answer to their project it is awesome to have such a quick reply! Thanks a lot!

User avatar
jpap
 
Posts: 3
Joined: Sun Sep 08, 2013 1:24 pm

Re: RFID Door lock help

Post by jpap »

In addition to this question, we have the reader working perfectly, but is it possible to have two RFID readers working at the same time in different locations and feeding to the same arduino?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: RFID Door lock help

Post by adafruit_support_bill »

Since the shield has a fixed i2c address, you can't have more than one on the i2c bus. But you could have a second shield & Arduino relay the data back to the first over a serial interface.

Locked
Please be positive and constructive with your questions and comments.

Return to “For Educators”