Open doors with a Raspberry Pi and RFID module


The RFID module uses the SPI interface for communication with the outer world. To activate this on the Rasp Pi, you need the corresponding C library for the BCM2835 component, the Broadcom chip used in the Raspberry Pi 1 (RPi1) models A, B, and B+.

You can download the library with the commands from Listing 2, and set it up under /usr/src/. The RPi2 has been supported since version 1.39 of this library, but you have to enable use of the Device Tree in the Advanced section of raspi-config [5] and have a recent version of Linux. The library has tested successfully on the RPi2 with 2015-02-16-raspbian-wheezy.

Listing 2

Getting the C Library

$ cd /usr/src
$ sudo wget
$ sudo tar xvfz bcm2835-1.42.tar.gz
$ cd bcm2835-1.42/
$ sudo ./configure
$ sudo make
$ sudo make install

Many different programs are available on the Internet for accessing the RFID module. The choice here was to use Rc522 which is easy to install (Listing 3) and configure. It works well in the background and has a simple interface for executing various activities, depending on the card ID.

Listing 3

Getting the RFID Program

$ cd /usr/src
$ sudo apt-get install subversion
$ sudo svn checkout rpi-rc522-read-only
$ cd rpi-rc522-read-only/rc522
$ sudo gcc config.c rfid.c rc522.c main.c -o rc522_reader -lbcm2835
$ sudo cp RC522.conf /etc/
$ sudo /usr/src/rpi-rc522-read-only/rc522/rc522_reader -d

The call in the last line starts the program in debug mode where, among other things, it shows the IDs for the RFID transponder (Listing 4). You should then enter one of these IDs in the /etc/RC522.conf file and link it to an action (Listing 5). The program will execute the action as soon as the transponder comes into the vicinity of the card reader. The small script from Listing 6 opens the door, waits 10 seconds, and closes it again.

Listing 4

Output of RFID IDs

$ cd /usr/src/rpi-rc522-read-only/rc522
$ sudo ./rc522_reader -d
Debug mode.
Type: 0400, Serial: b97b9a1a
New tag: type=0400 SNlen=4 SN=[b97b9a1a ]
Type: 0400, Serial: 039962c7
New tag: type=0400 SNlen=4 SN=[039962c7]
Type: 0400, Serial: da9143b2
New tag: type=0400 SNlen=4 SN=[da9143b2]
Type: 0400, Serial: 503649b5
New tag: type=0400 SNlen=4 SN=[503649b5]

Listing 5

Adding and Linking an RFID ID

# /etc/RC522.conf
# SPI_SPEED - spi clock rate in kHz. Must be >=4 and <=125000
#User id and group id (no user root allowed)
# Path for store memory dump of UltraLight tags.
  File will be named as serial number.
#GPIO pin (bsm2835 gpio number!) for indiction of active tasks.
[b97b9a1a] sh /home/pi/

Listing 6

Waiting on the RFID

# /home/pi/
echo 0=60 > /dev/servoblaster
sleep 10
echo 0=170 > /dev/servoblaster


This example of a cat door illustrates a complete access control using RFID and establishes once more the versatility of the Raspberry Pi GPIO. You can make several improvements in what is essentially a trial run project. For instance, the door only functions in one direction. Also, a cat will have to be amenable to letting the transponder get within 3cm of the card reader. Finally, you should build in a photoelectric barrier so that the door will not close when the cat is still in the door frame.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content