aj112358
Posts: 3
Joined: Thu Feb 13, 2020 6:10 am

Problems with RFID MFRC522 on Rpi4

Fri Feb 14, 2020 3:55 am

Hello,

I need some major help with setting up and MFRC522 RFID reader with my Raspberry Pi 4 (Rpi4). I have been trying for many days to get it to work correctly and have been unsuccessful. I would describe myself as a beginner working with the Rpi4. I apologize in advance if the formatting of my post is not up to standard. Please feel free to give advice as to how I can organize this post better.

I am currently (desperately!) trying to execute the following code. It is found here: https://github.com/ondryaso/pi-rc522/bl ... es/Read.py. Here is the code as I personally have copied it:

Code: Select all

#!/usr/bin/env python

import signal
import time
import sys

from pirc522 import RFID

run = True
rdr = RFID()
util = rdr.util()
util.debug = True

def end_read(signal, frame):
	global run
	print("\nCtrl+C captured, ending read.")
	run = False
	rdr.cleanup()
	sys.exit()
	
signal.signal(signal.SIGINT, end_read)

print("Starting...")

while run:
	rdr.wait_for_tag()
	
	(error, data) = rdr.request()
	if not error:
		print("\nDetected: " + format(data, "02x"))
		
	(error, uid) = rdr.anticoll()
	if not error:
		print("Card read UID: " + str(uid[0]) + "," + str(uid[1]) + "," + str(uid[2]) + "," + str(uid[3]))
		
		print("Setting tag...")
		util.set_tag(uid)
		
		print("\nAuthorizing...")
		#util.auth(rdr.auth_a, [0x12, 0x34, 0x56, 0x78, 0x96, 0x92])
		util.auth(rdr.auth_b, [0x74, 0x00, 0x52, 0x35, 0x00, 0xFF])

		print("\nReading...")
		util.read_out(4)
		
		print("\nDe-authorizing...")
		util.deauth()
		
		time.sleep(1)



Upon executing this code, I receive the following error in the terminal window:

Code: Select all

Traceback (most recent call last):
  File "Final_Boss.py", line 10, in <module>
    rdr = RFID()
  File "/usr/local/lib/python3.7/dist-packages/pirc522/rfid.py", line 47, in __init__
    self.spi.open(bus, device)
FileNotFoundError: [Errno 2] No such file or directory


------------------
(program exited with code: 1)
Press return to continue

To try and give you some more information, here are the outputs of some terminal commands:

pi@raspberrypi:/ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

pi@raspberrypi:/ $ lsmod | grep spi
spi_bcm2835 20480 0

pi@raspberrypi:/ $ dmesg | grep spi
[ 7.780193] ads7846 spi0.1: spi0.1 supply vcc not found, using dummy regulator
[ 7.780296] ads7846 spi0.1: Linked as a consumer to regulator.0
[ 7.787201] ads7846 spi0.1: touchscreen, irq 57
[ 7.787700] input: ADS7846 Touchscreen as /devices/platform/soc/fe204000.spi/spi_master/spi0/spi0.1/input/input8
[ 8.375316] graphics fb1: fb_ili9486 frame buffer, 480x320, 300 KiB video memory, 32 KiB buffer memory, fps=33, spi0.0 at 16 MHz

pi@raspberrypi:/ $ sudo nano /boot/config.txt
# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
dtparam=spi=on

pi@raspberrypi:/ $ lsmod

Code: Select all

Module                  Size  Used by
rfcomm                 49152  4
bnep                   20480  2
hci_uart               40960  1
btbcm                  16384  1 hci_uart
serdev                 20480  1 hci_uart
bluetooth             389120  29 hci_uart,bnep,btbcm,rfcomm
ecdh_generic           28672  1 bluetooth
fuse                  110592  3
8021q                  32768  0
garp                   16384  1 8021q
stp                    16384  1 garp
llc                    16384  2 garp,stp
ads7846                24576  0
fb_ili9486             16384  1
fbtft                  45056  1 fb_ili9486
syscopyarea            16384  1 fbtft
sysfillrect            16384  1 fbtft
sysimgblt              16384  1 fbtft
fb_sys_fops            16384  1 fbtft
joydev                 20480  0
evdev                  24576  18
brcmfmac              311296  0
brcmutil               16384  1 brcmfmac
sha256_generic         20480  0
cfg80211              614400  1 brcmfmac
rfkill                 28672  6 bluetooth,cfg80211
snd_bcm2835            24576  2
bcm2835_codec          36864  0
snd_pcm               102400  1 snd_bcm2835
raspberrypi_hwmon      16384  0
i2c_bcm2835            16384  0
hwmon                  16384  2 raspberrypi_hwmon,ads7846
v4l2_mem2mem           24576  1 bcm2835_codec
bcm2835_v4l2           45056  0
snd_timer              32768  1 snd_pcm
bcm2835_mmal_vchiq     32768  2 bcm2835_codec,bcm2835_v4l2
v4l2_common            16384  1 bcm2835_v4l2
snd                    73728  7 snd_timer,snd_bcm2835,snd_pcm
videobuf2_dma_contig    20480  1 bcm2835_codec
videobuf2_vmalloc      16384  1 bcm2835_v4l2
videobuf2_memops       16384  2 videobuf2_dma_contig,videobuf2_vmalloc
videobuf2_v4l2         24576  3 bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem
videobuf2_common       45056  4 bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
videodev              200704  6 bcm2835_codec,v4l2_common,videobuf2_common,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
spi_bcm2835            20480  0
media                  36864  2 videodev,v4l2_mem2mem
argon_mem              16384  0
vc_sm_cma              36864  1 bcm2835_mmal_vchiq
uio_pdrv_genirq        16384  0
uio                    20480  1 uio_pdrv_genirq
fixed                  16384  0
i2c_dev                20480  0
ip_tables              24576  0
x_tables               32768  1 ip_tables
ipv6                  450560  24

pi@raspberrypi:~ $ pip freeze

Code: Select all

appdirs==1.4.3
asn1crypto==0.24.0
astroid==2.1.0
asttokens==1.1.13
automationhat==0.1.0
beautifulsoup4==4.7.1
blinker==1.4
blinkt==0.1.2
buttonshim==0.0.2
Cap1xxx==0.1.3
certifi==2018.8.24
chardet==3.0.4
Click==7.0
colorama==0.3.7
colorzero==1.1
cookies==2.2.1
cryptography==2.6.1
cycler==0.10.0
Cython==0.29.10
decorator==4.3.0
docutils==0.14
drumhat==0.1.0
entrypoints==0.3
envirophat==1.0.0
ExplorerHAT==0.4.2
Flask==1.0.2
fourletterphat==0.1.0
gpiozero==1.5.1
guizero==0.6.0
html5lib==1.0.1
idna==2.6
ipykernel==4.9.0
ipython==5.8.0
ipython-genutils==0.2.0
isort==4.3.4
itsdangerous==0.24
jedi==0.13.2
Jinja2==2.10
jupyter-client==5.2.3
jupyter-core==4.4.0
keyring==17.1.1
keyrings.alt==3.1.1
Kivy==1.11.1
Kivy-Garden==0.1.4
kiwisolver==1.0.1
lazy-object-proxy==1.3.1
logilab-common==1.4.2
lxml==4.3.2
MarkupSafe==1.1.0
matplotlib==3.0.2
mccabe==0.6.1
microdotphat==0.2.1
mote==0.0.4
motephat==0.0.2
mypy==0.670
mypy-extensions==0.4.1
nudatus==0.0.4
numpy==1.16.2
oauthlib==2.1.0
olefile==0.46
pantilthat==0.0.7
parso==0.3.1
pexpect==4.6.0
pgzero==1.2
phatbeat==0.1.1
pi-rc522==2.2.1
pianohat==0.1.0
picamera==1.13
pickleshare==0.7.5
picraft==1.0
piglow==1.2.4
pigpio==1.42
Pillow==7.0.0
prompt-toolkit==1.0.15
psutil==5.5.1
pycairo==1.16.2
pycodestyle==2.4.0
pycrypto==2.6.1
pyflakes==2.0.0
pygame==1.9.4.post1
Pygments==2.3.1
PyGObject==3.30.4
pyinotify==0.9.6
PyJWT==1.7.0
pylint==2.2.2
pyOpenSSL==19.0.0
pyparsing==2.2.0
pyperclip==1.6.4
pyserial==3.4
python-apt==1.8.4
python-dateutil==2.7.3
pyxdg==0.25
pyzmq==17.1.2
qtconsole==4.3.1
rainbowhat==0.1.0
requests==2.21.0
requests-oauthlib==1.0.0
responses==0.9.0
roman==2.0.0
RPi.GPIO==0.6.5
RTIMULib==7.2.1
scrollphat==0.0.7
scrollphathd==1.2.1
SecretStorage==2.3.1
semver==2.0.1
sense-emu==1.1
sense-hat==2.2.0
simplegeneric==0.8.1
simplejson==3.16.0
six==1.12.0
skywriter==0.0.7
sn3218==1.2.7
soupsieve==1.8
SPI-Py==1.0
spidev==3.4
ssh-import-id==5.7
thonny==3.1.0
thonny-pi==1.1
tornado==5.1.1
touchphat==0.0.1
traitlets==4.3.2
twython==3.7.0
typed-ast==1.3.1
uflash==1.2.4
unicornhathd==0.0.4
urllib3==1.24.1
wcwidth==0.1.7
webencodings==0.5.1
Werkzeug==0.14.1
wrapt==1.10.11

I am pretty sure that I have correctly enabled the SPI interface on the Rpi4, and I am also pretty sure that the physical wire connections from the RFID to the Rpi4 are correct (meaning, they are correctly made as instructed on the GitHub website above). And I am completely sure that the RFID itself is functioning correctly, as I am able to use it to turn on/off an LED when I connect it to my Arduino Uno. I am also pretty sure that I have correctly downloaded/installed all the relevant libraries. If you would like me to double-check so you can verify, please tell me how and I will do it.

If someone could help me I would be extremely grateful, as I have been trying for days to get this to work, and I am now completely lost as to why I cannot.

Thank you for your help.

EDIT: Changes made to this post to decrease it's physical length (put terminal outputs in "code" format, instead of original "quote" format).
Last edited by aj112358 on Sun Feb 16, 2020 5:37 am, edited 1 time in total.

User avatar
topguy
Posts: 6466
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Problems with RFID MFRC522 on Rpi4

Fri Feb 14, 2020 1:14 pm

Code: Select all

[ 7.787201] ads7846 spi0.1: touchscreen, irq 57
[ 7.787700] input: ADS7846 Touchscreen as /devices/platform/soc/fe204000.spi/spi_master/spi0/spi0.1/input/input8
[ 8.375316] graphics fb1: fb_ili9486 frame buffer, 480x320, 300 KiB video memory, 32 KiB buffer memory, fps=33, spi0.0 at 16 MHz
Do you also have a LCD screen connected ?

aj112358
Posts: 3
Joined: Thu Feb 13, 2020 6:10 am

Re: Problems with RFID MFRC522 on Rpi4

Fri Feb 14, 2020 5:36 pm

Hello,

Thank you for your reply. I do not currently have the LCD connected.

But, I did previously purchase a 3.5-inch LCD touchscreen display, downloaded and installed the driver, and connected it to my Rpi4. It took me some time but I believe I did this correctly. To do all this, I followed the instructions from this link: http://www.lcdwiki.com/3.5inch_RPi_Display.

Right now, I do not have the LCD connected, and was only trying to connect and use the RFID. Do you think this is what was causing my problems - my Rpi4 is expecting the LCD when I infact have the RFID wired to it? Is there a way to connect both the LCD and RFID together? If not, then will I have to always change my Rpi4 settings somehow whenever I wish to switch between using the LCD and RFID?

Thank you for your help.

EDIT: I would like to add that I am currently using an old desktop monitor (non-touchscreen) with my Rpi4, and it is connected to the Rpi4 via the microHDMI port.

pwinwood
Posts: 78
Joined: Mon Jul 02, 2012 2:21 am
Location: Oxford, England

Re: Problems with RFID MFRC522 on Rpi4

Fri Feb 14, 2020 6:14 pm

If your LCD was interfaced via the GPIO pins, the LCD driver could have claimed the SPI 0 interface and the associated chip enable pins. The RC522 reader also uses SPI 0. As the LCD driver has claimed it the RFID reader will not work.
This happened to me in a work context.

I used SPI 1 instead for the RFID reader. I had to rewrite my RFID driver (written in C++) to enable the second SPI 1.
I added

Code: Select all

dtoverlay=spi1-3cs


to /boot/config.txt to enable SPI 1.

The SPI1 interface uses some of the highest numbered GPIO pins.
Pin 38 SPI1 MOSI
Pin 35 SPI1 MISO
Pin 40 SPI1 SCLK
The chip enable pin is programmable
If I remember correctly something like

Code: Select all

dtoverlay=spi1-3cs,cs1_pin=6
Moves the CE1 to Pin 31 (GPIO6).

Or something like that. YMMV.

aj112358
Posts: 3
Joined: Thu Feb 13, 2020 6:10 am

Re: Problems with RFID MFRC522 on Rpi4

Sun Feb 16, 2020 3:47 am

Hello pwinwood,

Thanks for your reply.

I understand what you're saying. I didn't realize that installing the LCD driver would cause it to forever "claim" the CE0 pin on the Rpi4 board. I actually installed the LCD back in December 2019, and only now started working with the RFID reader.

So when you say "I had to rewrite my RFID driver...", I am unable to locate which file/folder the driver is actually located, so I'm unsure as to the specific changes I can make.

But I did try your other recommendations. I made the corresponding changes to the physical wiring, then I made your recommended changes to the "/boot/config.txt" file, as you can see below. I attached the entire file, but my change is at the very bottom :

Code: Select all

# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
dtparam=spi=on

# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
#dtoverlay=vc4-fkms-v3d
#max_framebuffers=2

[all]
#dtoverlay=vc4-fkms-v3d

[b]##### THIS IS WHAT I ADDED #####[/b]
dtoverlay = spi1-3cs, cs1_pin = 6

# NOOBS Auto-generated Settings:
hdmi_force_hotplug=1
dtparam=i2c_arm=on
dtparam=spi=on
enable_uart=1
dtoverlay=tft35a:rotate=270

Unfortunately, running my Python code gave the same error as in my first post. I then tried to change the RFID library file I am using (called "rfid.py", which can be found here: https://github.com/ondryaso/pi-rc522/tr ... er/pirc522) to make it so that the RFID reads specifically from the CE1 pin. Here is a snippet of the part of the code I changed:

Code: Select all

import threading
import spidev
import RPi.GPIO as GPIO

class RFID(object):
    pin_rst = 22
    [b]pin_ce = 1[/b]
    pin_irq = 18
.....
.....
    def __init__(self, bus=0, device=0, speed=1000000, pin_rst=22,
            [b]pin_ce=1[/b], pin_irq=18, pin_mode=GPIO.BOARD):
        self.pin_rst = pin_rst
        self.pin_ce = pin_ce
        self.pin_irq = pin_irq

        self.spi = spidev.SpiDev()
        self.spi.open(bus, device)
        self.spi.max_speed_hz = speed

I understand the logic behind your solution, but I think my being a layman with the Rpi4 is preventing me from applying it. I also understand the wiring scheme you presented for the new MOSI/MISO/SCLK connections, as I had stumbled on this website: https://pinout.xyz.

If it is not too much to ask, could I get your continued help as I try to resolve my issue? Do you think there is still something I am doing wrong? I have some new ideas that I am trying to research. In particular, having discovered the pinout.xyz website, I'm trying to see if there is a way to program the GPIO pins to use one of their "alternative" functions. I feel that if I can do this, then my problem should be solved.

Thank you.

Return to “Interfacing (DSI, CSI, I2C, etc.)”