3

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?

5eleven7
  • 19
  • 4
Pouya
  • 55
  • 1
  • 11

2 Answers2

3

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.

Elliot Alderson
  • 206
  • 2
  • 7
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.

Pouya
  • 55
  • 1
  • 11