Leon Anavi
IT Tips && Tricks

Mobile & Embedded

Created: 21.10.2016 01:29 Last Modified: 21.10.2016 01:33 Views: 18617
Keywords: Linux, NFC, PN532, Python, Raspberry Pi, RFID

Writing and Reading Data to NFC Cards and RFID Tags with Raspberry Pi and PN532 Module

Introduction

This is a simple demo for writing and reading data to RFID tags on NFC cards using Raspberry Pi and PN532 NFC module. Only free and open source software has been used for examples in this video.

There are a lot of different RDIF and NFC modules on the market. I bought this particular unit from ebay. You can find it and other similar modules with the same PN532 chip for about 15 USD. I bought it as a convenient kit with RFID tag and NFC card for testing. Adafruit is offering a similar module.

PN532 operates at 13.56 MHz resonance frequency. The main advantage of the module is that it supports 3 hardware interfaces for connecting to other devices: high speed UART, I2C and SPI. Therefore it is very convenient for rapid prototyping on Raspberry Pi or Arduino.

Required Hardware

For this tutorial you will need:

  • Raspberry Pi (any model and version is suitable)
  • PN532 NFC Module
  • Breadboard
  • Female to male and male to male jumper wires
  • microSD card with Raspbian GNU/Linux distribution
  • NFC Card and RFID token (tag) for testing

Wiring

Connect PN532 NFC module to your Raspberry Pi as shown on the diagram below:

Wiring PN532 NFC and RFID module to Raspberry Pi

Please note that I have already soldered male header pins to the NFC module as shown in the video. It is powered by 3.3V from Raspberry Pi. Although the module support 3 hardware interfaces this tutorial covers only the usage through SPI. So after the wiring switch the module to SPI and after that boot Raspberry Pi with the Raspbian GNU/Linux distribution.

Source Code

Open a terminal on your Raspberry Pi or login remotely using SSH. If Python and Git are not install type the commands below to install it:

sudo apt-get update
sudo apt-get install build-essential python-dev git

Install Adafruit Python library for PN532:

cd ~
git clone https://github.com/adafruit/Adafruit_Python_PN532.git
cd Adafruit_Python_PN532
sudo python setup.py install

Type the following commands to get the source code of the example for writing and reading data to the NFC cards and RFID tags:

cd ~
git clone https://github.com/leon-anavi/rpi-examples.git
cd rpi-examples/PN532/python/

Execute the following commands and follow the instructions to write data contains user ID to NFC card on RFID token:

sudo python rfid-save.py

Execute the following commands to start reading data from NFC cards and RFID tags:

sudo python rfid-scan.py

Press Ctrl + C to terminate the execution of the Python script.



  Home | About | Contact | Disclaimer | Sitemap 2009-2016 Leon Anavi. All rights reserved.