Read RFID with the RC522 on Arduino

FunduinoLast updated on

Karten und Schlüsselanhänger erkennen und damit LEDs schalten

Der RFID-Empfänger („radio-frequency identification“) liest von RFID-Sendern – auch „RFID-Tags“ genannt – per Funk einen bestimmten Code aus. Jeder Sender hat dabei einen einmaligen, ganz individuellen Code. Damit kannst du mit dem Arduino Schließanlagen oder ähnliche Projekte verwirklichen, bei denen sich eine Person mit einem Sender identifizieren soll.

RFID-Tags können verschiedene Formen haben, zum Beispiel Schlüsselanhänger oder Karten im Kreditkartenformat. Auf dem Bild siehst du links zwei RFID-Tags und rechts den RFID-Empfänger.

Ein RFID-Empfänger enthält eine kleine Kupferspule, die ein magnetisches Feld erzeugt. Ein RFID-Sender enthält ebenfalls eine Kupferspule, die das magnetische Feld aufgreift und im Sender eine elektrische Spannung erzeugt. Diese Spannung bringt dann einen kleinen elektronischen Chip dazu, per Funk einen elektrischen Code auszusenden. Diesen Code kann der Mikrocontroller anschließend weiterverarbeiten.

Aufgabe: Mit einem Arduino-Mikrocontroller liest du einen RFID-Tag aus. Im nächsten Schritt schaltest du zwei LEDs mit zwei verschiedenen Karten ein und aus.

RFID kit with Mifare RC522: two RFID tags on the left, the RFID receiver on the right
Mifare RC522 RFID Kit

You need this

RC522 RFID kit - 13.56 MHz, with card and key fob RC522 RFID kit - 13.56 MHz, with card and key fob 2,86 € *Item no. FUN-1010560About the product — RC522 RFID kit - 13.56 MHz, with card and key fob
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V

All 5 parts together 20,59 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Wire the RFID receiver

The RFID receiver RFID-RC522 comes with a 90° pin header already soldered on. Some versions require you to solder the pin header to the RFID receiver yourself.

Here's how to wire the RFID receiver to the Arduino board:

  • SDA – Pin 10 on the UNO, Pin 53 on the Mega
  • SCK – Pin 13 on the UNO, Pin 52 on the Mega
  • MOSI – Pin 11 on the UNO, Pin 51 on the Mega
  • MISO – Pin 12 on the UNO, Pin 50 on the Mega
  • IRQ – leave unconnected
  • RST – Pin 9 on the UNO, Pin 5 on the Mega
  • GND – GND
  • 3.3V – 3.3V

The sketches in this guide are intended for the UNO R3. With the MEGA2560 and other controllers, you need to adjust the pins accordingly.

Schematic with RC522 RFID receiver on the Arduino UNO, plus a yellow LED on pin 7 and a green LED on pin 6
Circuit diagram: RC522 and two LEDs connected to an Arduino UNO
Overview of the RC522 RFID reader connections to the Arduino UNO
RFID receiver connections on the Arduino UNO
1 / 2

Install the MFRC522 library

Reading and processing data from an RFID reader would require many lines of source code, just like other complex tasks. That’s why we use a pre-made library.

You can find the “MFRC522” library by GithubCommunity in the Library Manager by searching for “MFRC522” and install it from there. For details on how to do this, see Adding Libraries to the Arduino Software.

Library manager for the Arduino software with the search result MFRC522 from GithubCommunity
The MFRC522 library in the Library Manager

Read the UID of an RFID tag

First, read the UID (“Unique Identification Number”), the unique identifier of an RFID tag. To do this, use the following program.

If everything worked, the Serial Monitor displays the decimal value of the card held up to the reader.

The program only works if you have added the library to the Arduino software as described in step 2.

Output in the Serial Monitor: Card detected and the decimal value of the UID
This is what the result looks like in the Serial Monitor.
Program code - read the UID of an RFID tag
// RFID-Bibliothek hinzufügen
#include "MFRC522.h"
// Anschlüsse definieren
#define SDA 10
#define RST 9
// RFID-Empfänger benennen, Pins zuordnen
MFRC522 mfrc522(SDA, RST);
void setup()
{
  Serial.begin(9600);
  SPI.begin();
  // Initialisierung des RFID-Empfängers
  mfrc522.PCD_Init();
}
void loop()
{
  String WertDEZ;
  // Wenn keine Karte in Reichweite ist ..
  if (!mfrc522.PICC_IsNewCardPresent())
  {
    // .. wird die Abfrage wiederholt.
    return;
  }
  // Wenn kein RFID-Sender ausgewählt wurde ..
  if (!mfrc522.PICC_ReadCardSerial())
  {
    // .. wird die Abfrage wiederholt.
    return;
  }
  Serial.println("Karte entdeckt!");
  // Dezimal-Wert in Strings schreiben
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    // String zusammenbauen
    WertDEZ = WertDEZ + String(mfrc522.uid.uidByte[i], DEC) + " ";
  }
  // Kennung dezimal anzeigen
  Serial.println("Dezimalwert: " + WertDEZ);
  // kurze Pause, damit nur ein Wert gelesen wird
  delay(1000);
}

Extension: Control two LEDs with two cards

Now you'll expand the program: When you hold a specific card up to the reader, either the green or the yellow LED should light up. When you hold the same card up to the RFID reader again, the corresponding LED turns off.

Explanation of the extension: Define the LEDs using define on pins 6 and 7. Set the LED states (StatusgelbeLED and StatusgrueneLED) to false - the LEDs are therefore off at startup. The WertDEZ variable contains the card's unique UID.

When the correct UID is recognized, the program switches the yellow LED on or off: The StatusgelbeLED variable is "toggled" from false to true, and the LED lights up. When you hold the card up again, it changes from true back to false, and the yellow LED turns off. The same then happens with the green LED.

Of course, you need to replace the UIDs mentioned in the example with the UIDs you determined yourself in step 3.

Program code - Switch two LEDs with two cards
// RFID-Bibliothek hinzufügen
#include "MFRC522.h"
// Anschlüsse definieren
#define SDA 10
#define RST 9
// LEDs
#define grueneLED 6
#define gelbeLED 7
// Zustand der LEDs false = beim Start ausgeschaltet
bool StatusgelbeLED = false;
bool StatusgrueneLED = false;
// RFID-Empfänger benennen, Pins zuordnen
MFRC522 mfrc522(SDA, RST);
void setup()
{
  Serial.begin(9600);
  SPI.begin();
  // Initialisierung des RFID-Empfängers
  mfrc522.PCD_Init();
  pinMode(gelbeLED, OUTPUT);
  pinMode(grueneLED, OUTPUT);
}
void loop()
{
  String WertDEZ;
  // Wenn keine Karte in Reichweite ist ..
  if (!mfrc522.PICC_IsNewCardPresent())
  {
    // .. wird die Abfrage wiederholt.
    return;
  }
  // Wenn kein RFID-Sender ausgewählt wurde ..
  if (!mfrc522.PICC_ReadCardSerial())
  {
    // .. wird die Abfrage wiederholt.
    return;
  }
  Serial.println("Karte entdeckt!");
  // Dezimal-Wert in Strings schreiben
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    WertDEZ = WertDEZ + String(mfrc522.uid.uidByte[i], DEC) + " ";
  }
  // trim() -> Leerzeichen am Ende des Strings entfernen
  WertDEZ.trim();
  // Kennung dezimal anzeigen
  Serial.println("Dezimalwert: " + WertDEZ);
  // gelbe LED schalten
  if (WertDEZ == "67 217 130 21")
  {
    // Status ändern an/aus
    StatusgelbeLED = !StatusgelbeLED;
    digitalWrite(gelbeLED, StatusgelbeLED);
  }
  // grüne LED schalten
  if (WertDEZ == "3 208 23 23")
  {
    // Status ändern
    StatusgrueneLED = !StatusgrueneLED;
    digitalWrite(grueneLED, StatusgrueneLED);
  }
  // Die Wartezeit verhindert, dass dieselbe Karte mehrfach hintereinander erkannt wird
  delay(1000);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.