Tag Archives: pi4j

ITEAD PN532 NFC Module and RaspberryPi via I2C and Java

In my last post I explained how I got the ITEAD PN532 NFC Module up and running with a RaspberryPi by using plain Java code with pi4j and SPI as an interface for communication.

However, for a different idea, I needed the SPI interface to be available completely, and that meant that I must somehow change the implementation for the NFC module. One option is to use I2C as an interface and to adapt the hardware and the software to use it instead.

The ITEAD PN532 NFC Module, has only the SPI pins connected at the RaspberryPi connector. I believe this is some compromise that the guys at ITEAD must have did. However, the PN532 supports SPI, I2C and Serial, and all these interfaces can be accessed through the other connector, having in mind that they share the same pins.

In order to achieve I2C communication with a RaspberryPi, the following steps need to be made:

1. Change the switches on the NFC module to indicate that I2C will be used: SET0 set to H and SET1 set to L.

2. The I2C pins need to be rewired manually to the I2C pins of the RaspberryPi connector. My gruesome workaround is this:

itead back rewire

3. The SPI pins of the RaspberryPi connector are still conected to the same shared pins, and with the rewiring, the I2C and SPI are interconnected and they won’t be functioning properly. To avoid this, the SPI pins need to be removed. Again, my workaround by cutting the 5 pins from the connector:

itead front rewire

As of now, the device is ready to work with I2C. In order to test it, you can use the libnfc as described in this ITEAD studio blogpost. (see from number 8 onward).

The final step was to make all this functioning with Java code. For that, I extended my library by porting the elechouse PN532 implementation for I2C to Java with pi4j. After some struggle with the proper API usage, the implementation got quite clean. It is already added in the same project at GitHub.

As of this point, since I got everything working, my idea is to resume with this projec, port the whole PN532 implementation to Java, and making the API more clear to use. That I will cover in a future blog post.

NFC with RaspberryPi and Java

For a presentation I did, I needed to come up with a neat example of directly connecting a RaspberryPi with some add-on device. The ITEAD PN532 NFC Module looked perfect for this case, and I started working with it. My end goal was to operate it with Java code.

RaspberryPi with ITEAD PN532 NFC Module
RaspberryPi with ITEAD PN532 NFC Module

The ITEAD blog has a nice example plus a library done in C for interfacing the NFC Module.  It uses the SPI interface and operates on a low level by using the WiringPi library.

The pi4j library is a JNI to the WiringPi C library, so in basic terms it should be able to do the same thing.

I started by porting an Arduino code first (github link) and using the Serial interface for communication, but I failed. Then I started porting the ITEAD provided library and used the SPI interface as in the original code. After some struggle, I managed to devise some working code: https://github.com/hsilomedus/raspi-pn532

Notes:

  • there are two interface implementations: PN532Spi and PN532Serial. The second one is marked as @Deprecated because it’s not functioning for some reasons
  • the SPI implementation at the moment can only get the firmware version and read the passive target id of an NFC tag. Hopefully I’ll implement the other functions soon.
  • before using the SPI interface, make sure that it is enabled. See the notes in the ITEAD blogpost and in the github repo readme.
  • when starting the example, execute java with sudo in order pi4j to work properly.