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.

5 comments:

  1. Great work! I followed your guide to setup the ITEAD NFC reader with Raspberry Pi 2 and it works flawlessly. Currently I am working on a project where a NFC tag would be used to trigger a relay and switch on. And when the tag is removed, the relay would turn off. Is it possible to include this in the present code? I am new to coding in python mostly because I am a mechanical engineer. If you can help me in any way, it would really be helpful. I can share with you further details.
    This is something to close to what I want to do: http://www.instructables.com/id/RFID-Sound-Triggers-with-the-Raspberry-Pi/?ALLSTEPS

    Thanks in advance

    ReplyDelete
  2. Vinayak,

    Here is something that might get you on the right track:

    Install the GPIOZero library which allows control over the GPIO pins

    If using Python 3:
    sudo apt-get install python3-gpiozero

    If using Python 2:
    sudo apt-get install python-gpiozero

    Then in your program:


    from gpiozero import LED
    from time import sleep

    led = LED(15)
    while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

    This is just an example which will blink an led, but simply substitute a relay for the led and the result will be the same.

    You can use a relay such as this:

    http://www.ebay.ca/itm/New-2-Channel-5V-Relay-Module-Opto-couple-For-Arduino-AVR-Raspberry-Pi-ARM-PIC-/270935013107?hash=item3f14fc36f3:g:WNUAAOSweW5VZEOo

    ReplyDelete
  3. I got this message, when I followed your steps:

    [nfc.clf] searching for reader on path tty:AMA0:pn532
    [main] no contactless reader available

    Any ideas?
    Thanks!

    ReplyDelete
  4. Aspire-writer:

    If you are on the Raspberry Pi 3, did you do the following:

    "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

    and reboot?

    ReplyDelete
  5. So I was on the Raspberry Pi 2 - but it turns out it was actually a faulty board. Got the new one, and your instructions worked great! Such a relief after hours of tinkering with other instructions online. Thanks!

    ReplyDelete