Skip to content
Seeed-StudioPublic
forked from xiongyihui/PN532

About

NFC library using PN532 to read/write card and communicate with android

Resources

Stars

503 stars

Watchers

48 watching

Forks

 
 

Latest commit

 

History

220 Commits

Folders and files

Repository files navigation

NFC library

This is an library for PN532 to use NFC technology. It is for NFC Shield and Grove - NFC.

NFC Shield Grove - NFC

Features

To Do

  • To support more than one INFO PDU of P2P communication
  • To read/write NFC Type 4 tag

Getting Started

Using Arduino IDE

  1. Download zip file, extract it into Arduino's libraries and rename it to PN532-Arduino.
  2. Download Don's NDEF library, extract it into Arduino's libraries and rename it to NDEF.
  3. Follow the examples of the two libraries.

Since v1.1.0 no build flags or #defines are needed: the I2C, SPI and HSU interfaces are compiled automatically. Only the SWHSU interface still has to be enabled explicitly (see SWHSU below).

PlatformIO library

Add https://github.com/Seeed-Studio/PN532.git to your lib_deps variable in platformio.ini like so. This library will automatically include Don's NDEF library as well.

lib_deps =
    https://github.com/Seeed-Studio/PN532.git

Since v1.1.0 the -DNFC_INTERFACE_<interface> build flag is optional. I2C, SPI and HSU are compiled by default, so a plain include is enough:

#include <PN532_I2C.h>
#include <PN532.h>

If you want to build only one interface (e.g. to save flash or to avoid an available dependency), you can still select it explicitly:

build_flags =
    -DNFC_INTERFACE_HSU

Git way for Linux/Mac (recommended)

  1. Get PN532 library and NDEF library

    cd {Arduino}\libraries
    git clone --recursive https://github.com/Seeed-Studio/PN532.git NFC
    git clone --recursive https://github.com/don/NDEF.git NDEF
    ln -s NFC/PN532 ./
    ln -s NDEF/NDEF ./
    
  2. Add the NFC_INTERFACE_<interface> build flag to your build system or define it in your code using #define NFC_INTERFACE_<interface> like

    #define NFC_INTERFACE_I2C
  3. Follow the examples of the two libraries

Interfaces

This library offers four ways to interface with the PN532 board:

  • HSU (High Speed Uart)
  • I2C
  • SPI
  • SWHSU (Software-based High Speed Uart)

Read the section for the interface you want to use.

Since v1.1.0 all you need is the right PN532_<interface>.h include file — I2C, SPI and HSU are compiled automatically:

#include <PN532_HSU.h>
#include <PN532.h>

Only SWHSU must be enabled explicitly, because it requires SoftwareSerial, which is not available on every board (see below).

HSU Interface

HSU is short for High Speed Uart. HSU interface needs only 4 wires to connect PN532 with Arduino, Sensor Shield can make it more easier. For some Arduino boards like Leonardo, DUE, Mega ect, there are more than one Serial on these boards, so we can use this additional Serial to control PN532, HSU uses 115200 baud rate.

To use the Serial1 control PN532, refer to the code below.

/* If you need to specify the pin: For ESP32, you can use PN532_HSU pn532hsu(Serial1, 36, 4) to specify the pin;
For other series that support soft serial port, you can use the soft serial port to specify the pin:
#define USE_SOFT_SERIAL_PIN
#include <PN532_HSU.h>
#include <PN532.h>

SoftwareSerial mysoft_serial(D7,D6);
PN532_HSU pn532hsu(mysoft_serial);
PN532 nfc(pn532hsu);
*/ 

#include <PN532_HSU.h>
#include <PN532.h>

PN532_HSU pn532hsu(Serial1);
PN532 nfc(pn532hsu);

void setup(void)
{
	nfc.begin();
	//...
}

If your Arduino has only one serial interface and you want to keep it for control or debugging with the Serial Monitor, you can use the SoftwareSerial library to control the PN532 by emulating a serial interface. Include PN532_SWHSU.h instead of PN532_HSU.h.

SWHSU is the one interface that is not compiled by default, because it depends on SoftwareSerial, which is not available on every board. Enable it with a global build flag (-DNFC_INTERFACE_SWHSU), or — in the Arduino IDE — by defining the macro and including the implementation in your sketch:

#define NFC_INTERFACE_SWHSU

#include <SoftwareSerial.h>
#include <PN532_SWHSU.h>
#include <PN532_SWHSU.cpp> // only needed when the macro is defined in the sketch
#include <PN532.h>

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

PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );

void setup(void)
{
	nfc.begin();
	//...
}

Attribution

This library is based on Adafruit_NFCShield_I2C. Seeed Studio rewrite the library to make it easy to support different interfaces and platforms. @Don writes the NDEF library to make it more easy to use. @JiapengLi adds HSU interface. @awieser adds card emulation function.

About

NFC library using PN532 to read/write card and communicate with android

Resources

Stars

503 stars

Watchers

48 watching

Forks

Releases

Packages

Used by

Contributors

Languages