RFID reader on a Raspberry Pi

Counting Pumpkins

© Lead Image © raspberrypi.com

© Lead Image © raspberrypi.com

Article from Issue 263/2022
Author(s):

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].

Figure 1: The circuit diagram for the test setup. You can see the display and RFID reader connections on the right.

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.

Figure 2: The complete test setup with a horde of pumpkins, the reader at front left, and the display at back left.

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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Chaos Communication Congress

    The 23rd annual Chaos Computer Club Congress offered a fresh perspective on topics such as RFID technology and cyber-crime laws.

  • Digital Spirit Level

    The small MPU6050 sensor contains a gyroscope and an accelerometer, which means that you can build a digital spirit level with it.

  • Countdown Counter

    Build a countdown counter with a Raspberry Pi and some electronics, and you can count down the time to any event.

  • Community Notes

    LUGS debate tax time, free BBC content online, hosting Linux swap on LVM, and more.

  • CircuitPython

    The CircuitPython run-time environment runs on almost all microcomputers and microcontrollers, making it perfect for cross-platform programming.

comments powered by Disqus
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

News