The RC522 RFID module allows the Raspberry Pi to read and write RFID (Radio Frequency Identification) tags, making it useful for access control, automation, and IoT projects. This guide will show you how to connect, configure, and read RFID cards using the Raspberry Pi and Python.
What You Will Need
- Raspberry Pi (any model with SPI support, e.g., Pi 3, Pi 4)
- RC522 RFID Module
- RFID Tag or Card (compatible with 13.56MHz frequency)
- Breadboard and Jumper Wires
- Python installed on your Raspberry Pi
Step 1: Wiring the RC522 to the Raspberry Pi
The RC522 communicates with the Raspberry Pi using the SPI protocol.
Connections (SPI Mode)
RC522 Pin | Raspberry Pi Pin | Function |
---|---|---|
VCC | 3.3V (Pin 1) | Power Supply |
GND | Ground (Pin 6) | Ground |
SDA (SS) | GPIO8 (Pin 24) | SPI Chip Select |
SCK | GPIO11 (Pin 23) | SPI Clock |
MOSI | GPIO10 (Pin 19) | SPI Data Input |
MISO | GPIO9 (Pin 21) | SPI Data Output |
RST | GPIO25 (Pin 22) | Reset |
Note: The RC522 operates at 3.3V. Do not connect it to 5V, as it may damage the module.
Step 2: Enable the SPI Interface
The RC522 uses SPI (Serial Peripheral Interface) for communication, so we need to enable SPI on the Raspberry Pi.
-
Open the Raspberry Pi configuration tool:
sudo raspi-config
-
Navigate to Interface Options > SPI and enable it.
-
Reboot the Raspberry Pi:
sudo reboot
-
Verify that SPI is enabled:
ls /dev/spidev*
You should see
/dev/spidev0.0
and/dev/spidev0.1
.
Step 3: Install Required Libraries
- Update your Raspberry Pi:
sudo apt update && sudo apt upgrade -y
- Install the SPI and RFID libraries:
sudo apt install -y python3-pip python3-spidev python3-rpi.gpio pip3 install mfrc522
Step 4: Python Code to Read RFID Data
Here’s a Python script to read RFID tag/card data using the RC522 module.
Python Code Example
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
print("Place your RFID tag or card near the reader...")
id, text = reader.read()
print(f"RFID Tag ID: {id}")
print(f"Stored Text: {text}")
except KeyboardInterrupt:
print("Exiting...")
finally:
GPIO.cleanup()
Step 5: Writing Data to an RFID Tag
You can also write custom text to an RFID tag.
Python Code Example for Writing Data
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
text = input("Enter text to write to the RFID tag: ")
print("Place your RFID tag near the reader...")
reader.write(text)
print("Data written successfully!")
except KeyboardInterrupt:
print("Exiting...")
finally:
GPIO.cleanup()
Step 6: Applications of the RC522 RFID Module
- Access Control – Use RFID cards to unlock doors or grant access to restricted areas.
- Attendance Systems – Log entries and exits in schools, offices, or events.
- Inventory Management – Track items using RFID tags.
- IoT and Automation – Trigger smart home devices or industrial systems with RFID authentication.
Troubleshooting
-
RFID Tag Not Detected
- Ensure the tag is within 2-3 cm of the reader.
- Check the wiring and connections.
-
Permission Denied Error
- Run the script with
sudo
:sudo python3 rfid_read.py
- Run the script with
-
No SPI Device Found
- Ensure SPI is enabled (
ls /dev/spidev*
should list devices). - Try re-enabling SPI via
sudo raspi-config
.
- Ensure SPI is enabled (
Conclusion
The RC522 RFID module is a powerful tool for integrating RFID-based identification and automation into Raspberry Pi projects. By following this guide, you can easily read and write RFID tag data, enabling applications in access control, security, and IoT projects. Experiment with different use cases to unlock its full potential! 🚀