Saturday, March 26, 2016

How to configure ITEAD PN532 NFC Module on Raspberry Pi

The other day I received the Itead PN532 NFC module in the mail. Time to install it on Raspberry Pi.

The PN532 allows for Near Field Communications, e,g, for using an access card to enter a building or transferring info from a smart phone to a computer.

The installation instructions that came with the device were rather vague or shall I say non existent. And so was any other information about it on the Interweb. Piecing together various snippets allowed me to install it and actually have it work. That is why I am documenting it here just in case someone else comes across the same problem.

In am installing it on Raspberry Pi 2, with Raspbian Jessie as the OS. Furthermore, I will be using it with NFC.py, a Python library I am quite familiar with. It uses the UART set up (serial).

First off, we will not use the double row of pins (26 pins) on the PN532. We will use the single row of 8 pins, of which only 4 will actually be connected to the Pi. Note that you will have to flip the device over to read the markings for each pin on the back of the PN532.

The connection from the Pi to the PN532 is as follows:

Connect Pin 2 (5V) on the Pi to the Pin marked 5V on the PN532
Connect Pin 6 (Ground) on the Pi to the Pin marked GND on the PN532
Connect Pin 8 (GPIO14) (UART TX) on the Pi to the Pin marked NSS/RCL/RX on the PN532
Connect Pin 10 (GPIO15) (UART RX) on the Pi to the Pin marked MO/SDA/TX on the PN532

So much for the hardware side. Now we go on to the software side. Of course, we bring the entire system up to the latest value by issuing:

sudo apt-get update
sudo apt-get upgrade


Then, we need to modify the file /boot/cmdline.txt. Do that by issuing the following command:

sudo nano /boot/cmdline.txt
 
Within this file, change the line


dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200
 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
 
to this:
 
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 
elevator=deadline fsck.repair=yes rootwait
 
Next, if you are running Raspbian Jessie on a Pi 3, add this to the bottom of /boot/config.txt file:
 
dtoverlay=pi3-miniuart-bt
 
This will disable Bluetooth, but allow the PN532 to work.
 
Reboot the Pi, and install NFC.py by  issuing the following commands:

sudo apt-get install python-dev 
curl -O http://python-distribute.org/distribute_setup.py 
sudo python distribute_setup.py 
curl -O https://raw.githubusercontent.com/pypa/pip/master/contrib/get-pip.py 
sudo python get-pip.py 
sudo pip install virtualenv

The above 6 commands install (fairly) standard Python utilities. They may be present on your machine already.

sudo pip install pyserial

This line will install serial library for Python used by NFC.py.

sudo apt-get install bzr

This command will install Launchpad bazaar, a VCS (Version Control System), which allows you to download NFC.py.

mkdir pythonprogs 
cd pythonprogs

Above 2 commands create and then switch to the directory where my Python programs will be stored on this machine. You may already have a favourite directory on your machine.

bzr branch lp:nfcpy

This line downloads the latest version of NFC.py.

cd /pythonprogs/nfcpy/examples

Now you've changed to the examples directory of NFC.py.
 
python tagtool.py --device tty:AMA0:pn53x show
 
Run the Python program tagtool. The ‘device’ switch following tagtool.py specifies that we want to use the serial device. If there is no error message, things should be working!

Touch a card on the reader. You should get a response on your screen. Notice that you cannot use a Mifare Classic 1K card, they are incompatible with NFC.py. However, I've had good luck with NTAG203 cards, which are readily available.