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
GND 6 GND
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:

rfid-2

rfid-3

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.begin(9600); 
  Serial.println("Ready to read RFID cards!");
  Serial.println("");
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  lastUID = "";
  currentUID = "";
  SPI.begin();
  mfrc522.PCD_Init();
}

void loop() {
  rfidScan();
  if (currentUID!=lastUID) {
    if (currentUID!="") {
      Serial.print("New card: ");
      Serial.println(currentUID);
      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);
  }
  s.toUpperCase();
  return s;
}

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

  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  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.

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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