RFID reader on a Raspberry Pi
Counting Pumpkins
![© Lead Image © raspberrypi.com © Lead Image © raspberrypi.com](/var/linux_magazin/storage/images/issues/2022/263/rfid-over-spi/rpi_3_b_raspberrypi_com.png/809800-1-eng-US/RPi_3_B_raspberrypi_com.png_medium.png)
© Lead Image © raspberrypi.com
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].
![](/var/linux_magazin/storage/images/issues/2022/263/rfid-over-spi/figure-1/809803-1-eng-US/Figure-1_large.png)
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.
![](/var/linux_magazin/storage/images/issues/2022/263/rfid-over-spi/figure-2/809806-1-eng-US/Figure-2_large.png)
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.
-
SUSE Offers CentOS 7 Support with Liberty Linux Lite
SUSE's Liberty Linux support offering now includes CentOS 7, which means businesses won't be forced to migrate those servers for some time.
-
Ubuntu's App Center Finally Supports Local Installs Again
If you regularly download .deb files and would prefer a GUI method of installing, Ubuntu has your back.
-
AlmaLinux Now Supports Raspberry Pi 5
If you're looking to create with the Raspberry Pi 5 and want to use AlmaLinux as your OS, you're in luck because it's now possible.
-
Kubuntu Focus Releases New Iterations of Ir14 and Ir16 Laptops
If you're a fan of the Kubuntu Focus laptops or have been waiting for the right time to purchase one, that time might be now.
-
NixOS 24.05 Is Ready for Prime Time
The latest release of NixOS (Uakari) has arrived and offers its usual reproducible, declarative, and reliable goodness.
-
Linux Lite 7.0 Officially Released
Based on Ubuntu 24.04 and kernel 6.8, Linux Lite version 7 now offers more options than ever.
-
KaOS Linux 2024.05 Adds Bcachfs Support and More
With updates all around, KaOS Linux now includes support for the bcachefs file system.
-
TUXEDO Computers Unveils New Iteration of the Stellaris Laptop Line
The Stellaris Slim 15 is the 6th generation and includes either an AMD or Intel CPU