RFID Card Reader

RFID Card Reader

Overview:

Many doors and devices today are locked with Radio Frequency Identification systems. These systems receive and/or emit an electromagnetic signal to communicate with each other and make life easier. Early applications for RFID chips included keeping track of cattle and large shipping containers. In the future, this technology may be used for the simplest things such as checking out at a grocery store!

Read this article for more information:

Parts:

1 – Arduino Uno

1 – RFID – RC522

1 or more passive RFID cards or “fobs”

1 – green LED

1 – red LED

assorted wires

Wiring:

Wiring from Arduino to RFID – RC522

Arduino

RFID – RC522

9

RST

10

SDA

11

MOSI

12

MISO

13

SCK

GND

GND

3.3V

3.3V

Code:

/*
  ----------------------------------------------------------------------------
  This sketch uses the MFRC522 library ; see https://github.com/miguelbalboa/rfid * for further details and other examples.

  NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
//add output pins for two LEDs
const int green = 3;
const int red = 4;
// Create MFRC522 instance.
MFRC522 mfrc522(SS_PIN, RST_PIN);
String read_rfid;
//list the string that is your UID code and name it after yourself
String andy = "141d5fff";
String zach = "10fb83a7";
//String ok_rfid_2="ffffffff";
//add as many as you need.
/*
  Initialize.
*/
void setup() {
  Serial.begin(9600); // Initialize serial communications with the PC
  while (!Serial); // Do nothing if no serial port is opened
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522 card
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
}
/*
  Helper routine to give the ID of the card as an "array" or "string"  * of hexidecimal values to the serial monitor.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}
void loop() {
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;
  //call the routine that gets the card ID and puts it in a hexidecimal string
  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  // prints the ID of the card being read
  Serial.println(read_rfid);
  if (read_rfid == andy) {
    Serial.println("Hello, Andy. You may enter");
    digitalWrite(green, HIGH);
    delay(1000);
    digitalWrite(green, LOW);
    delay(1000);
    return;
    //attach a servo or a door lock of some kind to be impressive!
  }
  else if (read_rfid == zach) {
    Serial.println("Hello, Zach. You may enter");
    digitalWrite(green, HIGH);
    delay(1000);
    digitalWrite(green, LOW);
    delay(1000);
    return;
    //attach a servo or a door lock of some kind to be impressive!
  }
  //Add below as many users as you want...
  //if (read_rfid==ok_rfid_2) {
  //also ok, open the door
  // open_lock();
  else {
    Serial.println("I'm sorry. I don't recognize that ID. You SHALL NOT PASS!");  digitalWrite(red, HIGH);
    delay(1000);
    digitalWrite(red, LOW);
    delay(1000);
    return;
  }
}

Last updated

Was this helpful?