2

I connect my RC522 RFID module to my Raspberry Pi 4 according to https://pimylifeup.com/raspberry-pi-rfid-rc522/ so we have Write.py and Read.py:

Write.py:

#!/usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        text = input('New data:')
        print("Now place your tag to write")
        reader.write(text)
        print("Written")
finally:
        GPIO.cleanup()

and Read.py is:

#!/usr/bin/env python

import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        id, text = reader.read()
        print(id)
        print(text)
finally:
        GPIO.cleanup()

but when i execute sudo python3 Write.py this error appears:

Traceback (most recent call last):
  File "Write.py", line 6, in <module>
    reader = SimpleMFRC522()
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 14, in __init__
    self.READER = MFRC522()
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py", line 130, in __init__
    self.spi.open(bus, device)

and for Read.py we have almost as same as Write.py execution error something like this:

Traceback (most recent call last):
  File "Read.py", line 6, in <module>
    reader = SimpleMFRC522()
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 14, in __init__
    self.READER = MFRC522()
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py", line 130, in __init__
    self.spi.open(bus, device)
FileNotFoundError: [Errno 2] No such file or directory

I have tried several ways but did not work at all: 1-checking wiring 2-using python2 3-checking SPI enabling using GUI and also boot/config.txt 4-using sudo apt-get update , sudo apt-get upgrade , sudo apt-get install python3-dev python3-pip and sudo pip3 install spidev.

I tried lsmod |grep spi to check spi and the result was:

spidev                 20480  0
spi_bcm2835            24576  0
spi_bcm2835aux         16384  0

what is the problem do you think? My Raspberry Pi 4 also had a 3.5-inch touchscreen LCD in the box. Is this the reason that spi0 is reserved? how to fix it? I can't even use another RC522 program those are in github.com and they have almost the same error in spi.open(bus, device). Is there any way to connect RC522 to my raspberry pi using a USB connection?

6
  • There is no error reported in the first traceback.
    – joan
    Aug 23 at 8:47
  • 1
    Some troubleshooting suggestions: electronics.stackexchange.com/questions/519993/…
    – tlfong01
    Aug 23 at 10:51
  • @joan I think actually one last line is not pasted here maybe. but it's something like the Read.py Traceback. anyway thanks
    – Pouya
    Aug 24 at 7:53
  • 1
    @tlfong01 not solved but thanks :)
    – Pouya
    Aug 24 at 7:55
  • 1
    Many (WaveShare) LCD display use SPI exclusively and causes trouble. Of course you can put the LCD display away and see if your problem disappears. Another complexity is that PiMyLifeUp's SPI is not the same as Rpi OS's build in SPI, and that might cause confusion.
    – tlfong01
    Aug 24 at 8:00
2

My guess is that you have not enabled the SPI interface. You should be able to use raspi-config to do this, but I prefer to modify /boot/config.txt. For example, my config.txt includes these lines:

dtparam=spi=on
# spi0 is used for RFID reader
dtoverlay=spi0-1cs

You will need a different overlay if you are not using SPI0.

1
  • Thanks. I edited boot/config.txt to enable SPI0 before but does not work. my config.txt: 'dtparam=i2c_arm=on dtparam=spi=on enable_uart=1 dtparam=audio=on dtoverlay=waveshare35a hdmi_cvt 480 320 60 6 0 0 0 hdmi_drive=2 dtoverlay=spi0-ics device_tree_param=spi=on dtoverlay=spi-bcm2708 dtoverlay=spi0-hw-cs' If I wanna use another SPI what should i do? How config.txt should be edited? GPIOs connection and ...?
    – Pouya
    Aug 25 at 7:10
0

I found out the simple answer. just reinstall the Raspbian OS to reset the SPI configuration (which was set for 3.5 inch LCD) so RFID writing and reading work well and everything goes correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.