Raspberry Pi and NFC

From packets2photons
Jump to navigation Jump to search

This guide will show you how to read NFC cards with a Raspberry Pi. The assumption is that you are using Raspbian and have the Adafruit PN532 NFC. This guide makes use of the guide: http://wiki.sunfounder.cc/index.php?title=PN532_NFC_Module_for_Raspberry_Pi

These instructions will show you how to setup SPI communication between your raspberry pi and the PN532 NFC Shield.

Start by enabling SPI

sudo raspi-config

Then select Select 5 Interfacing Options -> SPI -> yes. Then get the required packages to build libnfc:

sudo apt-get update
sudo apt-get install libusb-dev libpcsclite-dev i2c-tools

Now, we will download libnfc version 1.7.1

cd ~
wget https://wiki.packets2photons.com/libnfc-1.7.1.tar.bz2 
tar -xf libnfc-1.7.1.tar.bz2  

Compile and install the package

cd libnfc-1.7.1
./configure --prefix=/usr --sysconfdir=/etc
make
sudo make install 

Create the configuration file

cd /etc
sudo mkdir nfc
sudo vim /etc/nfc/libnfc.conf

The file will be blank. Paste in the following:


# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to set manually a device
# configuration using file or environment variable
allow_autoscan = true

# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, user should prefer to add manually his device.
allow_intrusive_scan = false

# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
log_level = 1

# Manually set default device (no default)
# To set a default device, you must set both name and connstring for your device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "_PN532_SPI"
device.connstring = "pn532_spi:/dev/spidev0.0:50000"
#device.name = "_PN532_I2c"
#device.connstring = "pn532_i2c:/dev/i2c-1"

Carefully examine the images provided and wire the Raspberry Pi to the PN532.

5 This image shows all the pins on the Raspberry Pi, with the relevant ones circled with blue
This image shows all the pins on the Raspberry Pi, with the relevant ones circled with blue
5 This photo shows the wiring of the Raspberry Pi with the PN532
This photo shows the wiring of the Raspberry Pi with the PN532

Check whether the SPI port is open:

ls /dev/spidev0.* 

This command should return /dev/spidev0.0 /dev/spidev0.1

Then type

sudo /home/pi/libnfc-1.7.1/utils/nfc-list

You should see NFC device: pn532_spi:/dev/spidev0.0 opened

Re-run that command again, this time with a NFC card above the radio

 sudo /home/pi/libnfc-1.7.1/utils/nfc-list

You should successfully read some data from your card:

/home/pi/libnfc-1.7.1/utils/.libs/lt-nfc-list uses libnfc 1.7.1
NFC device: pn532_spi:/dev/spidev0.0 opened
1 ISO14443A passive target(s) found:
ISO/IEC 14443A (106 kbps) target:
    ATQA (SENS_RES): 00  89  
       UID (NFCID1): af  43  a2  fe  
      SAK (SEL_RES): 26

The outputs above have been changed and in no way refers to any of my cards. You should be equally careful about what if any of the NFC data from your cards is shared.

Carefully look at this link to work out what type of NCF is in use.