Advertisement
leo3065

[Arduino] Reading RFID tag UID using MFRC522 module

Jun 26th, 2016
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.05 KB | None | 0 0
  1. /* The power for MFRC522 modile and LCD 5110 is 3.3 volts!!
  2.    Pin for MFRC522:
  3.    -----------------------------------------------------------------------------------------
  4.                MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  5.                Reader/PCD   Uno           Mega      Nano v3    Leonardo/Micro   Pro Micro
  6.    Signal      Pin          Pin           Pin       Pin        Pin              Pin
  7.    -----------------------------------------------------------------------------------------
  8.    RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  9.    SPI SS      SDA(SS)      10            53        D10        10               10
  10.    SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  11.    SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  12.    SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  13.  
  14.    Pin for LCD 5110:
  15.    pin 22 - Serial clock out (SCLK)
  16.    pin 23 - Serial data out (DIN)
  17.    pin 24 - Data/Command select (D/C)
  18.    pin 25 - LCD chip select (CS)
  19.    pin 26 - LCD reset (RST)
  20. */
  21.  
  22. #include <SPI.h>
  23. #include <MFRC522.h>
  24. #include <Adafruit_GFX.h>
  25. #include <Adafruit_PCD8544.h>
  26.  
  27. #define SS_PIN 53
  28. #define RST_PIN 5
  29.  
  30. MFRC522 rfid(SS_PIN, RST_PIN);
  31. MFRC522::MIFARE_Key key;
  32. Adafruit_PCD8544 display = Adafruit_PCD8544(22, 23, 24, 25, 26);
  33.  
  34. void setup() {
  35.   Serial.begin(9600);
  36.   SPI.begin(); // Init SPI bus
  37.   rfid.PCD_Init(); // Init MFRC522
  38.  
  39.   for (byte i = 0; i < 6; i++) {
  40.     key.keyByte[i] = 0xFF; // Key used is FF FF FF FF FF FF
  41.   }
  42.  
  43.   // Init LCD
  44.   display.begin();
  45.   display.setContrast(50);
  46.   display.setTextSize(1);
  47.   display.setTextColor(BLACK);
  48.   display.setRotation(2); // using it upside down
  49.   display.setCursor(0,0);
  50.   display.clearDisplay();
  51.   display.display();
  52. }
  53.  
  54. void loop() {
  55.  
  56.   // Look for new cards
  57.   if ( ! rfid.PICC_IsNewCardPresent())
  58.     return;
  59.  
  60.   // Verify if the UID has been readed
  61.   if ( ! rfid.PICC_ReadCardSerial())
  62.     return;
  63.  
  64.   // Check is the PICC of Classic MIFARE type and exit otherwise
  65.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  66.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  67.       piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  68.       piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  69.     return;
  70.   }
  71.  
  72.   // Print the UID
  73.   SerialPrintHex(rfid.uid.uidByte, rfid.uid.size);
  74.   Serial.println();
  75.   display.clearDisplay();
  76.   display.setCursor(3,display.height()/2-8);
  77.   display.print("UID: ");
  78.   lcdPrintHex(rfid.uid.uidByte, rfid.uid.size);
  79.  
  80.   // Halt PICC
  81.   rfid.PICC_HaltA();
  82. }
  83.  
  84. // Function for print byte array in hex
  85. void SerialPrintHex(byte *buffer, byte bufferSize) {
  86.   for (byte i = 0; i < bufferSize; i++) {
  87.     Serial.print(buffer[i] < 0x10 ? "0" : "");
  88.     Serial.print(buffer[i], HEX);
  89.   }
  90. }
  91.  
  92. void lcdPrintHex(byte *buffer, byte bufferSize) {
  93.   for (byte i = 0; i < bufferSize; i++) {
  94.     display.print(buffer[i] < 0x10 ? "0" : "");
  95.     display.print(buffer[i], HEX);
  96.     display.display();
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement