Third party cookies may be stored when visiting this site. Please see the cookie information.

Home Babies & Children Gallery Information Free Software Reviews Blog

Home



Welcome to the website of Stewart Watkiss.

The largest section of this is is the reviews and info for days out with babies and children. You will also find my photo galleries and blog.

You can find out a bit more about Stewart Watkiss.
Any comments or suggestions contact me using my email form.

Latest Blog Entry

Simple RFID using Python3 on a Raspberry Pi 2 - RC522 SPI RFID module

RFID (Radio Frequency Identification) and NFC (Near Field Communication) provide the ability for devices to talk to each other by touching them together. This could be between two smart devices or between a smart device to a simple tag embedded into a card or sticker.

In my case my requirements were quite basic as I just wanted to be able to read the unique ID on a RFID tag. I then store all the information on the Raspberry Pi rather than trying to read and write additional information onto the RFID tag.

RFID tag reader connected to Raspberry Pi 2 with display screen

There are a few different devices that are compatible with the Raspberry Pi which vary greatly in cost. As my needs were fairly simple I went for an inexpensive RFID reader/writer based around the RC522 which is available for a few pounds. I bought mine through RyanTeck, it’s also available through other suppliers. This is often advertised as being for the Arduino, but it just uses standard SPI so can be connected directly to the Raspberry Pi SPI connection on the GPIO port.

Wiring up the Raspberry Pi to the RFID module

The RFID module was supplied with header pins, but these had to be soldered on. I used header pins with a 90° bend so that the wires do not protrude upwards. I then used female-to-female jumper leads to connect from the RFID module to the Raspberry Pi GPIO connector.

The diagram below shows the connections between the RFID connections and the GPIO ports on the Raspberry Pi.

Raspberry Pi 2 - RFID RC522 - SPI Python control NFC

The connections are:

RFID Raspberry Pi GPIO phys pin Wire colour
SDA/SS* CE0 24 Green
SCK SCLK 23 Blue
MOSI SPI MOSI 19 Purple
MISO SPI MISO 21 Grey
IRQ N/C N/C N/A
Gnd Gnd 6 Black
RST GPIO 25 22 Brown
3.3V 3.3V 1 Red

* The pin is labelled as SDA which relates to it’s alternative use if using I2C, but is being used as SPI SS (slave select).

The colours shown relate to the multi-way jumper cables I had available, any colour can be used instead.

Setting up the Raspberry Pi

There are a few configuration changes that are needed to allow the Raspberry Pi to communicate with the RFID module. These relate to enabling the SPI drivers and downloading the Python code.

First go into the Raspberry Pi configuration application and from the Interfaces tab enable SPI.
(Also available using sudo raspi-config to configure in a terminal)
Say no to rebooting, as we need to make further changes that also need a reboot.

Edit /boot/config.txt using your favourite editor eg.:
sudo nano /boot/config.txt

Add the following entry to the end of the file:
dtoverlay=spi-bcm2708

Install the SPI-Py library

git clone https://github.com/lthiery/SPI-Py
cd SPI-Py 
sudo python3 setup.py install

Download the pi-rc522 files using

cd ~
git clone https://github.com/ondryaso/pi-rc522.git

I have updated this code through a pull-request to work with Python 3.

If you haven’t rebooted since adding the entry to config.txt then you should now reboot the Raspberry Pi.

Basic code to read the RFID UID

The code below shows the steps required to check for a card and read the UID. The UID is made up of a list of 4 values which are printed on a single line.

#!/usr/bin/python3
import sys
sys.path.insert(0, "/home/pi/pi-rc522/ChipReader")
import RFID
import signal
import time

rdr = RFID.RFID()
util = rdr.util()
util.debug = False

while True:
    #Request tag
    (error, data) = rdr.request()
    if not error:
        print ("\nDetected")

        (error, uid) = rdr.anticoll()
        if not error:
            #Print UID
            print ("Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3]))

            time.sleep(1)

This can be used with rfid cards, key-ring style tags, and NFC stickers.

View more Blog Entries


UK days out, children and holiday information is also available on the Days Out Diary web site
Linux, LPI and the Quiz / Test Program posts are also available on the Penguin Tutor website
First Aid Information, first aid games and first aid practice test / quiz entries are also available on the First Aid Quiz Web site