RFID reader on a Raspberry Pi
Counting Pumpkins
Inexpensive components for the SPI interface let you upgrade a Raspberry Pi 4 to a display system for zero-contact RFID-based data acquisition.
Radio-frequency identification (RFID) tags have become indispensable in industry and government, as well as the wholesale and retail spaces. The inexpensive transponder chips can be found on clothing labels, identification cards, and credit cards. Armed with just a Raspberry Pi and an RFID kit, you can read the data from these chips and view it on a display.
In this project, I read serial numbers from RFID tags stuck on 3D-printed pumpkins – a slightly different kind of detection task. To do this, I connect an RC522 [1] RFID kit and a 1.8-inch ST7735 serial peripheral interface (SPI) thin-film transistor (TFT) display [2] to a Raspberry Pi 4. Together, the two modules can cost less than $15 (EUR15, £14) in online stores. The pumpkins contain simple RFID tags [3], also available for very little cash. Although at first glance the project seems clear-cut and sounds as if it should work right away, check out the "Mishaps, Misfortunes, and Breakdowns" box to find out what can go wrong.
Mishaps, Misfortunes, and Breakdowns
After some brief research, I decided which libraries I was going to use to control the modules. After a day of pondering and programming, it turned out that the library for the RFID reader had not been maintained for several years. The only way to get it to work in Python 3 was to make several code changes. In open source projects, you should always take a look at the date of the last commit, and the issues, which will inform you as to whether the project is still under development by the community.
I have stumbled across many pieces of orphaned software on the web recently. At first sight, they might still seem active, but in fact, they have already been gathering dust for years. For example, the very popular wiringPi tool has not seen any support for a long time (see the Pigpio article in this issue) and has been removed from the Raspberry Pi OS package sources. However, the Internet never forgets, which means users are continually tripped up over outdated manuals and how-to articles.
Unfortunately, it looks like some low-level Python libraries have been hastily hot-wired without extensive testing, which results in incompatibilities. Therefore, I had to separate the reader and display programs in this project.
The circuit diagram in Figure 1 shows how the modules connect to the Raspberry Pi, along with two pushbuttons and LEDs for testing purposes. The KiCad layout of the project is included in the download section of this article [4].
Display and RFID Reader
Each of the modules is connected to its own SPI interface. Figure 2 shows the project after the build; the RFID tags are hidden in the pumpkins. Please note that only the Raspberry Pi 4 has multiple on-board SPI interfaces.
The Raspberry Pi 4 has a total of seven SPI interfaces, all of which can be accessed from the 40-pin GPIO header. The previous models came with just three SPI interfaces, of which only two were routed to the header. Table 1 shows which pins correspond to the individual interfaces on the header by default.
Table 1
Default SPI Pins
SPI | Header | Pin | SPI | Header | Pin |
---|---|---|---|---|---|
SPI0 |
MOSI |
19 |
SPI4 |
MOSI |
31 |
MISO |
21 |
MISO |
29 |
||
SCLK |
23 |
SCLK |
26 |
||
CE0 |
24 |
CE0 |
7 |
||
CE1 |
26 |
CE1 |
22 |
||
SPI1 |
MOSI |
38 |
SPI5 |
MOSI |
8 |
MISO |
35 |
MISO |
33 |
||
SCLK |
40 |
SCLK |
10 |
||
CE0 |
12 |
CE0 |
32 |
||
CE1 |
11 |
CE1 |
37 |
||
CE2 |
36 |
||||
SPI3 |
MOSI |
3 |
SPI6 |
MOSI |
38 |
MISO |
28 |
MISO |
35 |
||
SCLK |
5 |
SCLK |
40 |
||
CE0 |
27 |
CE0 |
12 |
||
CE1 |
18 |
CE1 |
13 |
Reading from the SPI Interface
Notice in Table 1 that some ports overlap and that interface SPI2 is not available on the header in the default configuration. However, the SPI interface configuration can be adjusted with the dtoverlay
command, which also lets you output a list of all predefined SPI devices (Listing 1).
Listing 1
SPI Interfaces
$ dtoverlay --all | grep spi.- spi0-1cs spi0-2cs spi1-1cs spi1-2cs spi1-3cs spi2-1cs spi2-2cs spi2-3cs spi3-1cs spi3-2cs spi4-1cs spi4-2cs spi5-1cs spi5-2cs spi6-1cs spi6-2cs
The individual interfaces have several configurations, each with a different number of chip select (CS) lines. Therefore, you can configure the interfaces very precisely to suit your project requirements. To get the currently loaded overlays, use the -l
option. To view the concrete definition of an SPI device, along with the default pins on the header, use the -h
option (Listing 2).
Listing 2
dtoverlay Output
$ dtoverlay -h spi1-3cs Name: spi1-3cs Info: Enables spi1 with three chip select (CS) lines and associated spidev dev nodes. The gpio pin numbers for the CS lines and spidev device node creation are configurable. N.B.: spi1 is only accessible on devices with a 40pin header, eg: A+, B+, Zero and PI2 B; as well as the Compute Module. Usage: dtoverlay=spi1-3cs,<param>=<val> Params: cs0_pin GPIO pin for CS0 (default 18 - BCM SPI1_CE0). cs1_pin GPIO pin for CS1 (default 17 - BCM SPI1_CE1). cs2_pin GPIO pin for CS2 (default 16 - BCM SPI1_CE2). cs0_spidev Set to 'disabled' to stop the creation of a userspace device node /dev/spidev1.0 (default is 'okay' or enabled). cs1_spidev Set to 'disabled' to stop the creation of a userspace device node /dev/spidev1.1 (default is 'okay' or enabled). cs2_spidev Set to 'disabled' to stop the creation of a userspace device node /dev/spidev1.2 (default is 'okay' or enabled).
To output a list of available SPI devices, run:
ls /dev/spi*
Make sure you enable SPI support up front with a dtparam=spi=on
line in the /boot/config.txt
file. Alternatively, you can use the Raspberry Pi OS configuration tool and the 3 Interface Options | I4 SPI option.
If you want to enable certain overlays directly at boot time, you could also add them to the /boot/config.txt
file (e.g., dtoverlay=spi1-2cs
). In principle, you could do a software rewire of the individual lines of the SPI interfaces with dtoverlay
, but that would be a bit over the top at this point.
Prepping the Pi
The operating system for the Raspberry Pi is an up-to-date 32-bit Raspberry Pi OS Lite. Listing 3 shows the commands for updating the system and installing the required programs and libraries from the package sources. The libraries for integrating the display and reader come from the pip
Python Package Index (PyPI).
Listing 3
Preparations
### Update Raspberry Pi $ sudo apt update $ sudo apt upgrade ### Install Pi OS packages $ sudo apt install python3-pip libopenjp2-7-dev libatlas-base-dev ttf-ubuntu-font-family ### SPI and graphics libraries $ sudo python3 -m pip install RPi.GPIO spidev Pillow numpy ### Library for the display $ sudo python3 -m pip install st7735 ### Library for the RFID reader $ sudo python3 -m pip install pi-rc522
When you are done, do not forget to enable SPI support for the SPI0 and SPI1 interfaces by opening the /boot/config.txt
file in your favorite text editor and adding two lines,
dtparam=spi=on dtoverlay=spi1-1cs
to the end of the file. You will need to restart the Raspberry Pi for the changes to take effect.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.