Reading RFID Card

Obtain RFID card UID

A simple program to read the card ID using the RC522 card reader.

This can provide a starting point for access control type projects.

/* Read the UID code from a RFID RC522 card.
 * Requires the MFRC522 library
 */
/* Pin layout should be as follows:
 * Signal     Pin              Pin               Pin
 *            Arduino Uno      Mega      MFRC522 board
 * ------------------------------------------------------------
 * Reset        9                5                 RST
 * SPI SS     10               53                SDA
 * SPI MOSI   11               51                MOSI
 * SPI MISO   12               50                MISO
 * SPI SCK    13               52                SCK
 */
 
 
#include <SPI.h>
#include <MFRC522.h>
 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance.
 
byte readID[4];  //Holds the ID read from the card
boolean cardSuccess; //Read success flag.
 
void setup() {
  Serial.begin(9600); // Initialise serial communications with the PC
  SPI.begin();      // Initialise SPI bus
  mfrc522.PCD_Init(); // Initialise the card reader
  Serial.println("Scan RFID card to see UID");
}
 
void loop() {
  cardSuccess = getID(); // Look for new cards
  if(cardSuccess){
    Serial.println(F("Card successfully read"));
  }
 
}
 
/* Get the card ID */
 int getID(){
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //look for a card
    return 0; //returns a 0 if nothing is found.
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) { //try to read a discovered card
    return 0; //returns a 0 if reading error.
  }
  Serial.println(F("Card ID:")); //if all good, get the card ID - assumes 4 bytes of UID code
  for(int i=0; i<4; i++){
    readID[i] = mfrc522.uid.uidByte[i];
    Serial.print(readID[i], HEX);
  }
  Serial.println("");
  mfrc522.PICC_HaltA(); //stop reading once ID received
  return 1; //returns a 1 to indicate ID read.
 }
 

 

 

Your IP Address is: 54.225.21.228
Copyright © 2024 Bitsbox. Powered by Zen Cart