Arduino and RFID using UID to identify a Key Master card.

RFID

The Sketch and Library can be downloaded from my GitHub page here.

There is a very good Library for RFID boards using the MFRC522 written by Miguel Balboa. The sketches in the examples show how to connect the RFID board to an Arduino, as the device is powered by 3.3 volts I assumed that the data lines should be 3.3 volts and used bi directional level converters like these here. These converters have four channels and as there are five data lines I used two of them.

The examples show how to read and write data to an RFID tag or card, but I just wanted to use the card like a key, the sketch could hold a list of UIDs that would identify a valid key and could open a lock, or open a specific web site in a browser. The sketch shown here was just a simple test of the idea. A Red and green LED are connected to two of the Arduino Digital pins, the Red led is normally ON and the Green LED is normally OFF. If a valid key is brought near the card reader the Green LED will switch ON for 1 second.

Rather than keep hard coding the UID into the Sketch I included a learning mode. If Digital Pin 4 is connected to Ground, when the sketch is first run the first card read will become the Master Key and will remain so until the Sketch is re-run.

This sketch was really written to test out the idea of using RFID tags as keys and can easily be modified and expanded. All the connections are shown in the sketch. The UID of the card is printed out in the Serial Monitor as both HEX and as a decimal number, the decimal number is a String and is used to compare the card read with the Master value stored in the sketch.
/*
* Identify an RFID tag or card from its UID

* This sketch when first run will store the UID of the first card scanned
* if Digital Pin 4 is connected to Gnd.
* When this card is scanned again the Green LED will light
* All other cards will turn both LEDs off.
* After 1 second the Green LED will be off and the Red LED will be on.
*
* Stored UIDs are lost if the power is removed or the Sketch is reset
*
* Connect a flying lead to pin 4, if the learn Mode is required then
* connect this to Gnd.
*
* UIDs are printed out in HEX and Decimal in the Serial Monitor
*
* Pin layout should be as follows:
* (Bi Directional Logic Level Convertors are required as the device is 3.3volts
* and two are required for the 5 signal lines)
* Signal Pin Pin
* Arduino Uno                 MFRC522 board
* -----------------------------------------
* Reset 5                     RST
* SPI SS 10                   SDA
* SPI MOSI 11                 MOSI
* SPI MISO 12                 MISO
* SPI SCK 13                  SCK
*
* Connect a Red LED in series with a 330R resistor between pin 2 and Gnd
* Connect a Green LED in series with a 330R resistor between pin 3 and Gnd
* Connect Digital Pin 4 to Gnd to activate the Learn Mode
*/
#include
#include

#define SS_PIN 10
#define RST_PIN 5
#define redLed 2
#define greenLed 3
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
String UIDstring;
boolean learnMode = false; // dont ask to remember this card number
String knownCard = "";

void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
pinMode(4, INPUT_PULLUP);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
if(digitalRead(4) == 0){
learnMode = true;
Serial.println("Learn Mode ON");
}
else{
learnMode = false;
Serial.println("Learn Mode OFF");
}
}

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

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

// Dump debug info about the card. PICC_HaltA() is automatically called.
//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

// Dump UID
Serial.print("Card UID:");
UIDstring = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UIDstring = UIDstring + String(mfrc522.uid.uidByte[i]);
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
if(learnMode){
knownCard = UIDstring;
learnMode = false; // turn it off
}
Serial.println(UIDstring);
// light correct LED
if (UIDstring == knownCard){
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
}
else{
digitalWrite(redLed, LOW); // turn both LEDS off, its the wrong card
digitalWrite(greenLed, LOW);
}
delay(1000);
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
}

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