Want to build this project?
View Parts

 

My entry to the "Double the Memory Challenge":  Can be seen in full here: http://hubcitylabs.org/nfc-on-raspberrypi-with-pn532-py532lib-and-i2c/ 

 

See how to enable I2C on your RaspberryPI here: http://www.danyo.ca/enabling-i2c-on-your-raspberrypi/

 

Over the past few weeks, we at HubCityLabs have been busy! We've started work on a project will let us control access to our facilities with NFC cards, and later on, bio-metrics. Since for each access point, there will be multiple readers (NFC, fingerprint, retina scanners...) for each host (a RaspberryPi), we settled on using I2C as a standard for communication between the host and the peripherals. Our chosen language was Python, since it seems to be the de-facto language on the RaspberryPi.

py532lib

Out of the requirements above was born the need for a PN532/i2c package in Python3, so I created a project called py532lib. I chose to use Quick2Wire's Python API for Linux as it provides clean access to I2C, SPI, and for GPIO (on RaspberryPI). Currently, only I2C is supported in py532lib, however, in the long run we should be able to add SPI and UART communication capabilities to the PN532 under a single, seamless API.   Please be gentle, as this is my first ever attempt at coding anything in Python! I've done my best to make it easy to use, and respect as many PEP8 rules as possible. Forks and pull-requests will be welcomed with open arms.  

Wiring

First you'll need to follow the instructions from my earlier post, about enabling I2C for your RaspberryPi.   Second, you'll need to make sure that the jumpers on the Adafruit NFC Breakout Board are set for I2C communications.  
PN532 Wiring I2C RPi
The wiring schematic above is easy as pie. From the RaspberryPi, simply connect 3.3V and GND to the Breakout Board on JP4, then connect the SDA and SCL signals. As noted in NXP's PN532 Application Note, 3.3k pull-up resistors should be used. If everything is connected properly, you should be able to see the PN532 on the I2C bus at the RaspberryPI's shell.
sudo i2cdetect -y 1
             0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- 24 -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Note: If you have an older RaspberryPI, you will need to do "sudo i2cdetect -y 0" instead.

Software

Now that py532lib are installed and available to your Python's PATH, you should be able to write the following:

from py532lib.i2c import *
from py532lib.frame import *
from py532lib.constants import *
 
pn532 = Pn532_i2c()
pn532.SAMconfigure()
 
card_data = pn532.read_mifare().get_data()
 
print(card_data)


Now that py532lib is in polling mode, put an NFC card in the Breakout Board's field, and it will return the card's data.   Hopefully someone will find this useful! Cheers!

 

Related Links

 

 

element14
Product Name*DescriptionBuy All PartsBuy All Parts
Raspberry Pi Model B with SD CardRaspberry Pi Model B with SD CardRaspberry Pi model B and 8GB SD Card with NOOBS preinstalled
Optical MouseOptical MouseBasic USB optical mouse
Raspberry Pi Power SupplyRaspberry Pi Power Supply120-240v to 5V power supply with micro USB connector
KeyboardKeyboardBasic USB keyboard
Adafruit Pi CobblerAdafruit Pi CobblerGPIO breakout kit for Raspberry Pi
BreadboardBreadboardBasic breadboard with 830 connection points
Jumper wire bundleJumper wire bundleJumper wires for use on breadboard
(2) 3.3k resistors(2) 3.3k resistors3.3K ohm, 250mW carbon film resistors
PN532 breakout board PN532 breakout board PN532 NFC/RFID controller breakout board

 

*Products and resources listed are listed to help members build their own Pi Projects. They are suggestions and listed for educational purposes. For substitutions of any parts, please post a question asking the original author.