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()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } // Dump debug info about the card; PICC_HaltA() is automatically called mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); // 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; break; } } } // Check status of card check for (int i=0 ; i < NUMCARDS; i++ ) { if (cardok[i] == true) { anyok = true; break; } } // 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.