|
| 1 | +// This code only takes the UID in string form. |
| 2 | +// If you want to see more detailed information about an RFID card or keychain, please run the program code "DumpInfo". |
| 3 | +// The "DumpInfo" program code is located at : |
| 4 | +// Library folder "MFRC522" -> "examples" folder -> "DumpInfo" folder -> DumpInfo.ino |
| 5 | +// or |
| 6 | +// From Arduino IDE : File -> Examples -> MFRC522 -> DumpInfo |
| 7 | +// or |
| 8 | +// From Arduino IDE : File -> Examples -> INCOMPATIBLE-> MFRC522 -> DumpInfo |
| 9 | + |
| 10 | +//----------------------------------------Including the libraries. |
| 11 | +#include <SPI.h> |
| 12 | +#include <MFRC522.h> |
| 13 | +//---------------------------------------- |
| 14 | + |
| 15 | +// Defines SS/SDA PIN and Reset PIN for RFID-RC522. |
| 16 | +#define SS_PIN 5 |
| 17 | +#define RST_PIN 4 |
| 18 | + |
| 19 | +// Variable to read data from RFID-RC522. |
| 20 | +int readsuccess; |
| 21 | +char str[32] = ""; |
| 22 | +String UID_Result = ""; |
| 23 | + |
| 24 | +// Create MFRC522 object as "mfrc522" and set SS/SDA PIN and Reset PIN. |
| 25 | +MFRC522 mfrc522(SS_PIN, RST_PIN); |
| 26 | + |
| 27 | +//________________________________________________________________________________getUID() |
| 28 | +// Subroutine to obtain UID/ID when RFID card or RFID keychain is tapped to RFID-RC522 module. |
| 29 | +int getUID() { |
| 30 | + if(!mfrc522.PICC_IsNewCardPresent()) { |
| 31 | + return 0; |
| 32 | + } |
| 33 | + if(!mfrc522.PICC_ReadCardSerial()) { |
| 34 | + return 0; |
| 35 | + } |
| 36 | + |
| 37 | + byteArray_to_string(mfrc522.uid.uidByte, mfrc522.uid.size, str); |
| 38 | + UID_Result = str; |
| 39 | + |
| 40 | + mfrc522.PICC_HaltA(); |
| 41 | + mfrc522.PCD_StopCrypto1(); |
| 42 | + |
| 43 | + return 1; |
| 44 | +} |
| 45 | +//________________________________________________________________________________ |
| 46 | + |
| 47 | +//________________________________________________________________________________byteArray_to_string() |
| 48 | +void byteArray_to_string(byte array[], unsigned int len, char buffer[]) { |
| 49 | + for (unsigned int i = 0; i < len; i++) { |
| 50 | + byte nib1 = (array[i] >> 4) & 0x0F; |
| 51 | + byte nib2 = (array[i] >> 0) & 0x0F; |
| 52 | + buffer[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; |
| 53 | + buffer[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; |
| 54 | + } |
| 55 | + buffer[len*2] = '\0'; |
| 56 | +} |
| 57 | +//________________________________________________________________________________ |
| 58 | + |
| 59 | +//________________________________________________________________________________VOID SETUP() |
| 60 | +void setup(){ |
| 61 | + // put your setup code here, to run once: |
| 62 | + |
| 63 | + Serial.begin(115200); |
| 64 | + Serial.println(); |
| 65 | + delay(1000); |
| 66 | + |
| 67 | + // Init SPI bus. |
| 68 | + SPI.begin(); |
| 69 | + // Init MFRC522. |
| 70 | + mfrc522.PCD_Init(); |
| 71 | + |
| 72 | + delay(1000); |
| 73 | + |
| 74 | + Serial.println(); |
| 75 | + Serial.println("Please tap your card or key chain to the RFID-RC522 module."); |
| 76 | +} |
| 77 | +//________________________________________________________________________________ |
| 78 | + |
| 79 | +//________________________________________________________________________________VOID LOOP() |
| 80 | +void loop(){ |
| 81 | + // put your main code here, to run repeatedly: |
| 82 | + |
| 83 | + readsuccess = getUID(); |
| 84 | + |
| 85 | + if(readsuccess){ |
| 86 | + Serial.println(); |
| 87 | + Serial.print("UID : "); |
| 88 | + Serial.println(UID_Result); |
| 89 | + delay(2000); |
| 90 | + } |
| 91 | + delay(10); |
| 92 | +} |
| 93 | +//________________________________________________________________________________ |
| 94 | +//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
0 commit comments