The PN532 can connect to the Pi in a number of ways, but for this project a SPI connection is used for communication. To use the PN532 with a SPI connection you'll need to install the Adafruit PN532 Python library.
If you're curious another way to connect the PN532 to a Pi is with a serial connection and to use libnfc like this tutorial shows. However using the serial connection on the Pi can be a little problematic because the kernel normally uses the Pi's serial port for outputting debug information on boot. The PN532 also supports the I2C protocol, but it can be a little tricky to use with the Pi since it uses clock stretching. To avoid all these issues this project and library uses a SPI connection to the Pi.
Also if you're new to using the Raspberry Pi be sure to first read guides on setting up the Raspberry Pi and connecting to its terminal to run commands.
Once you're ready to install the library make sure the Raspberry Pi is connected to the internet (with a wired or wireless network connection) and is running the latest Raspbian operating system. Then follow the steps below to install the library.
First boot up the Pi and connect to its terminal with either a keyboard and monitor, or through the network with SSH. Then run the following commands:
sudo apt-get update sudo apt-get install build-essential python-dev git cd ~ git clone https://github.com/adafruit/Adafruit_Python_PN532.git cd Adafruit_Python_PN532 sudo python setup.py install
You can ignore warnings about packages that are already installed. If everything was installed successfully you should see something like the following at the terminal:
If the installation stops with an error message, go back and carefully check that all the commands above were executed and try again.
Read MiFare Card Example
Once the library has been installed you can run an example to test detecting and reading a MiFare classic card. To run the example navigate to the examples subdirectory and run the readmifare.py script by executing:
cd examples sudo python readmifare.py
You should see the example start and wait for a MiFare card to be swiped, like below:
If you see an error message, carefully check the wiring between the PN532 is exactly as shown in the previous page and try again.
Now place a MiFare card above the PN532 antenna and notice information about the card is printed to the screen:
Almost like magic the PN532 wirelessly reads information from the MiFare card! Notice the UID (unique identifier) for the card is printed, and a few bytes of data from block 4 of the card are also displayed (the actual data you see might be different for your card).
When you remove the card from the PN532 the information about the card will stop being displayed. You can press Ctrl-C to quit the readmifare.py example at any time.
That's all there is to installing and using the PN532 library! Currently the library is somewhat limited in functionality and only exposes detecting, authenticating, reading, and writing MiFare classic cards. To learn how to use the library open the readmifare.py example in a text editor and examine the comments. The interface to the library is very similar to the PN532 Arduino library.
If you have more advanced NFC needs be sure to check out the libnfc library and using it with the Raspberry Pi.
Continue on to learn how to control Minecraft with NFC tags!
Text editor powered by tinymce.