NFC Tag Reader on Raspberry Pi

Venerable PN532 based NFC readers are cheap and easy to find. You can get one from ebay or aliexpress as low as $5 or even less.

And these readers are supported by libnfc so you can install driver directly from Debian package repository.

$ sudo apt install libnfc-bin libnfc-dev
$ sudo apt install libnfc-examples libnfc-pn53-examples

You need to enable I2C interface if not done already. If enabled, you should be able to see the device under /dev folder. Depending on the hardware it could be /dev/i2c-0.

libnfc checks its configuration file for hardware settings. Open up the file /etc/nfc/libnfc.conf and make sure it has following settings enabled. You may need to sudo to change the contents. For more information about the settings, check this page.

allow_autoscan = false
device.name = "PN532"
device.connstring = "pn532_i2c:/dev/i2c-1"

At this point you should be able to compile and run a program using C language. A simple example can be found here. If you need a reference manual of the library, download a stable source tar ball from this page and follow the instruction to generate documentation or simply run a doxygen.

To use python, you can use ctypes. Load the library and create python proxy for the following two header files

/usr/include/nfc/nfc.h
/usr/include/nfc/nfc-types.h

Then you can call the same functions in python.

A python example in this repository runs a separate thread for NFC tag reading. Thus main thread is free from doing other tasks instead of being blocked by the NFC function calls. It displays tag ID whenever new tag is detected until a certain tag is showed up.