Arduino & RFID

Arduino & RFID

I bought a RFID module some time ago, the RC522, to connect to an ESP8266. I never got that working but since I have stepped into Arduino, I wanted to give it another try. Turns out, it is very simple. Of course what helps is that there is a very good library for RFID which you can find here on github (it also has installation instructions you should follow). The MFRC522 or RC522 I bought through AliExpress as I do with a lot of my electronics.

Once you have the library installed, you need to connect the RFID reader to your Arduino. The interface is an SPI interface which means you need 4 connections to the board for the message bus, 2 for power and the RC522 requires a reset wire as well. I found this article useful to get my setup working.

I have used the following connections to get the RC522 connected to my Arduino Nano:

Description RC522 (left to right) Nano
SDA 1 10
SCK 2 13
MOSI 3 11
MISO 4 12
IRQ 5 nc
RST 7 9
3.3V 8 3.3V

And some pictures. I have given every wire a different color. Note the pin between green and black is not connected.rfid-1

And the Arduino Nano side:



It is a bit hard to see because of the angle of the picture, the black wire is connected to GND which is the second pin from the left at the top of the Arduino (if it is oriented the same way as in the picture). The others you can see more easily. The first row on my breadboard is not used, so blue is connected to the right most pin of the Nano.

Now we are all set for our first program. If you download the DumpInfo example from the RFID library examples, you should be able to run that without changing it because we are using the same pin layout as the example.

For my first program using RFID, I wanted to have the UID in a variable and not read the RFID in the loop function but rather in a seperate function.

The code:

#include <SPI.h>
#include "MFRC522.h"

#define RST_PIN         9
#define SS_PIN          10
MFRC522 mfrc522(SS_PIN, RST_PIN);
#define LED_PIN     3

String lastUID, currentUID;

void setup() {
  Serial.println("Ready to read RFID cards!");
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  lastUID = "";
  currentUID = "";

void loop() {
  if (currentUID!=lastUID) {
    if (currentUID!="") {
      Serial.print("New card: ");
      digitalWrite(LED_PIN, HIGH);
    } else {
      Serial.println("Card "+lastUID+" is gone.");
      digitalWrite(LED_PIN, LOW);
    lastUID = currentUID;

String getCurrentUID (byte *buffer, byte bufferSize) {
  String s;
  s = "";
  for (byte i=0; i<bufferSize; i++) {
    s = s+(buffer[i] < 0x10 ? "0" : "");
    s = s+String(buffer[i],HEX);
  return s;

void rfidScan() {
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    if ( ! mfrc522.PICC_IsNewCardPresent()) {
      currentUID = "";

  if ( ! mfrc522.PICC_ReadCardSerial()) {
  currentUID = getCurrentUID(mfrc522.uid.uidByte, mfrc522.uid.size);

So what I want is a variable with the current UID (currentUID) and I want to know if it has changed so I can do some action. In this example I just print to the serial monitor that a card shows up or is removed. I also turn on the internal led if there is a card present and off it is removed.

In setup we set up SPI for the RC522 and initialize serial and the variables (where lastUID is the lastUID we have seen).

In the loop function we scan the RFID reader with rfidScan and whether there is a change we do something with the led and send info to serial.

The getCurrentUID is slightly different from the some of the examples as I have removed the spaces. I want the UID to be a whole block without spaces.

Finally the function rfidScan does the actual scanning, check whether there is a card present, read it’s info and if all goes well, get the UID.

In the original code, the function mfrc522.PICC_IsNewCardPresent is called and if nothing is returned it sends a return which means the function loop starts over again. For some reason, I don’t know why, I kept on getting an empty currentUID if I only called the function once every other time, even if the card was still there. That is the reason why there is a double call to the mfrc522.PICC_IsNewCardPresent function. Very dirty but it works. If someone knows how to fix this cleaner, I would sure be happy to know.

With this program, if you put a card on the reader, you can see it and if it is removed you can also see it. It is a good start for triggering something based on an RFID card.

One thought on “Arduino & RFID

  1. Howdy! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s