Search code examples
pythonraspberry-pigpiorfidspi

Unable to connect RC522 RFID module to Raspberry Pi 4


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

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


Solution

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