Toggle Nav

Interfacing PN532 NFC RFID Module with Arduino

Table of Contents

PN532 NFC RFID Module Features

If you are experienced in electronics, you have probably heard of RFID (radio-frequency-identification) and NFC (near-field-communication). PN532 module is a kind of RFID-NFC module that uses I2C, SPI and HSU protocols for communication. This module is based on NXP PN532 IC. The key features are:

  • Support I2C, SPI, high-speed UART (HSU) protocols
  • PCB design for antenna
  • Communication distance: 5 to 7cm
  • RFID reader and writer supports the following:
  1. Mifare 1K 4K, Ultralight and DesFire cards
  2. ISO / IEC 14443-4 cards such as CD97BX, CD light, DesFire and P5CN072 (SMX)
  3. Innovision Jewel cards such as the IRT5001 card
  4. FeliCa cards such as RCS_860 and RCS_854

You can download the datasheet of this module here.

PN532 NFC RFID Module Pinout

As mentioned above, this module supports HSU, SPI and I2C communication protocols. The module uses separate pins for SPI protocol, but the pins used for I2C and HSU communication protocols are the same. The names of the I2C pins are on the front of the module and the names of the HSU pins are on the back of the module. HSU is the default protocol of the module. You can change the communication protocol of the module to one of the I2C, SPI or HSU using DIP switches on the module.

This Module has 16 pins. The pins of each protocol are as follows:

I2C protocol:

  • VCC: Module power supply – 5V
  • GND: Ground
  • SDA: Data pin
  • SCL: Clock pin

SPI protocol:

  • VCC: Module power supply – 3-5V
  • GND: Ground
  • SCK: Clock pin
  • MISO: Output data pin
  • MOSI: Input data pin
  • SS: Slave select
  • IRQ: Interrupt pin
  • RSTO: Reset pin

HSU protocol:

  • VCC: Module power supply – 5V
  • GND: Ground
  • TXD: Transmit data pin
  • RXD: Receive data pin

You can see the pinout of this module in the image below.

Required Materials

Hardware Components

Arduino UNO R3 × 1
PN532 NFC Module × 1
Male to Female jumper wire × 1

Software Apps

Arduino IDE

Interfacing PN532 NFC RFID Module with Arduino

Step 1: Circuit

The following circuit show how you should connect Arduino to PN532 module. Connect wires accordingly.

Step 2: Library

Download the library Zip file here. Unzip the file and copy the folders to Arduino installation section (Libraries section).

Note

If you need more help with installing a library on Arduino, read this tutorial: How to Install an Arduino Library

Step 3: Code

Upload the following code to Arduino. After that open the Serial Monitor.

    /*
  PN532-NFC-RFID-Module-Library
  modified on 18 Nov 2020
  by Amir Mohammad Shojaee @ Electropeak
  https://electropeak.com/learn/

  based on www.electroschematics.com Arduino Examples
*/

#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("");

    // 2 second halt

    delay(2000);

  }

  else

  {

    // PN532 probably timed out waiting for a card

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

  }
}

The required libraries must be included first. This code is for HSU mode. Next, 2 pins 10 and 11 are defined as serial pins in order to connect to the module. If we bring the card near to the module, it is detected and its data appears on Serial Monitor.

See the image below. Hold the card close to the module and then take it away. As you see, first the card is detected and then its data is displayed on Serial Monitor.

Liked What you see?

Get updates and learn from the best

More To Explore

Leave a Reply

Your email address will not be published. Required fields are marked *