NFC (Near Field Communications)

If you would like to add some security to your Arduino projects, NFC or Near Field Communications is a low cost solution. We purchased a NFC module and card for under $5. This module is also supported by Raspberry Pi Python libraries.

To get things started you will need to load that Arduino MFRC522 library. You can do this by calling up the Arduino Library Manager, then search for 522.


The default circuit wiring should be:


NFC tags can contain information besides just their tag ID number.

We modified one of the library examples, to include a check based on some “good” tag IDs. The code is below:

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

#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance

#define NUMCARDS 2
int gooduid[NUMCARDS][10] = {
  {0xD5, 0xF6, 0xA6, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0},
  {0x13, 0x2F, 0x4E, 0xE5, 0x0, 0x0, 0x0, 0x0, 0x0}

void setup() {
	Serial.begin(9600);		// Initialize serial communications with the PC

	SPI.begin();			// Init SPI bus
	mfrc522.PCD_Init();		// Init MFRC522
	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

void loop() {
	// Look for new cards
	if ( ! mfrc522.PICC_IsNewCardPresent()) {

	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial()) {

	// Dump debug info about the card; PICC_HaltA() is automatically called

  // Reset the valid card checks
  bool anyok = false;
  bool cardok[NUMCARDS];
  for (int i ;  i < NUMCARDS; i++ ) {cardok[i] = false; }
  // Check the card ID   
  for (int j=0; j< NUMCARDS; j++) { 
    for (int i=0; i<mfrc522.uid.size; i++) {
      if (mfrc522.uid.uidByte[i] == gooduid[j][i]) {
        cardok[j] = true;
      } else {
        cardok[j] = false;
  // Check status of card check
  for (int i=0 ;  i < NUMCARDS; i++ ) {
    if (cardok[i] == true) {
      anyok = true;
// Print the card check status  
  if (anyok == true) { 
    Serial.println ("Good Card -- do some action");
  } else {
    Serial.println ("Invalid Card");

When the Arduino monitor is running the output should look something like:


Once we found our “good” tag Id we added a reference to it in our code:

int gooduid[NUMCARDS][10] = { {0xD5, 0xF6, 0xA6, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0},
{0x13, 0x2F, 0x4E, 0xE5, 0x0, 0x0, 0x0, 0x0, 0x0} };

Once the basic code and setup is working there are a lot of potential projects to work on.  Below is a example where we used a PowerSwitch Tail II with an NFC tag to control a power connection.


RFID Card Reader Apps

Blank RFID (Radio Frequency ID) cards along with a USB based RFID card reader can offer an easy and low cost solution for your Pi security projects.

For under $10 we were able use blank RFID cards (5 for $3) and a generic USB based RFID card reader ($6).

Using some simply Python code it is possible to make projects for security systems, door entry systems or control of remote devices.


RFID Cards and Readers

The RFID cards and readers need to work at the same frequency. For us we used the lower cost 125 KHz equipment.

The blank 125 KHz RFID cards have a predefined RFID number, this number is typically written on the card. These blank cards are be used directly, so no programming and writing to the card is required.


The USB card reader will momentarily light up and beep when it reads a card. The USB card reader act like a USB keyboard and it will pass the card’s RFID number as a series of key strokes. An <ENTER> key is sent at the end of the sequence.

Get RFID numbers and Build a Lookup Table

To verify the cards RFID number, open a terminal session and start swiping the cards. The cards ID numbers should appear at the command line  (and your system won’t know what to do with the numbers):

pi@raspberrypi:~ $ 0006241052
bash: 0006241052: command not found
pi@raspberrypi:~ $ 0003617750
bash: 0003617750: command not found

The next step is to create a simple text file (card_id.txt) with the valid RFID card numbers:


Create a Test Setup

For a simple test setup we used some Lego to security the card reader, Pi and a small breadboard.


A simple LED circuit was used, with the LED wired to GPIO 7 (BCM pin 4), and the other side to GND.


Python Application

A simple Python application can be written to:

  • read the RFID card that is swiped
  • check it against a lookup fire (card_ids.txt)
  • if the card is valid do some action (like toggle an LED).
# - read swipecard and toggle a Pi output
import RPi.GPIO as GPIO
LEDpin = 4
while True:
print "Swipe RFID card to toggle I/O"
card_id = raw_input()
print "card id is:", card_id
id_file = open("card_ids.txt","r")
for line in id_file:
if (card_id == line.strip()):
GPIO.output(LEDpin, not GPIO.input(LEDpin))
print "Valid Card"