树莓派4 SPI通道被占用致RFID-RC522程序无法运行求助
Hey David, let's figure out why your RFID setup is failing on the Pi4 even though it works perfectly on the Pi3+. The clue is right in your lsmod output—let's break this down step by step.
First, Understand the lsmod Difference
On your Pi3+, spidev 20480 0 means no processes are using the SPI device (the final 0 is the use count). But on the Pi4, spidev 20480 2 tells us two processes are already holding onto SPI resources, which is why your Python program can't access the RFID reader.
Step-by-Step Fixes
1. Find Out What's Occupying SPI
Run these commands to identify exactly which processes are using the SPI device nodes:
lsof /dev/spidev0.0 lsof /dev/spidev0.1
Common culprits include auto-started system services (like pigpio), other SPI-based programs, or even default Pi4 services that reserve SPI ports automatically.
2. Stop and Disable the Occupying Processes
If you find a persistent service (e.g., pigpiod), stop it immediately:
sudo systemctl stop pigpiod
To prevent it from starting on boot (so the issue doesn't come back after a restart):
sudo systemctl disable pigpiod
If it's a user-run program, kill its process with kill [PID] (replace [PID] with the number from the lsof output).
3. Double-Check Pi4 SPI Configuration
Even if you think the config matches the Pi3+, verify these settings to be safe:
- Open
raspi-configwithsudo raspi-config - Go to Interface Options > SPI and confirm it's enabled
- Ensure no other conflicting interfaces (like I2C) are interfering (this is rare, but worth a quick check)
4. Adjust Your Python Code to Use a Different SPI Channel
The SimpleMFRC522 library defaults to spidev0.0, but if that's occupied, try specifying the alternate SPI device (spidev0.1) when initializing the reader:
# Modify the initialization line in your code CardReader = SimpleMFRC522(spi_port=0, spi_device=1)
Just make sure your RFID-RC522's CS pin is connected to GPIO7 (Pin 26) for spidev0.1 (if you were using GPIO8/Pin24 for spidev0.0).
5. Reset the SPI Module
If none of the above works, unload and reload the SPI kernel modules to clear any stuck connections:
sudo rmmod spidev sudo rmmod spi_bcm2835 sudo modprobe spi_bcm2835 sudo modprobe spidev
Then re-run your program to test.
6. Verify Hardware Connections
Double-check your wiring—Pi4 uses the same SPI pinout as Pi3+, but loose connections are easy to miss:
- SCK → GPIO11 (Pin 23)
- MOSI → GPIO10 (Pin 19)
- MISO → GPIO9 (Pin 21)
- CS → GPIO8 (Pin24) or GPIO7 (Pin26)
- VCC → 3.3V (not 5V—RC522 is 3.3V only!)
- GND → GND
Final Notes
Most likely, a background service is hogging the SPI port on your Pi4. The lsof command will point you directly to the culprit, and stopping it should get your reader working. If you still run into issues, try updating the mfrc522 library with pip install --upgrade mfrc522 to ensure compatibility with the Pi4's kernel.
内容的提问来源于stack exchange,提问作者David Leweke

