2019年8月5日 星期一

PN532 NFC RFID Module

Advertisement
If you’re an active electronics hobbyist, you’re likely familiar with the terms “near-field communication” (NFC) and “radio frequency identification” (RFID). Well, do you have an NFC project in mind? Or do you have an irresistible temptation to build your own RFID access system? In any case, this article will clear a few doubts that you may have about NFC and RFID DIY projects.

How to begin

For starters, you need a pre-wired NFC RFID module as the key hardware. NFC and RFID projects have become a huge trend, but many modules are pricey for electronics hobbyists and need a complex setup. Fortunately, the PN532 NFC RFID module is one of the more affordable NFC and RFID modules. The most common module/breakout board is the PN532 NFC RFID module (v3) by Elechouse.


This module is built around NXP PN532, and the maker break out almost all of the I/O pins of the NXP532 chip on this little module. This Arduino-compatible module has the following features:

  • Supports II2, SPI, and high-speed UART (HSU)
  • RFID reader/writer mode support for:
    • Mifare 1K, 4K, Ultralight, and DesFire cards
    • ISO/IEC 14443-4 cards such as CD97BX, CD light, DesFire, and P5CN072 (SMX)
    • Innovision Jewel cards such as the IRT5001 card
    • FeliCa cards such as RCS_860 and RCS_854
  • Built-in PCB antenna (covered by white paint)
  • 5- to 7-cm communication distance
  • On-board level shifter
  • Standard 5-V TTL for I2C/UART and 3.3-V TTL SPI (VCC 3.3 to 5 V)
  • Works as an RFID reader/writer
  • Works as a 1443-A card or a virtual card
  • Supports NFC with Android phone

In the module, the I2C and HSU share the same pins. The definition of the I2C pins is labeled on top of the breakout board, while the HSU’s is labeled at the bottom. Although the HSU mode is configured as the default mode, with a small on-board SMD DIP switch, it becomes pretty easy to change among I2C, SPI, and HSU interfaces.


I am guessing that you have an Arduino Uno board handy. Even though most of the available libraries (for PN532) are focused primarily on Arduino Mega, I still prefer the Uno, as it’s more comfortable for a beginner. Apart from the NFC RFID module, you need only an Arduino board to complement the test setup.

Now, you can download the PN532 library from here. This new library is based on Adafruit_NFCShield_I2C, improved by Seeed Studio, with an HSU driver added by Elechouse. The library supports all interfaces of PN532 and works well with Don’s NDEF Library. After downloading the zip file, extract the following six folders into Arduino’s libraries:

  • PN532
  • PN532_SPI
  • PN532_I2C
  • PN532_HSU
  • PN532_SWHSU
  • NDEF

See the partial snap of my libraries folder below:


Testing the hardware

Because the default communication mode/interface of the module is HSU, let’s continue without bearing on the selector switch right now. Just take a set of breadboard jumper wires and follow the hardware setup as shown in the below table:

PN532 NFC RFID MODULEARDUINO UNO
VCC5 V
GNDGND
RXD11
TXD10

Soldering right-angled male-header pins (which come with the module) on the breakout board will ease up the jumper wire interlinks. If you directly solder bare wires on top, make sure that the wires go across the antenna lines at a 90° angle. My test setup is shown below:


Now, onto a simple test sketch (code) tailored for running HSU mode with Arduino Uno. The HSU interface needs only four wires to connect PN532 with Arduino (HSU uses 115200 baud rate), and in some Arduino boards (such as Leonardo, DUE, and Mega), there is more than one serial, so we can use the additional serial (serial 1) to control PN532. However, here, the Arduino Uno has only one serial interface, and we want to keep it for control or debugging with the Serial Monitor, so the “SoftwareSerial” library is called to control PN532 by emulating a serial interface. (Arduino Uno has only one serial interface, which is also connected to the USB port of the PC. In HSU mode, the Serial Monitor could not be used as a message displaying window. That’s why the software serial trick is used here.)

/*

* PN532 NFC RFID Module (v3)

* Quick Test Code v2

* Based on an example code from PN532 Arduino Library

* Tailored for HSUART & Arduino Uno

* T.K.Hareendran/2019

* www.electroschematics.com

*/

#include <SoftwareSerial.h>

#include <PN532_SWHSU.h>

#include <PN532.h>

SoftwareSerial SWSerial( 10, 11 ); // RX, TX

PN532_SWHSU pn532swhsu( SWSerial );

PN532 nfc( pn532swhsu );


void setup(void) {

  Serial.begin(115200);

  Serial.println("Hello Maker!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();

  if (! versiondata) {

    Serial.print("Didn't Find PN53x Module");

    while (1); // Halt

  }

  // Got valid data, print it out!

  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);

  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);

  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // Configure board to read RFID tags

  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A Card ...");

}

void loop(void) {

  boolean success;

  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID

  uint8_t uidLength;                       // Length of the UID (4 or 7 bytes depending on ISO14443A card type)

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);

  if (success) {

    Serial.println("Found A Card!");

    Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");

    Serial.print("UID Value: ");

    for (uint8_t i=0; i < uidLength; i++)

    {

      Serial.print(" 0x");Serial.print(uid[i], HEX);

    }

    Serial.println("");

    // 1 second halt

    delay(1000);

  }

  else

  {

    // PN532 probably timed out waiting for a card

    Serial.println("Timed out! Waiting for a card...");

  }

}
Now, just upload the sketch to Arduino Uno and open the Serial Monitor. When you put the ISO14443A card/tag on the module, you will get an output similar to this one in your Serial Monitor window:


You could try other example codes in the PN532 library. Remember, there’s also another renowned library in front of us — the Adafruit PN532 library — for SPI and I2C access to the PN532 RFID NFC chip.

Proximity cards & tags

The HF RFID (13.56 MHz) card tested here seems to be a MiFare Classic card (UID length 4 bytes and UID value 29-21-B0-23). See the “PuTTY” window captured while doing a second test with “readMifare” example code from the PN532 (Elechouse) library:


MiFare is one of the four 13.56-MHz card protocols (FeliCa is another popular one). MiFare Classic cards come in 1K and 4K varieties but typically have a 4-byte NUID that uniquely identifies the card (it’s possible to have a 7-byte ID as well). Mifare Classic cards are divided into sections called sectors and blocks. Each sector has individual access rights and contains a fixed number of blocks that are controlled by these access rights. Each block contains 16 bytes, and sectors contains either four blocks (1K/4K cards) for a total of 64 bytes per sector or 16 blocks (4K cards only) for a total of 256 bytes per sector. The card types are organized as:

  • 1K cards — 16 sectors of four blocks each (sectors 0–15)
  • 4K cards — 32 sectors of four blocks each (sectors 0–31) and eight sectors of 16 blocks each (sectors 32–39)

Notably, the first block is very special and is called the manufacturer block and should generally be avoided. It may have a different key set (different from the default key values that are assumed by your example sketches) preventing you from accessing it. For more in-depth learning, you can visit here and here.

Moving toward NFC experiments

There’s certainly more fun and practical stuff that we can do with NFC. At the start, you can use the same hardware setup tried earlier but with a new test code included here. In this NFC experiment, the I2C interface is used, so remember to set the selector switch of the module to I2C mode; i.e., just move the selector 1 of the micro DIP switch to 1 (on) and selector 2 to 0 (off). Additionally, route the SDA of the module to A4 of Uno and SCL to A5.


/*

* PN532 NFC RFID Module (v3)

* NFC Tag Quick Test Code v1

* Based on an example code from PN532 Arduino Library

* Tailored for I2C & Arduino Uno

* Remember to set mode switch of PN532 module to I2C!

* T.K.Hareendran/2019

* www.electroschematics.com

*/

#include <Wire.h>

#include <PN532_I2C.h>

#include <PN532.h>

#include <NfcAdapter.h>

PN532_I2C pn532_i2c(Wire);

NfcAdapter nfc = NfcAdapter(pn532_i2c);

/* Uno's A4 to SDA & A5 to SCL */

void setup(void) {

    Serial.begin(9600);

    Serial.println("NDEF Reader");

    nfc.begin();

}

void loop(void) {

    Serial.println("\nScan a NFC tag\n");

    if (nfc.tagPresent())

    {

        NfcTag tag = nfc.read();

        tag.print();

    }

    delay(5000);

}

Once you have saved and uploaded this code, the Serial Monitor should display a message: “NDEF Reader.” When I put one tag, I get this on my Serial Monitor:


Notice that it delivers the unique identification of the tag and tells me something more about my tag. Not an NDEF-formatted tag? Sadly, it’s been a couple of years since I actively did any experiment around proximity cards and tags, so I’ll have to burn the midnight oil to brush up on my knowledge of current NFC technology.

In fact, I just now relearned that the NFC Data Exchange Format (NDEF) is a standardized data format that can be used to store and exchange information (URLs, plain text, etc.) between any compatible NFC device and another NFC device/tag. NFC tags like Mifare Classic cards can be configured as NDEF tags, and data written to them by one NFC device (NDEF records) can be understood and accessed by any other NDEF-compatible device. Furthermore, NDEF messages can be used to exchange data between two active NFC devices in “peer-to-peer” mode.

By the end of the night, I’d successfully formatted one Mifare Classic tag and saved a link to my Facebook profile, with the help of two example sketches in the NDEF library.


Because I don’t have a smart phone with NFC, I got the exact link when I scanned the tag on my previous NFC tag reader setup (NFC Tag Quick Test Code v1). It worked!


Way to go

There are countless possibilities for using NFC and RFID. Start playing around with them, and use your imagination to find awesome ideas and build fantastic projects. There will be a follow-up to this, so stay tuned.


沒有留言:

張貼留言

WOKWI DHT22 & LED , Node-Red + SQLite database

 WOKWI DHT22 & LED , Node-Red + SQLite database Node-Red程式 [{"id":"6f0240353e534bbd","type":"comment&...