Advertisement
microrobotics

Arduino interfaced with RC522

Aug 30th, 2024
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3.  
  4. #define SS_PIN 10  // SDA pin
  5. #define RST_PIN 9  // RST pin
  6.  
  7. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  8.  
  9. void setup() {
  10.   Serial.begin(9600); // Initialize serial communications with the PC
  11.   SPI.begin();        // Init SPI bus
  12.   mfrc522.PCD_Init(); // Init MFRC522
  13.   Serial.println("Scan an RFID tag...");
  14. }
  15.  
  16. void loop() {
  17.   // Look for new cards
  18.   if (!mfrc522.PICC_IsNewCardPresent()) {
  19.     return;
  20.   }
  21.  
  22.   // Select one of the cards
  23.   if (!mfrc522.PICC_ReadCardSerial()) {
  24.     return;
  25.   }
  26.  
  27.   // Show UID on serial monitor
  28.   Serial.print("UID tag :");
  29.   String content = "";
  30.   for (byte i = 0; i < mfrc522.uid.size; i++) {
  31.     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  32.     Serial.print(mfrc522.uid.uidByte[i], HEX);
  33.     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
  34.     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  35.   }
  36.   Serial.println();
  37.   delay(1000);
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement